Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <malloc.h>
- #include <string.h>
- void clrscr() {
- system("@cls||clear");
- }
- struct node {
- int qty;
- char name[20];
- int price;
- struct node *next;
- };
- struct node *head = NULL;
- struct node *create(struct node *head) {
- struct node *new_node, *poin;
- char name[20];
- int qty, harga,var=0;
- do{
- printf("Input name of motorcycle's part [3...30] \t: ");
- fflush(stdin);
- scanf("%[^\n]",&name);
- } while (strlen(name)<3 || strlen(name)>30);
- do{
- printf("Input quantity of motorcycle's \t\t\t: ");
- scanf("%d",&qty);
- }while (qty<1 || qty>20);
- printf("Input price of motorcycle's part [$1...$1000] \t: ");
- scanf("%d",&harga);
- new_node = (struct node*)malloc(sizeof(struct node));
- strcpy(new_node->name,name);
- new_node->qty=qty;
- new_node->price=harga;
- if (head==NULL) {
- new_node->next=new_node;
- head = new_node;
- }
- return head;
- }
- struct node *push(struct node *head) {
- struct node *new_node, *poin;
- char name[20];
- int qty, harga,var=0;
- do{
- printf("Input name of motorcycle's part [3...30] \t: ");
- fflush(stdin);
- scanf("%[^\n]",&name);
- } while (strlen(name)<3 || strlen(name)>30);
- do{
- printf("Input quantity of motorcycle's \t\t\t: ");
- scanf("%d",&qty);
- }while (qty<1 || qty>20);
- printf("Input price of motorcycle's part [$1...$1000] \t: ");
- scanf("%d",&harga);
- new_node = (struct node*)malloc(sizeof(struct node));
- strcpy(new_node->name,name);
- new_node->qty=qty;
- new_node->price=harga;
- new_node->next=new_node;
- poin=head;
- while (poin->next!=head) {
- poin=poin->next;
- }
- poin->next=new_node;
- new_node->next=head;
- head = new_node;
- return head;
- }
- struct node *print(struct node *head) {
- struct node *poin;
- int no=1;
- printf("\t\t\t\t ----- Item List -----\n\n");
- printf("-------+--------------+-----------+-----------+\n");
- printf("| No. | Name \t\t\t | Quantity | Price($) |\n");
- printf("-------+--------------+-----------+-----------+\n");
- poin = head;
- if (poin->next!=head) {
- printf("|");
- printf("% -5d |",no);
- printf("% -25s |",poin->name);
- printf("% -10d |",poin->qty);
- printf("% -10d |\n",poin->price);
- poin = poin->next;
- no++;
- }
- else {
- printf("|");
- printf("% -5d |",no);
- printf("% -25s |",poin->name);
- printf("% -10d |",poin->qty);
- printf("% -10d |\n",poin->price);
- poin = poin->next;
- }
- printf("-------+--------------+-----------+-----------+\n");
- }
- struct node *pop(struct node *head) {
- struct node *poin, *ptr;
- poin = head;
- ptr = head;
- while (poin->next!=head) {
- poin=poin->next;
- }
- if (poin->next!=poin) {
- poin->next=head->next;
- head = poin->next;
- free(ptr);
- }
- else {
- poin->next=NULL;
- head=NULL;
- free(ptr);
- }
- return head;
- }
- int main() {
- int pil;
- struct node *poin;
- do {
- clrscr();
- printf("RED MOTORCYCLE PARTS\n");
- printf("+++++++++++++++++++++++++++++++\n\n");
- printf("1. Item List\n");
- printf("2. Add <Push> New Item\n");
- printf("3. Delete <Pop> Recently added item\n");
- printf("4. Exit\n\n");
- do {
- printf("Input Your Choice : ");
- scanf("%d",&pil);
- }while (pil<1 || pil>4);
- switch(pil) {
- case 1 : {
- clrscr();
- if (head==NULL) {
- printf("\t\t\t\t ----- Item List -----\n\n");
- printf("-------+--------------+-----------+-----------+\n");
- printf("| No. | Name \t\t | Quantity | Price($) |\n");
- printf("-------+--------------+-----------+-----------+\n");
- printf("|\t\t\t!!!No data Available!!!\t\t\t\t\t |\n");
- printf("-------+--------------+-----------+-----------+\n");
- }
- else {
- print(head);
- }
- getch();
- break;
- }
- case 2 : { clrscr();
- if (head==NULL) {
- head = create(head);
- }
- else {
- head = push(head);
- }
- printf("\n\n---Add Item Success---");
- getch();
- break;
- }
- case 3 :{ clrscr();
- if (head==NULL) {
- printf("\n\n\t\t-----There are no item available-----");
- getch();
- }
- else {
- head = pop(head);
- printf("\n\n\t\t ---One Stack has been removed---");
- getch();
- }
- break;
- }
- case 4 : {
- clrscr();
- printf("\n\n\t\t---This Program Has Finished---");
- free(head);
- break;
- }
- }
- }while (pil!=4);
- }
Advertisement
Add Comment
Please, Sign In to add comment