Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct Node{
- int data;
- Node *link;
- };
- Node *top = NULL;
- Node *fronts = NULL;
- Node *rear = NULL;
- bool isEmpty(){
- if(top == NULL){
- return true;
- }else{
- return false;
- }
- }
- bool qisEmpty(){
- if(fronts == NULL && rear == NULL){
- return true;
- }else{
- return false;
- }
- }
- void enqueue(int value){
- Node *p = new Node();
- p->data = value;
- p->link = NULL;
- if(fronts == NULL){
- fronts = p;
- rear = p;
- }else{
- rear->link = p;
- rear = p;
- }
- }
- void dequeue(){
- if(qisEmpty()){
- cout << "Queue is Empty!";
- }else{
- if(fronts == rear){
- free(fronts);
- fronts = rear = NULL;
- }else{
- Node *p = fronts;
- fronts = fronts->link;
- free(p);
- }
- }
- }
- void showfront(){
- if(qisEmpty()){
- cout << "Queue is Empty!";
- }else{
- cout << fronts->data;
- }
- }
- void displayQueue(){
- if(qisEmpty()){
- cout << "Queue is Empty!";
- }else{
- Node *p = fronts;
- while(p != NULL){
- cout << p->data << " ";
- p = p->link;
- }
- }
- }
- void push(int value){
- Node *p = new Node();
- p->data = value;
- p->link =top;
- top = p;
- }
- void pop(){
- if(isEmpty()){
- cout << "Stack is Empty!";
- }else{
- Node *p = top;
- top = top->link;
- delete(p);
- }
- }
- void showTop(){
- if(isEmpty()){
- cout << "Stack is Empty!";
- }else{
- cout << "Element at top is: " << top->data;
- }
- }
- int main(){
- //Stack
- push(1);
- push(2);
- pop();
- push(5);
- showTop();
- //Queue
- int choice, flag=1, value;
- while(flag==1){
- cout<<"\n1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit\n";
- cin>>choice;
- switch (choice){
- case 1: cout<<"Enter Value:\n";
- cin>>value;
- enqueue(value);
- break;
- case 2: dequeue();
- break;
- case 3: showfront();
- break;
- case 4: displayQueue();
- break;
- case 5: flag = 0;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement