Advertisement
Nayeemzaman

Untitled

Apr 3rd, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct mylist
  5. {
  6.     int data;
  7.     char name[20];
  8.     struct mylist *next;
  9. } node;
  10.  
  11. void insert(node *p,char name[20], int data)
  12. {
  13.     while(p->next != NULL)
  14.     {
  15.         p = p->next;
  16.     }
  17.     p->next = (node *)malloc(sizeof(node));
  18.     strcpy(p->next->name,name);
  19.     p->next->data = data;
  20.     p->next->next = NULL;
  21. }
  22.  
  23. int search(node *p, char name[20])
  24. {
  25.     p = p->next;
  26.     while(p != NULL)
  27.     {
  28.         if(strcmp(p->name,name) == 0)
  29.         {
  30.             printf("Price is %d\n",p->data);
  31.             return;
  32.         }
  33.         p = p -> next;
  34.     }
  35.     printf("Sorry!Element not found.\n");
  36.     return;
  37. }
  38.  
  39. void delete(node *p, char name[20])
  40. {
  41.     node *temp;
  42.     while(p->next != NULL)
  43.     {
  44.         if(strcmp(p->next->name,name) == 0)
  45.         {
  46.             temp = p->next;
  47.             p->next = temp->next;
  48.             free(temp);
  49.             return 0;
  50.         }
  51.         p = p->next;
  52.     }
  53. }
  54.  
  55. void display(node *p)
  56. {
  57.     while(p -> next != NULL)
  58.     {
  59.         printf("%s %d\n", p->next->name, p->next->data);
  60.         p = p->next;
  61.     }
  62. }
  63. int main()
  64. {
  65.     int q,price;
  66.     char name[20];
  67.     node *start = (node *)malloc(sizeof(node));
  68.     start -> next = NULL;
  69.  
  70.     printf("1. Insert\n");
  71.     printf("2. Delete\n");
  72.     printf("3. Print\n");
  73.     printf("4. Search\n");
  74.     while(1)
  75.     {
  76.         printf("Enter your choice: ");
  77.  
  78.         scanf("%d",&q);
  79.  
  80.         switch(q)
  81.         {
  82.         case 1:
  83.             printf("Enter the product name & price:\n");
  84.             scanf("%s %d",name,&price);
  85.             //scanf("%d",&data);
  86.             insert(start,name,price);
  87.             break;
  88.  
  89.         case 2:
  90.             scanf("%s", name);
  91.             delete(start,name);
  92.             break;
  93.  
  94.         case 3:
  95.             printf("The list is\n");
  96.             display(start);
  97.             printf("\n");
  98.             break;
  99.  
  100.         case 4:
  101.             scanf("%s",name);
  102.             search(start,name);
  103.             break;
  104.            
  105.         default:
  106.             printf("Invalid choice.Try again!\n");
  107.             break;
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement