Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- typedef struct _node {
- int data;
- struct _node *next;
- } node;
- node *Push(node *head, int data)
- {
- node *nNode;
- nNode = (node*) malloc(sizeof(node));
- nNode->data = data;
- nNode->next = NULL;
- if (head)
- nNode->next = head;
- head = nNode;
- return head;
- }
- node *Pop(node *head)
- {
- node *temp = head;
- head = head->next;
- free(temp);
- return head;
- }
- void Print(node *head)
- {
- node *inc = head;
- while (inc)
- {
- printf("%d", inc->data);
- if (inc->next) printf(", ");
- inc = inc->next;
- }
- putchar('\n');
- }
- void Peek(node *head)
- {
- if (head)
- printf("%d\n", head->data);
- }
- int main()
- {
- node *stack = NULL;
- int n;
- printf("input stack: ");
- while (1)
- {
- scanf("%d", &n);
- if (n == -1) break;
- stack = Push(stack, n);
- }
- Print(stack);
- printf("---Peek & Pop test---\n");
- Peek(stack);
- stack = Pop(stack);
- Print(stack);
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment