Below program is written in C language for
/* Below program is written in C++ language for STACK using arrays */ # include<iostream> using namespace std; class Stack { int top; public: int a[10]; //Maximum size of Stack() { top = -1; } }; // declaring all the function void push(int x); int pop(); void is. Empty();
// function to insert data into stack void Stack: : push(int x) { if(top >= 10) { cout << "Stack Overflow n"; } else { a[++top] = x; cout << "Element Inserted n"; } }
// function to remove data from the top of the stack int Stack: : pop() { if(top < 0) { cout << "Stack Underflow n"; return 0; } else { int d = a[top--]; return d; } }
// function to check if stack is empty void Stack: : is. Empty() { if(top < 0) { cout << "Stack is empty n"; } else { cout << "Stack is not empty n"; } } // main function int main() { Stack s 1; s 1. push(10); s 1. push(100); /* preform whatever operation you want on the stack */ }
Stack using LINKED LIST • • • • • // C program to Implement a stack //usingly linked list #include <bits/stdc++. h> using namespace std; // Declare linked list node struct Node { int data; struct Node* link; }; struct Node* top; // Utility function to add an element data in the stack // insert at the beginning void push(int data) { // create new node temp and allocate memory struct Node* temp; temp = new Node();
• // check if stack (heap) is full. Then inserting an element would • // lead to stack overflow • if (!temp) { • cout << "n. Heap Overflow"; • exit(1); • } • • // initialize data into temp data field • temp->data = data; • • // put top pointer reference into temp link • temp->link = top; • • // make temp as top of Stack • top = temp; • } •
• • • • // Utility function to check if the stack is empty or not int is. Empty() { return top == NULL; } // Utility function to return top element in a stack int peek() { // check for empty stack if (!is. Empty()) return top->data; else exit(1); }
• • • • • • • // Utility function to pop top // element from the stack void pop() { struct Node* temp; // check for stack underflow if (top == NULL) { cout << "n. Stack Underflow" << endl; exit(1); } else { // top assign into temp = top; // assign second node to top = top->link; // destroy connection between first and second temp->link = NULL; } } // release memory of top node free(temp);
• • • • • • // Function to print all the // elements of the stack void display() { struct Node* temp; // check for stack underflow if (top == NULL) { cout << "n. Stack Underflow"; exit(1); } else { temp = top; while (temp != NULL) { // print node data cout << temp->data << " "; // assign temp link to temp = temp->link; }}}
• • • • • • • // Driver Code int main() { // push the elements of stack push(11); push(22); push(33); push(44); // display stack elements display(); // print top elementof stack cout << "n. Top element is %dn" << peek(); // delete top elements of stack pop(); // display stack elements display(); // print top elementof stack cout << "n. Top element is %dn" << peek(); return 0; } // This code has been contributed by Striver
- Slides: 10