Adrita

sem 2 lab 4 linked list

Jul 25th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include<stdio.h>
  2. struct linked_list
  3. {
  4. int data;
  5. struct linked_list *next;
  6. };
  7. typedef struct linked_list *node;
  8.  
  9. node createnode()
  10. {
  11. node temp;
  12. temp=(node)malloc(sizeof(node));
  13. temp->next=NULL;
  14. return temp;
  15. }
  16. node add_node(node head,int value)
  17. {
  18. node temp=createnode(),p;
  19. temp->data=value;
  20. if(head==NULL)
  21. head=temp;
  22. else
  23. {
  24. p=head;
  25. while(p->next!=NULL)
  26. p=p->next;
  27. p->next=temp;
  28. }
  29. return head;
  30. }
  31. void print_node(node head)
  32. {
  33. node a;
  34. if(head==NULL)
  35. printf("empty\n");
  36. else
  37. {
  38. a=head;
  39. while(a!=NULL)
  40. {
  41. printf("%d ",a->data);
  42. a=a->next;
  43. }
  44. }
  45. printf("\n");
  46. }
  47. int main()
  48. {
  49. int n;
  50. node head;
  51. head=NULL;
  52. x:
  53. scanf("%d",&n);
  54. if(n==1)
  55. {
  56. int ndata;
  57. printf("new data\n");
  58. scanf("%d",&ndata);
  59. head=add_node(head,ndata);
  60. goto x;
  61. }
  62. else if(n==2)
  63. {
  64. print_node(head);
  65. goto x;
  66. }
  67. else if(n==0)
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment