Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node{
  5. int info;
  6. struct node *link;
  7. };
  8.  
  9. struct node *start = NULL, *last=NULL;
  10.  
  11. void ins_at_beg();
  12. void ins_at_pos();
  13. void ins_at_end();
  14. void traverse();
  15.  
  16. int main(){
  17. while(1){
  18. printf("\n\t***MAIN MENU***");
  19. printf("\n1.Insert at beginning");
  20. printf("\n2.Insert at intermediate");
  21. printf("\n3.Insert at end");
  22. printf("\n4.Display");
  23. printf("\n5Exit\n");
  24.  
  25. switch(getch()){
  26. case '1': ins_at_beg(); break;
  27. case '2': ins_at_pos(); break;
  28. case '3': ins_at_end(); break;
  29. case '4': traverse(); break;
  30. case '5': exit(0); break;
  31.  
  32. }
  33. }
  34. }
  35.  
  36.  
  37. void ins_at_beg(){
  38. struct node *temp;
  39. temp = (struct node*) malloc(sizeof(struct node));
  40. printf("\nEnter value to insert: ");
  41. scanf("%d", &temp->info);
  42.  
  43. if(start==NULL){
  44. temp->link = temp;
  45. start = temp;
  46. last = temp;
  47. }else{
  48. temp->link = start;
  49. start = temp;
  50. last->link = temp;
  51. }
  52. printf("\n%d has been inserted at the beginning.",temp->info);
  53. }
  54.  
  55. void ins_at_end(){
  56. struct node *temp;
  57.  
  58. temp = (struct node*) malloc(sizeof(struct node));
  59. printf("\nEnter value to insert: ");
  60. scanf("%d", &temp->info);
  61.  
  62. if(start==NULL){
  63. temp->link = temp;
  64. start = temp;
  65. last = temp;
  66. }else{
  67. temp->link = start;
  68. last->link = temp;
  69. last = temp;
  70. }
  71.  
  72. printf("\n%d has been inserted at the end.",temp->info);
  73. }
  74.  
  75. void ins_at_pos(){
  76. int i, pos;
  77.  
  78. struct node *temp;
  79. struct node *temp1;
  80.  
  81. temp = (struct node*) malloc(sizeof(struct node));
  82. temp1 = (struct node*) malloc(sizeof(struct node));
  83.  
  84. printf("\nEnter value to insert: ");
  85. scanf("%d", &temp->info);
  86.  
  87. if(start==NULL){
  88. printf("\nList is empty");
  89. }else{
  90. printf("\nEnter which position you want to insert at: ");
  91. scanf("%d",&pos);
  92. temp1 = start;
  93. for(i=-1; i <= pos-1 ;i++){
  94. temp1 = temp1 -> link;
  95. }
  96. temp->link = temp1 -> link;
  97. temp1 -> link = temp;
  98. printf("\n%d has been inserted at pos %d", temp->info, pos);
  99.  
  100. }
  101.  
  102. }
  103.  
  104. void traverse(){
  105. struct node *temp;
  106.  
  107. temp = (struct node*) malloc(sizeof(struct node));
  108. if(start==NULL){
  109. printf("\nList is empty");
  110. }else{
  111. temp = start;
  112. while(temp->link != start){
  113. printf("%d, ", temp->info);
  114. temp = temp->link;
  115. }
  116. printf("%d, ", temp->info);
  117.  
  118. }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement