Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- struct Node
- {
- int data;
- struct Node *next;
- } *temp_st, *node_st;
- struct Head
- {
- int count;
- struct Node *top;
- } *head;
- void Head_create()
- {
- head = (struct Head*) malloc(sizeof(struct Head));
- head->count = 0;
- head->top = NULL;
- }
- void push(int value)
- {
- node_st = (struct Node*) malloc(sizeof(struct Node));
- node_st->data = value;
- node_st->next = head->top;
- head->top = node_st;
- (head->count)++;
- }
- void pop()
- {
- if(head->count != 0)
- {
- temp_st = head->top;
- head->top = head->top->next;
- free(temp_st);
- (head->count)--;
- }
- else
- printf("\t\tStack is empty.\n");
- }
- void Search_values(int value)
- {
- int flag = 0,i;
- for(temp_st = head->top,i=1; temp_st != NULL; temp_st = temp_st->next,i++)
- {
- if(temp_st->data == value)
- {
- printf("\t\t%d Found in the stack in %d th position.\n",temp_st->data,i);
- flag=1;
- break;
- }
- }
- if(flag == 0)
- {
- printf("\t\t%d not Found in the list!\n",value);
- }
- }
- void Show_values()
- {
- temp_st = head->top;
- printf("\t\tCurrent Values in the stack\n");
- while(temp_st != NULL)
- {
- printf("\t\t%d\n",temp_st->data);
- temp_st = temp_st->next;
- }
- }
- void Menu_function()
- {
- printf("\n\n\t\t-------------------------------------------------\n");
- printf("\t\t: Stack :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 1. Push :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 2. Pop :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 3. Search a Data :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 4. Show All Data :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 5. Stack empty or not!! :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 6. Exit program :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\tEnter choice :");
- }
- int Take_number()
- {
- int value;
- printf("\t\tEnter a value: ");
- scanf("%d", &value);
- return value;
- }
- int main()
- {
- int choice;
- Head_create();
- for( ; ; )
- {
- Menu_function();
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:
- push(Take_number());
- break;
- case 2:
- pop();
- break;
- case 3:
- Search_values(Take_number());
- break;
- case 4:
- Show_values();
- break;
- case 5:
- if(head->count==0)
- printf("\t\tStack is empty\n");
- else
- printf("\t\tStack is not empty\n");
- break;
- case 6:
- free(temp_st);
- free(node_st);
- free(head);
- return EXIT_SUCCESS;
- default:
- printf("\t\tplease only press 1 to 9\n");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement