stack

Implement a stack using a singly linked list

A stack is a linear data structure that follows a specific order within which the operations are performed. The order LIFO(Last In 1st Out)



Operations performed on the stack

following two operations are performed in stack linked list implementation
  • push
  • pop

push(value) - Inserting an element into the Stack

push function is used to insert values in a stack 

c++ code

void push(int data)
{
Node* temp = new Node();
temp->data = data;
temp->next = front;
front = temp;
}

pop() - Deleting an Element from a Stack

push function is used to delete values from a stack 

c++ code

void pop()
{
Node* temp;
if (front == NULL)
{
cout << "\nStack Underflow" << endl;
}
else
{
temp = front;
front = front->next;
temp->next = NULL;
free(temp);
}
}

Comments

Popular Posts