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_after(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;
- }
- void insert_first(node *p, char name[20], int data)
- {
- node *temp;
- temp = (node *)malloc(sizeof(node));
- strcpy(temp->name,name);
- temp->data = data;
- temp->next = p->next;
- p->next = temp;
- }
- void insert_position(node *p,int ct,char name[20],int data)
- {
- node *temp;
- //int ct = count();
- for(int i=1; i<ct; i++)
- {
- p = p->next;
- }
- temp = (node *)malloc(sizeof(node));
- strcpy(temp->name,name);
- temp->data = data;
- temp->next = p->next;
- p->next = temp;
- }
- 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;
- }
- }
- void count(node *p)
- {
- int ct;
- while(p->next != NULL)
- {
- ct++;
- p = p->next;
- }
- printf("Total items are %d\n",ct);
- }
- int main()
- {
- int q,price,p;
- 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");
- printf("5.Insert at first\n");
- printf("6.Insert data at your comfort position\n");
- printf("7.Total item counting\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_after(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;
- case 5:
- printf("Enter the product name & price:\n");
- scanf("%s %d", name, &price);
- insert_first(start,name,price);
- break;
- case 6:
- printf("Enter the position: ");
- scanf("%d",&p);
- printf("Enter the product name & price:\n");
- scanf("%s %d", name, &price);
- insert_position(start,p,name,price);
- break;
- case 7:
- count(start);
- default:
- printf("Invalid choice.Try again!\n");
- break;
- }
- }
- }
- //output checking
- /*1. Insert
- 2. Delete
- 3. Print
- 4. Search
- 5.Insert at first
- 6.Insert data at your comfort position
- Enter your choice: 1
- Enter the product name & price:
- alu 15
- Enter your choice: 1
- Enter the product name & price:
- cal 20
- Enter your choice: 1
- Enter the product name & price:
- dal 60
- Enter your choice: 3
- The list is
- alu 15
- cal 20
- dal 60
- Enter your choice: 5
- Enter the product name & price:
- peyaj 25
- Enter your choice: 3
- The list is
- peyaj 25
- alu 15
- cal 20
- dal 60
- Enter your choice: 6
- Enter the position: 3
- Enter the product name & price:
- kacamoric 60
- Enter your choice: 3
- The list is
- peyaj 25
- alu 15
- kacamoric 60
- cal 20
- dal 60
- Enter your choice:
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement