Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute Swift Online

import Foundation

struct LinkedList<Value> {
    
    var head : Node<Value>?
    var tail : Node<Value>?
    
    
    init() {}
    
    func isEmpty() -> Bool {
        return head == nil
    }
    
   mutating func push(_ value: Value) {
        
        head = Node(value: value, next: head)
        
        if tail == nil {
           tail = head
        }
    }
    
    mutating func append(_ value : Value)
    {
        
    }
    
}
 
 class Node<Value> {
     
     var value :Value
     var next : Node?
     
     init(value:Value, next:Node? = nil )
     {
         self.value = value
         self.next = next
     }
 }
 
 
 extension LinkedList : CustomStringConvertible {
     
     var description : String {
         
         guard let head = head else {
            return " Empty List"    
         }
         
         return String(describing: head)
         
     }
     
 }
 
 
 extension Node : CustomStringConvertible {
     
     var description : String {
         guard let next = next else {
             return "\(value)"
         }
         return "\(value) -> " + String(describing : next) + " "
         
     }
 }
 
var list = LinkedList<Int>();
list.push(1)
list.push(2)
list.push(3)
list.push(5)
 
 print(list)

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.