Advertisement
f0rkB0mb

Firoz Ansari wrote...

Jun 10th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.67 KB | None | 0 0
  1. /*******************************************************************
  2. ** @author firoz **
  3. ** **
  4. ** **
  5. ** Description: A program to: **
  6. ** **
  7. ** 1)INSERT **
  8. ** at first position, last position and arbitrary position. **
  9. ** 2)TRAVERSE **
  10. ** 3)DELETE **
  11. ** at first position, last position and arbitrary position. **
  12. ** 4)SORT **
  13. ** **
  14. ** the elements in a LINKED LIST. **
  15. *******************************************************************/
  16.  
  17. #include<stdio.h>
  18. #include<stdlib.h>
  19.  
  20. typedef struct node
  21. {
  22. int data;
  23. struct node *next;
  24. } *Node;
  25.  
  26. /////////////////////////////
  27. Node insertFront( Node head )
  28. {
  29. Node temp;
  30. char choice;
  31. do{
  32. printf("\nEnter the Element in the linked list:\n");
  33. temp=(Node)malloc(sizeof(struct node));
  34. scanf("%d",&temp->data);
  35. temp->next=head;
  36. head=temp;
  37. printf("%d Entered at front\nWanna insert more data in the front?(Y/N) :",head->data);
  38. scanf("\n %c",&choice);
  39. }while(choice=='y'||choice=='Y');
  40.  
  41. return head;
  42. }
  43.  
  44. //////////////////////////////
  45. void traverse(Node head){
  46. Node temp;
  47. temp = head;
  48. printf("\n***Traversing the Linked List:***\n");
  49. while(temp!=NULL){
  50. printf("%d-> ",temp->data);
  51. temp=temp->next;
  52. }
  53. printf("NULL\n");
  54. }
  55.  
  56. //////////////////////////
  57. Node insertBack(Node head){
  58. Node temp, tail;
  59. char choice;
  60. tail = head;
  61. if(head==NULL){
  62. printf("Linked list is empty, therefore inserting the node at first position:\n");
  63. head=insertFront(head);
  64. }
  65. else{
  66. while(tail->next!=NULL)
  67. tail=tail->next;
  68. do{
  69. temp=(Node)malloc(sizeof(struct node));
  70. printf("\nEnter Element of Linked list\n");
  71. scanf("%d",&temp->data);
  72. temp->next=NULL;
  73. tail->next=temp;
  74. tail=tail->next;
  75. printf("%d is inserted at end\nWanna Insert more data at end(Y/N)?",tail->data);
  76. scanf("\n%c",&choice);
  77. }while(choice=='y'||choice=='Y');
  78. }
  79. return head;
  80. }
  81.  
  82. /////////////////////////////
  83. Node insertPosition(Node head){
  84. int i, n;
  85. char choice;
  86. Node temp, position;
  87. do{
  88. position=head;
  89. printf("Enter the position at which you want to insert the data\n");
  90. scanf("%d",&n);
  91. if(n==1)
  92. head=insertFront(head);
  93. else{
  94. for(i=2; i<n && position!=NULL ; i++)
  95. position=position->next;
  96. if(position==NULL){
  97. printf("Invalid position for insertion !\n Press Enter to Continue.....\t");
  98. fflush(stdin);
  99. getchar();
  100. getchar();
  101. }
  102. else{
  103. temp=(Node)malloc(sizeof(struct node));
  104. printf("Enter the data to be inserted at position %d:\t",n);
  105. scanf("%d",&temp->data);
  106. temp->next = position -> next;
  107. position->next = temp;
  108. printf("%d is inserted at position %d",position->next->data, n);
  109. }
  110. }
  111. printf("\nDo wanna insert more data at arbitary position?(Y/N)\n");
  112. scanf("\n%c",&choice);
  113. }while(choice=='y'||choice=='Y');
  114. return head;
  115. }
  116.  
  117. //////////////////////
  118. Node deleteFront(Node head){
  119. Node temp;
  120. temp=head;
  121. head=temp->next;
  122. free(temp);
  123. return head;
  124. }
  125.  
  126. //////////////////////
  127. Node deleteBack(Node head){
  128. Node temp, old_temp;
  129. temp=head;
  130. old_temp=temp;
  131. while(temp->next!=NULL){
  132. old_temp=temp;
  133. temp=temp->next;
  134. }
  135. if(head->next==NULL)
  136. head=NULL;
  137. old_temp->next=NULL;
  138. free(temp);
  139. return head;
  140. }
  141.  
  142. /////////////////////
  143. Node deletePosition(Node head,int n){
  144. Node temp, old_temp;
  145. temp=head;
  146. old_temp=temp;
  147. int i;
  148. if(n==1){
  149. head=deleteFront(head);
  150. printf("data at position %d is deleted\n",n);
  151. }
  152. else{
  153. for(i=1;i<n && temp!=NULL;i++){
  154. old_temp=temp;
  155. temp=temp->next;
  156. }
  157. if(temp==NULL){
  158. printf("Invalid position for deletion!\n");
  159. }
  160. else{
  161. old_temp->next=temp->next;
  162. free(temp);
  163. printf("data at position %d is deleted\n",n);
  164. }
  165. }
  166. return head;
  167. }
  168.  
  169. /////////////////////////
  170. void sort(Node head){
  171. int temp=0;
  172. Node temp1, temp2;
  173. for( temp1 = head; temp1 != NULL; temp1 = temp1->next)
  174. for(temp2=temp1->next; temp2 != NULL; temp2 = temp2->next)
  175. if( temp1->data > temp2->data){
  176. temp = temp1->data;
  177. temp1->data = temp2->data;
  178. temp2->data = temp;
  179. }
  180. printf("***Now the linked list is sorted***\nThe Sorted list is::\n");
  181. traverse(head);
  182. }
  183.  
  184. int main()
  185. {
  186. int select, n;
  187. Node head=NULL;
  188. char choice;
  189. printf("\n\n***Hi! Welcome to the program of making linked list***\n");
  190.  
  191. do{
  192. printf("Press 1 to Insert:\n");
  193. printf("Press 2 to Traverse:\n");
  194. printf("Press 3 to Delete:\n");
  195. printf("Press 4 to Sort:\n");
  196. scanf("%d",&select);
  197.  
  198. if(select==1){
  199. //code for insert
  200. printf("\nPress 1 to Insert at Front:\n");
  201. printf("Press 2 to Insert at Back:\n");
  202. printf("Press 3 to Insert at Specific position:\n");
  203. scanf("%d",&n);
  204. switch(n){
  205. case 1:
  206. //insert at front
  207. head=insertFront(head);
  208. break;
  209. case 2:
  210. //insert at back
  211. head=insertBack(head);
  212. break;
  213. case 3:
  214. //insert at specific position
  215. head=insertPosition(head);
  216. break;
  217. default :
  218. printf("Wrong choice\n");
  219. }
  220. }
  221. else if(select==2){
  222. //code for Traverse
  223. traverse(head);
  224. }
  225. else if(select==3){
  226. if(head!=NULL){
  227. //cade for Delete
  228. printf("\nPress 1 to Delete at Front:\n");
  229. printf("Press 2 to Delete at Back:\n");
  230. printf("Press 3 to Delete at Specific position:\n");
  231. scanf("%d",&n);
  232. switch(n){
  233. case 1:
  234. //insert at front
  235. head=deleteFront(head);
  236. printf("First Node is deleted\n");
  237. break;
  238. case 2:
  239. //insert at back
  240. head=deleteBack(head);
  241. printf("Last Node is deleted\n");
  242. break;
  243. case 3:
  244. //insert at specific position
  245. printf("Enter the position at which data is to be deleted\n");
  246. scanf("\n%d",&n);
  247. if(n==1)
  248. head=deleteFront(head);
  249. else
  250. head=deletePosition(head, n);
  251. break;
  252. default :
  253. printf("Wrong choice\n");
  254. }
  255. }
  256. else{
  257. printf("The linked list is already empty!!!\n");
  258. }
  259. }
  260. else if(select==4){
  261. //code for sort
  262. sort(head);
  263. }
  264. else{
  265. printf("\nYou have Entered Wrong option, Try again::\n\n");
  266. choice='y';
  267. continue;
  268. }
  269. printf("\nDo you want to continue in Linked list Program?\nPress Y for Yes and N for No::\n");
  270. scanf("\n");
  271. choice=getchar();
  272. }while(choice=='y'||choice=='Y');
  273. return 0;
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement