Skip to main content

Singly Linked List

Singly Linked List 

CODE
๐Ÿ‘‡
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node 
  4. {
  5. int info;
  6. struct node *next;
  7. }NODE;

  8. NODE* createlist(NODE *list); 
  9. void Display (NODE *list);
  10. void search (NODE *list);
  11. NODE* insertbeg(NODE *list);
  12. NODE* insertbetween(NODE *list);
  13. NODE* insertlast(NODE *list);
  14. NODE* Delpos(NODE *list);
  15. NODE* Delvalue(NODE *list);
  16. void main ()
  17. {
  18. NODE*list=NULL, *temp;
  19.        int ch,n,pos;
  20. list=createlist (list);
  21. Display (list);

  22. search (list);

  23. list=insertbeg (list);
  24. Display (list);
  25. list=insertbetween (list);
  26. Display (list);

  27. list=insertlast (list);
  28. Display (list);

  29. list=Delpos (list);
  30. Display (list);

  31. list=Delvalue (list);
  32. Display (list);
  33. }

  34. NODE* createlist (NODE *list)
  35. {
  36.  int n,count;
  37. NODE *temp, *newnode;
  38.  printf ("How many nodes you want to enter ? \n");
  39.  scanf("%d" ,&n);
  40.  for(count=1 ; count<=n; count++)
  41.   {
  42.   newnode=(NODE*)malloc(sizeof(NODE));
  43.  newnode->next=NULL;
  44.   printf( "Enter the node data:-  " );
  45. scanf ("%d",&newnode->info);
  46.  if (list==NULL)
  47.   {
  48.     list=temp=newnode;
  49.    }
  50.   else 
  51.  { 
  52.    temp->next=newnode;
  53.    temp=newnode;
  54.   }
  55. }
  56.   return list;
  57. }
  58.   void Display (NODE *list)
  59.  {
  60.   NODE *temp=list;
  61.    while(temp!=NULL)
  62.   {
  63.      printf ("%d",temp->info);
  64.      printf("-->");
  65.     temp=temp->next ;
  66.  }
  67.    printf("NULL \n");
  68. }

  69. NODE * insertbeg(NODE *list)
  70. {
  71. int n;
  72. printf("Enter the node you want to insertat first position:");
  73. scanf("%d", &n);
  74. NODE *newnode;
  75. newnode=(NODE*)malloc(sizeof(NODE));
  76. newnode->info=n;
  77. newnode->next=list;
  78. list=newnode;
  79. return list;
  80. }

  81. NODE * insertbetween(NODE *list)
  82. {
  83. NODE *newnode, *temp=list;
  84. int n,i,pos;
  85. printf("Enter the  node data and position you want to insert between the node: \t");
  86. scanf("\n%d", &n);
  87. scanf("\n%d", &pos);
  88. newnode=(NODE*)malloc(sizeof(NODE));
  89. newnode->next=NULL;
  90. newnode->info=n;

  91. for(i=1; i<pos-1&&temp->next!=NULL; i++)
  92. temp=temp->next;
  93. newnode->next=temp->next;
  94. temp->next=newnode;

  95. return list;
  96. }

  97. NODE * insertlast(NODE *list)
  98. {
  99. int n;
  100. printf("Enter the  node data you want to insert at last position: \t");
  101. scanf("%d", &n);
  102. NODE *newnode, *temp;
  103. newnode=(NODE*)malloc(sizeof(NODE));
  104. newnode->info=n;
  105. newnode->next=NULL;

  106. for(temp=list; temp->next!=NULL; temp=temp->next);

  107. temp->next=newnode;
  108. return list;
  109. }

  110. void search (NODE *list)
  111. {
  112. int num, flag=0;
  113. NODE *temp;
  114. printf("Enter the element to be search:");
  115. scanf("%d", &num);
  116. for(temp=list; temp!=NULL; temp=temp->next)
  117. {
  118. if(temp->info==num)
  119. {
  120. printf(">>%d is Found<<\n",num);
  121. flag=1;
  122. exit;
  123. }
  124. }
  125. if(flag==0)
  126. printf(">>%d is not found<<\n", num);
  127. }

  128. NODE * Delpos(NODE *list)
  129. {
  130. NODE *temp=list, *temp1;
  131. int i, pos;
  132. printf("Enter node position to delete the node: \t");
  133. scanf("%d", &pos);
  134. if(pos==1)
  135. {
  136. list=temp->next;
  137. free (temp);
  138. return list;
  139. }
  140. for(i=1, temp=list; i<=pos-1 && temp!=NULL; i++)
  141. temp=temp->next;
  142. if(temp==NULL)
  143. {
  144. printf(">>position is out of range<<");
  145. return list;
  146. }
  147. temp1=temp->next;
  148. temp->next=temp1->next;
  149. free (temp1);
  150. return list;
  151. }

  152. NODE* Delvalue(NODE *list)
  153. {
  154. NODE *temp=list, *temp1;
  155. int num;
  156. printf("Enter node data to delete that node \t");
  157. scanf("%d", &num);
  158. if(list->info==num)
  159. {
  160. list=list->next;
  161. free (temp);
  162. return list;
  163. exit;
  164. }
  165. for(temp=list; temp->next!=NULL; temp=temp->next)
  166. if(temp->next->info==num)
  167. {
  168. temp1=temp->next;
  169. temp->next=temp1->next;
  170. free (temp1);
  171. return list;
  172. exit;
  173. }
  174. printf(">>Element is not found<<\n");
  175. return list;
  176. }


๐Ÿ‘‰Execute๐Ÿ‘ˆ


//Output
/*
How many nodes you want to enter ? 
5

Enter the node data:-  9
Enter the node data:-  1
Enter the node data:-  4
Enter the node data:-  2
Enter the node data:-  3
9-->1-->4-->2-->3-->NULL 

Enter the element to be search:5
>>5 is not found<<

Enter the node you want to insertat first position:7
7-->9-->1-->4-->2-->3-->NULL 

Enter the  node data and position you want to insert between the node: 5 4
7-->9-->1-->5-->4-->2-->3-->NULL 

Enter the  node data you want to insert at last position: 8
7-->9-->1-->5-->4-->2-->3-->8-->NULL 

Enter node position to delete the node: 2
7-->9-->5-->4-->2-->3-->8-->NULL 

Enter node data to delete that node 3
7-->9-->5-->4-->2-->8-->NULL 

*/



  •  

                                                               //ThE ProFessoR

Comments