ZinedinZidan

Single Linked List

Jan 22nd, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct mylist
  4. {
  5.     int data;
  6.     struct mylist*next;
  7. };
  8.  
  9. struct mylist*head=NULL;
  10.  
  11. void display()
  12. {
  13.     struct mylist*temp=head;
  14.     while(temp!=NULL)
  15.     {
  16.         printf("%d->",temp->data);
  17.         temp=temp->next;
  18.  
  19.     }
  20.     printf("\n");
  21. }
  22.  
  23. void insert_first(int data)
  24. {
  25.         struct mylist*temp=(struct mylist*)malloc(sizeof(struct mylist));
  26.         temp->data=data;
  27.         temp->next=head;
  28.         head=temp;
  29. }
  30. void insert_last(int data)
  31. {
  32.     struct mylist*temp=head;
  33.     struct mylist*new_node=(struct mylist*)malloc(sizeof(struct mylist));
  34.     new_node->data=data;
  35.     new_node->next=NULL;
  36.     if(temp==NULL)
  37.     {
  38.         head=new_node;
  39.         return;
  40.     }
  41.     while(temp->next!=NULL)
  42.     {
  43.         temp=temp->next;
  44.     }
  45.     temp->next=new_node;
  46.     return;
  47. }
  48. void create(int data)
  49. {
  50.     insert_last(data);
  51. }
  52. int main()
  53. {
  54.     insert_last(8);
  55.     insert_last(5);
  56.     display();
  57.     return 0;
  58. }
Add Comment
Please, Sign In to add comment