Telaryon

Láncolt lista példa

Mar 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct NODE {
  5.     int number;
  6.     struct NODE *next;
  7.     };
  8.  
  9. int search_value(struct NODE *llist, int num);
  10. void append_node(struct NODE *llist, int num);
  11. void display_list(struct NODE *llist);
  12. void delete_node(struct NODE *llist, int num);
  13.  
  14. int main(void){
  15.     int num = 0;
  16.     int input = 1;
  17.     int retval = 0;
  18.     struct NODE *llist;
  19.  
  20.     llist=(struct NODE *) malloc(sizeof(struct NODE));
  21.     llist->number = 0;
  22.     llist->next = NULL;
  23.  
  24.     while(input!=0){
  25.         printf("\n-- Menu Selection --\n");
  26.         printf("0) Quit\n");
  27.         printf("1) Insert\n");
  28.         printf("2) Delete\n");
  29.         printf("3) Search\n");
  30.         printf("4) Display\n");
  31.         scanf("%d", &input);
  32.  
  33.         switch(input) {
  34.         case 0:
  35.         default:
  36.             printf("Goodbye ...\n");
  37.             input = 0;
  38.             break;
  39.  
  40.         case 1:
  41.             printf("Your choice: 'Insertion'\n");
  42.             printf("Enter the value which should be inserted: ");
  43.             scanf("%d", &num);
  44.             break;
  45.  
  46.         case 2:
  47.             printf("Your choice: 'Deletion'\n");
  48.             printf("Enter the value which should be deleted: ");
  49.             scanf("%d", &num);
  50.             delete_node(llist, num);
  51.             break;
  52.  
  53.         case 3:
  54.             printf("Your choice: 'Search'\n");
  55.             printf("Enter the value you want to find: ");
  56.             scanf("%d", &num);
  57.             if((retval = search_value(llist, num)) == -1)
  58.                 printf("Value `%d' not found\n", num);
  59.             else
  60.                 printf("Value `%d' located at position `%d'\n", num, retval);
  61.             break;
  62.  
  63.         case 4:
  64.             printf("Your choice: `Display'\n");
  65.             display_list(llist);
  66.             break;
  67.         }
  68.     }
  69.     free(llist);
  70.     return (0);
  71. }
  72.  
  73. void display_list(struct NODE *llist){
  74.     while (llist->next != NULL){
  75.         printf("%d", llist->number);
  76.         llist = llist->next;
  77.     }
  78.     printf("%d", llist->number);
  79. }
  80.  
  81. void delete_node(struct NODE *llist, int num){
  82.     struct NODE *temp;
  83.     temp = (struct NODE *) malloc(sizeof(struct NODE));
  84.     if(llist->number == num){
  85.         temp = llist->next;
  86.         free(llist);
  87.     } else{
  88.         while(llist->next->number != num)
  89.             llist = llist->next;
  90.  
  91.         temp = llist->next->next;
  92.         free(llist->next);
  93.         llist->next = temp;
  94.     }
  95. }
  96.  
  97. int search_value(struct NODE *llist, int num){
  98.     int retval = -1;
  99.     int i = 1;
  100.     while(llist->next != NULL){
  101.         if(llist->next->number == num)
  102.             return i;
  103.         else i++;
  104.         llist = llist->next;
  105.     }
  106.     return retval;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment