bishalbiswas

traversing a linked list

Sep 6th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<iostream>
  4. using namespace std;
  5. struct node
  6. {
  7. char info[10];
  8. struct node *link;
  9. } *start=NULL;
  10. int main()
  11. {
  12. int size;
  13. struct node *ptr;
  14. cout<<"size of the list:";
  15. cin>>size;
  16. if(start==NULL)
  17. {
  18. start = (struct node *)malloc(sizeof(struct node));
  19. cin>>start->info;
  20. start->link=NULL;
  21. }
  22. ptr=start;
  23.  
  24.  
  25. for(int i=0;i<size-1;i++)
  26. {
  27.  
  28. ptr->link=(struct node *)malloc(sizeof(struct node));
  29. ptr=ptr->link;
  30. cin>>ptr->info;
  31. ptr->link=NULL;
  32. }
  33. cout<<"the list is:\n";
  34. ptr=start;
  35. for(int i=0;i<size;i++)
  36. {
  37.  
  38. cout<<ptr->info<<endl;
  39. ptr=ptr->link;
  40. }
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment