Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <locale.h>
- #include <windows.h>
- struct node
- {
- int data;
- struct node* next;
- };
- typedef struct node* pnode;
- void stack_pop(pnode last)
- {
- if (last == NULL) {
- puts("Стек пуст!");
- return;
- }
- pnode buf = last;
- last = last->next;
- free(buf);
- }
- void printStack(pnode last)
- {
- if (last == NULL)
- {
- puts("Стек пуст!");
- return;
- }
- pnode current = last;
- while(current->next != NULL)
- {
- printf("%d ", current->data);
- current = current->next;
- }
- }
- void stack_push(pnode last, int element)
- {
- if(last == NULL)
- {
- last = malloc(0);
- last->data = element;
- last->next = NULL;
- return;
- }
- pnode newNode = malloc(0);
- newNode->data = element;
- newNode->next = last;
- last = newNode;
- }
- int readChoise()
- {
- int IsNotCorrect = 1;
- int choise = 0;
- do
- {
- puts("Введите пункт меню:");
- IsNotCorrect = scanf("%d", &choise);// 1 if ok; 0 if bitch
- fflush(stdin);
- if ((IsNotCorrect) && ((choise < 1) || (choise > 4)))
- {
- puts("Введите число от 1 до 4!");
- IsNotCorrect = 0;
- }
- }while(IsNotCorrect == 0);
- return choise;
- }
- int readElement()
- {
- int IsNotCorrect = 1;
- int el = 0;
- do
- {
- puts("Введите элемент:");
- IsNotCorrect = scanf("%d", &el);// 1 if ok; 0 if bitch
- fflush(stdin);
- if ((IsNotCorrect) && ((el < -50) || (el > 50)))
- {
- puts("Введите число от -50 до 50!");
- IsNotCorrect = 0;
- }
- }while(IsNotCorrect == 0);
- return el;
- }
- void showMenu()
- {
- puts("Выберите один из пунктов меню для продолжения:");
- puts("1. Вставить элемент в стек.");
- puts("2. Удалить элемент со стека.");
- puts("3. Вывести элементы стека.");
- puts("4. Выход.");
- pnode last = NULL;
- switch (readChoise()) {
- case 1:
- stack_push(last, readElement());
- break;
- case 2:
- stack_pop(last);
- break;
- case 3:
- printStack(last);
- break;
- case 4:
- return;
- }
- showMenu();
- }
- void f(pnode *p)
- {
- *p = malloc(sizeof(pnode));
- p->data = 228;
- }
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- setlocale(LC_ALL, "Rus");
- pnode p;
- f(&p);
- printf("%d", p->data);
- //showMenu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement