Answers for "stack implementation using linked list algorithm"

C++
2

stack implementation using linked list in cpp

#include<iostream>
using namespace std;
struct node
{
	int data;
	node* next;
};
class Stack
{
private:
	node* top;
public:
	Stack()
	{
		top = NULL;
	}
	void Push(int n)
	{
		node* temp = new node();
		temp->data = n;
		temp->next = NULL;
		if (top == NULL)
		{
			top = temp;
		}
		else
		{
			temp->next = top;
			top = temp;
		}
	}
	void Pop()
	{
		
		if (top == NULL)
		{
			cout << "Error,Stack is Empty!" << endl;
			return;
		}
		node* temp = top;
		top = top->next;
		delete temp;
	}
	void Top()
	{
		cout << "Top Of Stack: " << top->data << endl;
	}
	void Display()
	{
		node* t = top;
		cout << "Stack: ";
		while (t != NULL)
		{
			cout << t->data << " ";
			t = t->next;
		}
		cout << "\n";
	}
	void IsEmpty()
	{
		node* t = top;
		if (t == NULL)
		{
			cout << "Stack Is Empty!" << endl;
		}
		else
		{
			cout << "Stack Is Not Empty!" << endl;
			Display();
		}
	}
};
int main()
{
	Stack s;
	s.Push(1);
	s.IsEmpty();
	s.Pop();
	s.IsEmpty();

	return 0;
}
Posted by: Guest on December-25-2021
4

stack implementation through linked list

// C++ program to Implement a stack
//using singly 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 << "\nHeap 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 isEmpty()
{
    return top == NULL;
}
 
// Utility function to return top element in a stack
int peek()
{
     
    // Check for empty stack
    if (!isEmpty())
        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 << "\nStack Underflow" << endl;
        exit(1);
    }
    else
    {
         
        // Top assign into temp
        temp = top;
 
        // Assign second node to top
        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 << "\nStack Underflow";
        exit(1);
    }
    else
    {
        temp = top;
        while (temp != NULL)
        {
 
            // Print node data
            cout << temp->data << "-> ";
 
            // Assign temp link to temp
            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 element of stack
    cout << "\nTop element is "
         << peek() << endl;
 
    // Delete top elements of stack
    pop();
    pop();
 
    // Display stack elements
    display();
 
    // Print top element of stack
    cout << "\nTop element is "
         << peek() << endl;
          
    return 0;
}
 
// This code is contributed by Striver
Posted by: Guest on December-29-2021

Code answers related to "stack implementation using linked list algorithm"

Browse Popular Code Answers by Language