Objective C - Linked List printing 0s -


i making linked list in objective c , works part. issue when print items list, prints 0 after printing each item.

the output of code is:

5 0 6 0 7

i trying figure out why 0 showing , how stop it. code is:

the node class header file

@interface node : nsobject {     nsinteger data; }  -(id) initwith : (nsinteger) number;  @property (nonatomic) nsinteger getdata; @property (nonatomic, strong) node *nextnode; 

the node class implementation file

-(id) initwith: (nsinteger)number {     self = [super init];      if (self) {         node *node = [[node alloc] init];         self.nextnode = node;         data = number;     }      return self; }  -(id) init {     self = [super init];      if (self) {         // custom initialization     }      return self; }  -(nsinteger)getdata {     return data; } 

the linkedlist header file

@interface linkedlist : nsobject {     nsinteger data; }  -(id)initwith : (nsinteger) number; -(void) insertinteger : (nsinteger) number; -(void) printlist; 

the linkedlist implementation file

@implementation linkedlist {     node *head; }  -(id)initwith:(nsinteger)number {     self = [super init];      if (self) {         if (head == nil) {             head = [[node alloc] initwith:number];         }     }     return self; }  -(id) init {     self = [super init];      if (self) {         // custom initialization     }      return self; }  -(void) insertinteger:(nsinteger)number {      if (head == nil) {         head = [[node alloc] initwith:number];     }     else {         // traverse end of linked list         node *currentnode = head;          while (currentnode.nextnode != nil) {             currentnode = currentnode.nextnode;         }          // add item @ end of list         node *insertnode = [[node alloc] initwith:number];         [currentnode setnextnode:insertnode];     } }  -(void) printlist {     if (head == nil) {         nslog(@"list empty");     }     else {         node *currentnode = head;         while (currentnode.nextnode !=nil) {             nslog(@"%tu", currentnode.getdata);             currentnode = currentnode.nextnode;         }     } } 

the main class file

int main(int argc, const char * argv[]) {      linkedlist *list1 = [[linkedlist alloc] init];     [list1 insertinteger:5];     [list1 insertinteger:6];     [list1 insertinteger:7];      [list1 printlist];      return 0; } 

the problem seems every time create node, new node creates yet node , makes it's "next" node. why? don't create 2nd node, set nextnode nil.

so have node between each proper node.

and need adjust of code walk list. example, printing should be:

-(void) printlist {     if (head == nil) {         nslog(@"list empty");     }     else {         node *currentnode = head;         while (currentnode) {             nslog(@"%tu", currentnode.getdata);             currentnode = currentnode.nextnode;         }     } } 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -