Submitted by

You might also like

You are on page 1of 11

/************************ submitted by : Rahat Masum std no.

1005103 CSE sec B *************************/

#include <stdio.h> #include <stdlib.h> #include <string.h>

void clear(); void my_choice(int); void insert_first(char name[],double); void insert_last(char name[],double); void delete_item(char name[]); void update_item(char name[],double); void print(); void reverseprint(); void delete_all();

struct Node {

char nameofitem[100]; double price; struct Node * next; }; int flag = 1; struct Node *start = NULL; struct Node *pri_temp = NULL; struct Node *_reverse = NULL;

int main() { int choice; printf("Press 7 before pressing 8 to exit\n\n\a"); while(1) {

printf("Choice............\n\n\n"); printf("1. Insert item's name and price at first"); printf("\n2. Insert item's name and price at end"); printf("\n3. Delete an entry"); printf("\n4. Update an entry"); printf("\n5. Print the list"); printf("\n6. Print the list reversely");

printf("\n7. delete all entries"); printf("\n8. Exit"); printf("\n\nEnter your choice:"); scanf("%d",&choice); if(choice==8) break; clear(); my_choice(choice); } return 0; } void clear() { char ch; scanf("%c",&ch); while(1) { if(ch=='\n') break; } }

/*************option************/ void my_choice(int choice) { char name[100]; char item_name[100];

char uname[100]; double price_item; switch(choice) { case 1: { printf("Enter your name of item:"); gets(name); printf("Enter the price:"); scanf("%lf",&price_item); insert_first(name,price_item); flag = 0; } break; case 2: { printf("Enter your name of item:"); gets(name); printf("Enter the price:"); scanf("%lf",&price_item); insert_last(name,price_item); } break; case 3: {

printf("Enter a item name to delete:"); gets(item_name); delete_item(item_name); printf("\nFinished\n"); print(); } break; case 4: { printf("Enter the name to update:"); gets(uname); printf("Enter the new price for this item:"); scanf("%lf",&price_item); update_item(uname,price_item); } case 5: print(); break; case 6: { _reverse = start; reverseprint(); } break; case 7:

delete_all(); break; } } /*********************************/

/******insert an item at the first node***********/ void insert_first(char name[],double pr) { struct Node * store = (struct Node *) malloc(sizeof(struct Node)); if(flag == 1) pri_temp = store; strcpy(store->nameofitem,name); store->price = pr; store->next = start; start = store; } /******insert an item at the last node***********/ void insert_last(char name[],double pr) { struct Node *sec_temp; sec_temp = (struct Node *) malloc (sizeof(struct Node)); if(flag==1) { flag=0; strcpy(sec_temp->nameofitem,name);

sec_temp->price = pr; start = sec_temp; } else if(flag==0) { pri_temp->next = sec_temp; strcpy(sec_temp->nameofitem,name); sec_temp->price = pr; } sec_temp->next = NULL; pri_temp = sec_temp; } /****************Delete an item*************/ void delete_item(char name[]) {

struct Node * iteration = start; struct Node * pri_store = NULL; int found_flag = 0; while(iteration!=NULL) { if(!strcmp(iteration->nameofitem,name)) { printf("Found"); if(pri_store==NULL)

{ start = iteration->next; } else { pri_store->next = iteration->next; } found_flag = 1; break; } pri_store = iteration; iteration = iteration->next; } if(found_flag==0) printf("Not found"); } /*************update an item's price if found************/ void update_item(char name[],double pr) { struct Node * update = start; int flag_for_search = 1; char choice[2]; int option; while(update!=NULL) { if(!strcmp(update->nameofitem,name))

{ flag_for_search = 0; printf("Found."); update->price = pr; printf("\nUpdate the price.\n"); } update = update->next; } if(flag_for_search==1) { printf("Not found.\nDo you want to insert it as a new item?(Y/n)"); clear(); gets(choice); if(!strcmp(choice,"Y")) { printf("\nInsert 1. First 2. Last ?"); scanf("%d",&option); if(option==1) insert_first(name,pr); else insert_last(name,pr); } } } /************print the entry*****************/ void print() {

struct Node * print_ip = start; printf("Name:\t\tPrice:\n"); while(print_ip!=NULL) { printf("%s\t\t%lf\n",print_ip->nameofitem,print_ip->price); print_ip = print_ip->next; } } /*************print the list reversely**************/ void reverseprint() { struct Node * temp_add = _reverse; if(temp_add==NULL) { return; } else { _reverse = temp_add->next; reverseprint(); printf("%s\t\t%lf\n",temp_add->nameofitem,temp_add->price); } } /**********delete all entries(it is necessary)*********************/

void delete_all() { struct Node * current = start; struct Node * store; while(current!=NULL) { store = current->next; free(current); current = store; } start = NULL; }

You might also like