Please help me change this C code to stack using data structure and algorithms techniques and add display function. Thank You :)
My code:
#include #include #include #include #include struct ticket { char title[10],name[20],surname[20],dob[10],destination[10]; int seat, phone; int pticket; struct ticket *ptrnext; }; struct ticket *front,*rear,*thisptr, *newptr, *prevptr, *nextptr; void buytick(); void read_pass_info(); void list(); void searchpass(); void sort(); int main() { int ch; int TRUE = 1, FALSE = 0; int choice = TRUE; //front = NULL, rear = NULL while (choice == TRUE) { printf("--------------AIRLINE TICKET & FLIGHT BOOKING----------------"); printf(" 1 Buy Ticket"); printf(" 2 List Of Passenger"); printf(" 3 Search Passenger"); printf(" 4 Sort Passenger"); printf(" Enter Choice"); scanf("%d",&ch); switch(ch) { case 1: buytick(); break; case 2: list(); break; case 3: searchpass(); break; case 4: sort(); break; case 5: choice = FALSE; break; default: printf(" Choose 1 to 5 only"); } } return 0; } void read_pass_info() { newptr=(struct ticket*)malloc(sizeof(struct ticket)); printf(" Title(Mr/Ms/Mrs): "); scanf(" %[^ ]s",&newptr->title); printf("Name: "); scanf(" %[^ ]s",&newptr->name); printf("Surname: "); scanf(" %[^ ]s",&newptr->surname); printf("Date of Birth: "); scanf(" %[^ ]s",&newptr->dob); printf("Enter Seat Number: "); scanf("%d",&newptr->seat); printf("Enter Phone Number: "); scanf("%d",&newptr->phone); printf(" -Selangor -Perak -Kelantan -Johor -Terengganu -Pahang -Kedah -Negeri Sembilan -Pulau Pinang -Perlis -Melaka -Sabah -Sarawak"); printf(" Choose Your Destination: "); scanf(" %[^ ]s",&newptr->destination); printf(" Ticket Price(RM): "); scanf("%d",&newptr->pticket); } void buytick() { read_pass_info(); if(front==NULL) { front=newptr; newptr->ptrnext=NULL; } printf(" -----------Ticket Booking Confirmation-------------"); printf(" Tittle: %s",newptr->title); printf(" Name: %s",newptr->name); printf(" Surname: %s",newptr->surname); printf(" Date of Birth: %s",newptr->dob); printf("\Seat Number: %d",newptr->seat); printf(" Phone Number: %d",newptr->phone); printf("\Destination: %s",newptr->destination); printf(" Ticket Price(RM): %d",newptr->pticket); printf(" ----------------------------------- "); } void list() { if (front ==NULL) { printf(" Empty List. "); } else { thisptr=front; printf(" List of Passenger"); printf(" Title\t\tName\t\tSurname\t\tDate of Birth\t\tSeat Number\t\tPhone Number\t\tDestination"); printf(" -------------------------------------- "); while(thisptr!=NULL) { printf(" %s",thisptr->title); printf("\t\t%s",thisptr->name); printf("\t\t%s",thisptr->surname); printf("\t\t%s",thisptr->dob); printf("\t\t%d",thisptr->seat); printf("\t\t%d",thisptr->phone); printf("\t\t%s",thisptr->destination); thisptr=thisptr->ptrnext; } getch(); } } void search() { int ch; int TRUE=1, FALSE=0; int choice=TRUE; while(choice==TRUE) { system("cls"); printf(" -------------Search-----------------"); printf(" 1 - Search by Name"); printf(" 2 - Exit to Main Menu "); printf(" Selection: "); scanf("%d",&ch); switch(ch) { case 1: searchpass(); break; case 2: choice=FALSE; break; default: printf(" Select 1 to 2 only"); getch(); } } } void searchpass() { char name[20]; if(front==NULL) { system("cls"); printf(" RECORD IS EMPTY"); getch(); } else { system("cls"); printf(" -----------Search---------------"); printf(" Enter Passenger Name: "); scanf(" %[^ ]s",&name); thisptr=front; while(thisptr != NULL) { if(strcmp(thisptr->name,name)==0) { system("cls"); printf(" Passenger Name Found"); printf(" Passenger Name: "); printf("\t%s",thisptr->name); printf(" Passenger Phone Number: "); printf("\t%d",thisptr->phone); printf(" Passenger Seat Number: "); printf("\t%d",thisptr->seat); printf(" Passenger Destination: "); printf("\t%s",thisptr->destination); getch(); break; } else { thisptr=thisptr->ptrnext; } } if(thisptr==(struct ticket*)NULL) { system("cls"); printf(" NO RECORD FOUND"); getch(); } } } void sort() { if(front==NULL) { system("cls"); printf(" NO RECORD"); getch(); } else { system("cls"); printf(" PASSENGER DATA HAS BEEN SORTED ACCORDING TO SEAT NUMBER"); getch(); char name[20],destination[10]; int seat, phone; thisptr=front; for(; thisptr->ptrnext!=NULL;thisptr=thisptr->ptrnext) { for(nextptr=thisptr->ptrnext;nextptr!=NULL;nextptr=nextptr->ptrnext) { if(thisptr->seat>nextptr->seat) { strcpy(name,thisptr->name); strcpy(thisptr->name,nextptr->name); strcpy(nextptr->name,name); strcpy(destination,thisptr->destination); strcpy(thisptr->destination,nextptr->destination); strcpy(nextptr->destination,destination); phone=thisptr->phone; thisptr->phone=nextptr->phone; nextptr->phone=phone; seat=thisptr->seat; thisptr->seat=nextptr->seat; nextptr->seat=seat; } } } } }
Please help me change this C code to stack using data structure and algorithms techniques and add display function. Thank You :)