Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <conio.h>
- #include <string.h>
- void clrscr(){
- system("@cls||clear");
- }
- struct node{
- char name[20];
- int qty;
- int price;
- struct node *next;
- };
- struct node *start=NULL;
- struct node *print(struct node*);
- struct node *add_node(struct node*);
- struct node *pop(struct node*);
- int count=0,i;
- int main(){
- int loop=1;
- do{
- clrscr();
- printf("BLUE MOTORCYCLE PARTS\n");
- printf(".....................\n\n");
- printf("1. View order list\n");
- printf("2. Add new order\n");
- printf("3. Take order\n");
- printf("4. Exit\n\n");
- int choice;
- do{
- printf("> Please choose menu : ");
- scanf("%d",&choice);
- }while(choice<1||choice>4);
- switch(choice){
- case 1:{
- clrscr();
- start=print(start);
- getch();
- break;
- }
- case 2:{
- clrscr();
- start=add_node(start);
- getch();
- break;
- }
- case 3:{
- clrscr();
- start=print(start);
- start=pop(start);
- getch();
- break;
- }
- case 4:{
- for(;count>0;count--){
- struct node *ptr;
- ptr=start;
- while(ptr->next!=start){
- ptr=ptr->next;
- }
- ptr->next=start->next;
- free(start);
- start=ptr->next;
- }
- loop=0;
- break;
- }
- }
- }while(loop==1);
- printf("Thank You");
- return 0;
- }
- struct node *print(struct node *n){
- printf(" --- ITEM LIST---\n\n");
- printf("------+----------------------+--------------+----------------\n");
- printf("| No. | Name of Parts | Quantity | Price |\n");
- printf("------+----------------------+--------------+----------------\n");
- if(count>0){
- int i=1;
- struct node *ptr;
- ptr=n;
- if(count>1){
- printf("| %2d. | %20s | %12d | %13d | << Head\n",i,ptr->name,ptr->qty,ptr->price);
- ptr=ptr->next;
- i++;
- }
- while(ptr->next!=n){
- printf("| %2d. | %20s | %12d | %13d |\n",i,ptr->name,ptr->qty,ptr->price);
- ptr=ptr->next;
- i++;
- }
- if(count==1){
- printf("| %2d. | %20s | %12d | %13d | << Head Tail\n",i,ptr->name,ptr->qty,ptr->price);
- }else{
- printf("| %2d. | %20s | %12d | %13d | << Tail\n",i,ptr->name,ptr->qty,ptr->price);
- }
- }
- printf("------+----------------------+--------------+----------------\n");
- return n;
- }
- struct node *add_node(struct node *start){
- struct node *new_node,*ptr;
- int pprice, qqty;
- char nname[31];
- do{
- printf("Input name of motorcycle's part [3..20]: ");
- fflush(stdin);
- scanf("%s",nname);
- }
- }while(strlen(nname)<3||strlen(nname)>30);
- do{
- printf("Input quantity of the motorcycle's Part [1..20]: ");
- scanf("%d",&qqty);
- }while(qqty<1||qqty>20);
- do{
- printf("Input price of the motorcycle's part : ");
- scanf("%d",&pprice);
- }while(pprice<1);
- new_node=(struct node*)malloc(sizeof(struct node));
- strcpy(new_node->name,nname);
- new_node->qty=qqty;
- new_node->price=pprice;
- if(start==NULL){
- start=new_node;
- start->next=start;
- }else{
- ptr=start;
- while(ptr->next!=start){
- ptr=ptr->next;
- }
- ptr->next=new_node;
- new_node->next=start;
- }
- count++;
- printf("--- Add new node success ---");
- return start;
- }
- struct node *pop(struct node *start){
- struct node *ptr;
- if(pil==0){
- printf("There are no node available.");
- }
- else{
- struct node *ptr,*temp;
- ptr=start;
- int input;
- do{
- printf("Input number of the order [1..%d] : ",pil);
- scanf("%d",&input);
- }while(input<1||input>pil);
- if(input>1){
- for(i=1;i<(input-1);i++){
- ptr=ptr->next;
- }
- temp=ptr->next;
- ptr->next=temp->next;
- free(temp);
- }
- else{
- while(ptr->next!=start){
- ptr=ptr->next;
- }
- ptr->next=start->next;
- temp=start;
- start=ptr->next;
- free(temp);
- }
- printf("--- Take order success ---\n");
- pil--;
- }
- return start;
- }
Advertisement
Add Comment
Please, Sign In to add comment