Advertisement
Nayeemzaman

Untitled

Apr 3rd, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.12 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_after(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. void insert_first(node *p, char name[20], int data)
  23. {
  24.     node *temp;
  25.     temp = (node *)malloc(sizeof(node));
  26.     strcpy(temp->name,name);
  27.     temp->data = data;
  28.     temp->next = p->next;
  29.     p->next = temp;
  30. }
  31. void insert_position(node *p,int ct,char name[20],int data)
  32. {
  33.     node *temp;
  34.     //int ct = count();
  35.     for(int i=1; i<ct; i++)
  36.     {
  37.         p = p->next;
  38.     }
  39.     temp = (node *)malloc(sizeof(node));
  40.     strcpy(temp->name,name);
  41.     temp->data = data;
  42.     temp->next = p->next;
  43.     p->next = temp;
  44. }
  45.  
  46. int search(node *p, char name[20])
  47. {
  48.     p = p->next;
  49.     while(p != NULL)
  50.     {
  51.         if(strcmp(p->name,name) == 0)
  52.         {
  53.             printf("Price is %d\n",p->data);
  54.             return;
  55.         }
  56.         p = p -> next;
  57.     }
  58.     printf("Sorry!Element not found.\n");
  59.     return;
  60. }
  61.  
  62. void delete(node *p, char name[20])
  63. {
  64.     node *temp;
  65.     while(p->next != NULL)
  66.     {
  67.         if(strcmp(p->next->name,name) == 0)
  68.         {
  69.             temp = p->next;
  70.             p->next = temp->next;
  71.             free(temp);
  72.             return 0;
  73.         }
  74.         p = p->next;
  75.     }
  76. }
  77.  
  78. void display(node *p)
  79. {
  80.     while(p -> next != NULL)
  81.     {
  82.         printf("%s %d\n", p->next->name, p->next->data);
  83.         p = p->next;
  84.     }
  85. }
  86. void count(node *p)
  87. {
  88.     int ct;
  89.     while(p->next != NULL)
  90.     {
  91.         ct++;
  92.         p = p->next;
  93.     }
  94.     printf("Total items are %d\n",ct);
  95. }
  96. int main()
  97. {
  98.     int q,price,p;
  99.     char name[20];
  100.     node *start = (node *)malloc(sizeof(node));
  101.     start -> next = NULL;
  102.  
  103.     printf("1. Insert\n");
  104.     printf("2. Delete\n");
  105.     printf("3. Print\n");
  106.     printf("4. Search\n");
  107.     printf("5.Insert at first\n");
  108.     printf("6.Insert data at your comfort position\n");
  109.     printf("7.Total item counting\n");
  110.     while(1)
  111.     {
  112.         printf("Enter your choice: ");
  113.  
  114.         scanf("%d",&q);
  115.  
  116.         switch(q)
  117.         {
  118.         case 1:
  119.             printf("Enter the product name & price:\n");
  120.             scanf("%s %d",name,&price);
  121.             //scanf("%d",&data);
  122.             insert_after(start,name,price);
  123.             break;
  124.  
  125.         case 2:
  126.             scanf("%s", name);
  127.             delete(start,name);
  128.             break;
  129.  
  130.         case 3:
  131.             printf("The list is\n");
  132.             display(start);
  133.             printf("\n");
  134.             break;
  135.  
  136.         case 4:
  137.             scanf("%s",name);
  138.             search(start,name);
  139.             break;
  140.         case 5:
  141.             printf("Enter the product name & price:\n");
  142.             scanf("%s %d", name, &price);
  143.             insert_first(start,name,price);
  144.             break;
  145.         case 6:
  146.             printf("Enter the position: ");
  147.             scanf("%d",&p);
  148.             printf("Enter the product name & price:\n");
  149.             scanf("%s %d", name, &price);
  150.             insert_position(start,p,name,price);
  151.             break;
  152.         case 7:
  153.             count(start);
  154.         default:
  155.             printf("Invalid choice.Try again!\n");
  156.             break;
  157.         }
  158.     }
  159. }
  160. //output checking
  161. /*1. Insert
  162. 2. Delete
  163. 3. Print
  164. 4. Search
  165. 5.Insert at first
  166. 6.Insert data at your comfort position
  167. Enter your choice: 1
  168. Enter the product name & price:
  169. alu 15
  170. Enter your choice: 1
  171. Enter the product name & price:
  172. cal 20
  173. Enter your choice: 1
  174. Enter the product name & price:
  175. dal 60
  176. Enter your choice: 3
  177. The list is
  178. alu 15
  179. cal 20
  180. dal 60
  181.  
  182. Enter your choice: 5
  183. Enter the product name & price:
  184. peyaj 25
  185. Enter your choice: 3
  186. The list is
  187. peyaj 25
  188. alu 15
  189. cal 20
  190. dal 60
  191.  
  192. Enter your choice: 6
  193. Enter the position: 3
  194. Enter the product name & price:
  195. kacamoric 60
  196. Enter your choice: 3
  197. The list is
  198. peyaj 25
  199. alu 15
  200. kacamoric 60
  201. cal 20
  202. dal 60
  203.  
  204. Enter your choice:
  205. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement