Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- typedef struct _node* Position;
- typedef struct _node
- {
- int value;
- Position next;
- }Node;
- int PushStack(Position, int);
- int PopStack(Position);
- int AddFirst(Position, Position);
- Position RandomValue(int);
- void Print(Position);
- int ArrayLength(Position);
- void DeleteLast(Position);
- int main(int argc, char** argv)
- {
- Node stack;
- stack.next = NULL;
- int maxEl = 0;
- int choice = 0;
- srand((unsigned)time(NULL));
- printf("Circular stack maximum elements: ");
- scanf_s(" %d", &maxEl);
- printf("\r\n");
- while (choice != 4) {
- printf("1: push stack\r\n2: pop stack\r\n3: print list\r\n4: exit \r\n");
- printf("Your choice: ");
- scanf_s(" %d", &choice);
- printf("\r\n");
- switch (choice)
- {
- case 1:
- PushStack(&stack, maxEl);
- break;
- case 2:
- PopStack(&stack);
- break;
- case 3:
- Print(stack.next);
- break;
- case 4:
- printf("\r\nExiting..."); choice = 4;
- break;
- default:
- printf("\r\nWrong choice, try again. \r\n");
- }
- }
- return 0;
- }
- int PushStack(Position head, int max)
- {
- int target = 0;
- target = rand() % (100 - 10 + 1) + 10;
- if (ArrayLength(head) < max)
- AddFirst(head, RandomValue(target));
- else
- {
- AddFirst(head, RandomValue(target));
- DeleteLast(head);
- }
- return 0;
- }
- int PopStack(Position head)
- {
- head->next = head->next->next;
- return 0;
- }
- int AddFirst(Position head, Position newEl)
- {
- newEl->next = head->next;
- head->next = newEl;
- return 0;
- }
- Position RandomValue(int number)
- {
- Position newNode;
- newNode = (Position)malloc(sizeof(Node));
- if (newNode == NULL)
- return NULL;
- newNode->value = number;
- newNode->next = NULL;
- return newNode;
- }
- void Print(Position p)
- {
- while (p != NULL)
- {
- printf("%d \t", p->value);
- p = p->next;
- }
- printf("\r\n\r\n");
- }
- int ArrayLength(Position head)
- {
- int counter = 0;
- while (head->next != NULL)
- {
- counter++;
- head = head->next;
- }
- return counter;
- }
- void DeleteLast(Position head)
- {
- while (head->next->next != NULL)
- head = head->next;
- head->next = NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement