Advertisement
ZinedinZidan

stringsinglelinkedlist

Mar 2nd, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. typedef struct node nd;
  5. struct node
  6. {
  7.     int id;
  8.     char name[80];
  9.     nd *next;
  10. };
  11. nd *head=NULL;
  12.  
  13. void insertfirst(int id,char name[80])
  14. {
  15.     nd *newnode=(nd*)malloc(sizeof(nd));
  16.     strcpy(newnode->name,name);
  17.     newnode->id=id;
  18.     newnode->next=head;
  19.     head=newnode;
  20.     return;
  21. }
  22. void insertmiddle(int n,int id,char name[80])
  23. {
  24.     nd *temp=head;
  25.     nd *newnode=(nd*)malloc(sizeof(nd));
  26.     strcpy(newnode->name,name);
  27.     newnode->id=id;
  28.     int i;
  29.     for(i=0;i<(n-2);i++)
  30.     {
  31.         temp=temp->next;
  32.     }
  33.     nd *temp1=temp->next;
  34.     temp->next=newnode;
  35.     newnode->next=temp1;
  36.     return;
  37.  
  38. }
  39. void insertlast(int id,char name[80])
  40. {
  41.     nd *temp=head;
  42.     nd *newnode=(nd*)malloc(sizeof(nd));
  43.     strcpy(newnode->name,name);
  44.     newnode->id=id;
  45.     newnode->next=NULL;
  46.     if(head==NULL)
  47.     {
  48.         head=newnode;
  49.         return;
  50.     }
  51.     while(temp->next!=NULL)
  52.     {
  53.         temp=temp->next;
  54.     }
  55.     temp->next=newnode;
  56.     return;
  57. }
  58. void delete(int id)
  59. {
  60.     nd *temp=head;
  61.     int i,count=0;
  62.     while(temp!=NULL)
  63.     {
  64.         count++;
  65.         if(temp->id==id)
  66.         {
  67.             break;
  68.  
  69.         }
  70.         temp=temp->next;
  71.     }
  72.     if(count==1)
  73.     {
  74.         head=temp->next;
  75.         return;
  76.     }
  77.     nd *temp2=head;
  78.     for(i=0;i<(count-2);i++)
  79.     {
  80.         temp2=temp2->next;
  81.     }
  82.     nd *temp1=temp2->next;
  83.     temp2->next=temp1->next;
  84.     return;
  85. }
  86. void search(int id)
  87. {
  88.     nd *temp=head;
  89.     while(temp!=NULL)
  90.     {
  91.         if(temp->id==id)
  92.         {
  93.             printf("Number is found\n",temp->id);
  94.             break;
  95.  
  96.         }
  97.         temp=temp->next;
  98.     }
  99.     return;
  100. }
  101. void display()
  102. {
  103.     nd *temp=head;
  104.     while(temp!=NULL)
  105.     {
  106.         printf("Id: %d Name: %s\n",temp->id,temp->name);
  107.         temp=temp->next;
  108.     }
  109.     printf("\n");
  110. }
  111. int main()
  112. {
  113.     nd *temp=head;
  114.     insertfirst(192152784,"Jamir Uddin Zidan");
  115.     insertfirst(192152776,"Osman Goni");
  116.     insertfirst(192152878,"Nahid Hasan");
  117.     insertmiddle(2,192152886,"Akash Sarker");
  118.     insertlast(192152774,"Mredul Hasan Pias");
  119.     display();
  120.     delete(192152776);
  121.     display();
  122.     search(192152784);
  123.     display();
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement