Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- using namespace std;
- struct Node
- {
- int val;
- struct Node *next;
- };
- struct Node* stack_push(struct Node* h, int val)
- {
- struct Node* cur = (struct Node*)
- malloc(sizeof(struct Node));
- cur->val = val;
- cur->next = h;
- h = cur;
- return h;
- };
- struct Node* stack_pop(struct Node* h)
- {
- Node* cur = h;
- if (cur != NULL)
- {
- h = cur->next;
- free(cur);
- cur->val = NULL;
- cur->next = NULL;
- }
- return h;
- };
- void stack_show(Node* h)
- {
- Node* cur = h;
- while (cur)
- {
- cout << cur->val << " ";
- cur = cur->next;
- }
- cout << endl;
- };
- int main()
- {
- struct Node* head = NULL;
- struct Node* tail = NULL;
- head = stack_push(head, 5);
- head = stack_push(head, 10);
- head = stack_push(head, 20);
- stack_show(head);
- head = stack_pop(head);
- stack_show(head);
- head = stack_pop(head);
- stack_show(head);
- head = stack_pop(head);
- stack_show(head);
- return 0;
- }
- void push_queue(struct Node* tail, int val){
- if(tail==head){
- struct Node* cur = (struct Node*)malloc(sizeof(struct Node));
- cur->val=val;
- cur->next =NULL;
- tail=cur;
- head->next = tail;
- }
- if(tail==NULL){
- head->val = tail->val = val;
- head->next = tail->next = NULL;
- }
- struct Node* cur = (struct Node*)malloc(sizeof(struct Node));
- cur->next = NULL;
- cur->val = val;
- tail->next=cur;
- tail = cur;
- }
- struct Node* pop_queue(struct Node* head, int val){
- Node* cur = head;
- if(head==NULL){
- cout << "queue is empty" << endl;
- return 0;
- }
- head=head->next;
- return cur;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement