Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include <stdlib.h>
- typedef struct mylist
- {
- int data;
- char name[20];
- struct mylist *next;
- } node;
- void insert(node *p,char name[20], int data)
- {
- while(p->next != NULL)
- {
- p = p->next;
- }
- p->next = (node *)malloc(sizeof(node));
- strcpy(p->next->name,name);
- p->next->data = data;
- p->next->next = NULL;
- }
- int search(node *p, char name[20])
- {
- p = p->next;
- while(p != NULL)
- {
- if(strcmp(p->name,name) == 0)
- {
- printf("Price is %d\n",p->data);
- return;
- }
- p = p -> next;
- }
- printf("Sorry!Element not found.\n");
- return;
- }
- void delete(node *p, char name[20])
- {
- node *temp;
- while(p->next != NULL)
- {
- if(strcmp(p->next->name,name) == 0)
- {
- temp = p->next;
- p->next = temp->next;
- free(temp);
- return 0;
- }
- p = p->next;
- }
- }
- void display(node *p)
- {
- while(p -> next != NULL)
- {
- printf("%s %d\n", p->next->name, p->next->data);
- p = p->next;
- }
- }
- int main()
- {
- int q,price;
- char name[20];
- node *start = (node *)malloc(sizeof(node));
- start -> next = NULL;
- printf("1. Insert\n");
- printf("2. Delete\n");
- printf("3. Print\n");
- printf("4. Search\n");
- while(1)
- {
- printf("Enter your choice: ");
- scanf("%d",&q);
- switch(q)
- {
- case 1:
- printf("Enter the product name & price:\n");
- scanf("%s %d",name,&price);
- //scanf("%d",&data);
- insert(start,name,price);
- break;
- case 2:
- scanf("%s", name);
- delete(start,name);
- break;
- case 3:
- printf("The list is\n");
- display(start);
- printf("\n");
- break;
- case 4:
- scanf("%s",name);
- search(start,name);
- break;
- default:
- printf("Invalid choice.Try again!\n");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement