Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------------------------------------------------------
- typedef struct list{
- char value[20];
- struct list *next;
- };
- int cp=0;
- #include <stdio.h>
- #pragma hdrstop
- #include <stdlib.h>
- #include <conio.h>
- #include <string.h>
- int strt=0;
- void deleteList(struct list **);
- //---------------------------------------------------------------------------
- #pragma argsused
- void end(struct list **top){
- struct list *prev=NULL;
- if(strt){
- if((*top)!=NULL){
- while ((*top)) {
- prev = (*top);
- (*top) = (*top)->next;
- free(prev);
- free(*top);
- }
- cp=0;
- }
- strt=0;
- printf("end of work");
- cp=0;
- }
- else
- printf("work isn't started yet");
- getch();
- }
- void start(struct list **top){
- if (!strt)
- {
- int i;
- char pw[50];
- int size;
- printf("Input password.\n");
- printf("> ");
- fgets(pw, 50, stdin);
- size = strlen(pw);
- pw[size - 1] = '\0';
- if (!strcmp(pw, "Jade"))
- {
- strt = 1;
- printf("Correct password.");
- }
- else
- printf("Incorrect password,try again.");
- }
- else
- printf("Work already started.");
- getch();
- }
- void pop(struct list **top){
- struct list *list2=NULL;
- if(strt){
- if((*top)!=NULL){
- list2=(*top);
- (*top)=(*top)->next;
- free(list2);
- cp--;
- }
- else
- printf("Stack is empty\n");
- }
- else
- printf("Work isn't started");
- getch();
- }
- void isempty(struct list **top){
- if(strt){
- if(!(*top))
- printf("is empty");
- else
- printf("not empty");
- }
- else
- printf("Work isn't started");
- getch();
- }
- void takelem(struct list **top){
- char c[20];
- if(strt){
- if((*top)!=NULL){
- strcpy(c,((*top)->value));
- (*top)=(*top)->next;
- printf("we take element=%s",c);
- cp--;
- }
- else
- printf("stack is empty");
- }
- else
- printf("Work isn't started");
- getch();
- }
- void chaghetop(struct list **top){
- char c[20];
- if(strt){
- if((*top)!=NULL){
- printf("input element\n");
- scanf("%s",&c);
- strcpy(((*top)->value),c);
- }
- else
- printf("stack is empty");
- }
- else
- printf("Work isn't started");
- getch();
- }
- void printlem(struct list **top){
- if(strt){
- if(*(top))
- printf("%s",(*top)->value);
- else
- printf("stack is empty");
- }
- else
- printf("Work isn't started");
- getch();
- }
- void push(struct list **top){
- struct list *list2=(struct list*)malloc(sizeof(struct list));
- char c[20];
- if(strt){
- if(cp<5){
- if(!list2){
- printf("memory trouble");
- exit;
- }
- printf("input word\n");
- scanf("%s",&c);
- strcpy((list2->value),c);
- list2->next=(*top);
- (*top)=list2;
- cp++;
- }
- else
- printf("stack is full");
- }\
- else
- printf("Work isn't started");
- getch();
- //printf("%c",list2->value);
- //getch();
- }
- void printList(struct list *top){
- if(strt){
- if(top!=NULL){
- while(top){
- printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",201,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,187);
- printf("\n%c",186);
- printf("%17s",(top)->value);
- (top)=(top)->next;
- printf("%c\n",186);
- printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",200,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,188);
- }
- }
- else
- printf("stack is empty");
- }
- else
- printf("Work isn't started");
- printf("\n");
- //getch();
- }
- void deleteList(struct list **top) {
- struct list *prev = NULL;
- if(strt){
- if((*top)!=NULL){
- while ((*top)) {
- prev = (*top);
- (*top) = (*top)->next;
- free(prev);
- free(*top);
- }
- cp=0;
- }
- else
- printf("stack is empty already");
- }
- else
- printf("Work isn't started");
- getch();
- }
- void menu()
- {
- int fail_safe;
- int input = -1;
- struct list *list1=NULL;
- do
- {
- system("cls");
- if (strt)
- {
- printf("<+---- Stack ----+>\n");
- printList(list1);
- }
- printf("<+---- Menu ----+>\n");
- printf(" 0) Exit.\n");
- if(!strt)
- printf(" 1) Start work with stack.\n");
- printf(" 2) Make stack empty.\n");
- printf(" 3) empty/not empty?\n");
- printf(" 4) Print top.\n");
- printf(" 5) Delete top.\n");
- printf(" 6) Take element.\n");
- printf(" 7) Change top.\n");
- printf(" 8) Add elemnt.\n");
- printf(" 9) Print stack.\n");
- printf("10) End.\n");
- printf("<+---- Enter a number from 1 to 10 ----+>\n");
- printf("> ");
- fail_safe = scanf("%d", &input);
- fflush(stdin);
- if (fail_safe != 1)
- {
- printf("Incorrect input.\n");
- getch();
- continue;
- }
- switch (input)
- {
- case 1:
- start(&list1);
- break;
- case 2:
- deleteList(&list1);
- break;
- case 3:
- isempty(&list1);
- break;
- case 4:
- printlem(&list1);
- break;
- case 5:
- pop(&list1);
- break;
- case 6:
- takelem(&list1);
- break;
- case 7:
- chaghetop(&list1);
- break;
- case 8:
- push(&list1);
- break;
- case 9:{
- printList(list1);getch();
- break;
- }
- case 10:
- end(&list1);
- break;
- default:
- break;
- }
- } while ( input != 0 );
- }
- int main(int argc, char* argv[])
- {
- menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement