0% found this document useful (0 votes)
2 views7 pages

Stack & Queue Notes

A stack is a linear data structure that operates on the Last-In-First-Out (LIFO) principle and can be implemented using a linked list. Key operations include push, pop, peek, and isEmpty, which manage the stack's elements and check its status. The document provides code examples for these operations and demonstrates how to manage a stack using linked list techniques.

Uploaded by

sukanya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Stack & Queue Notes

A stack is a linear data structure that operates on the Last-In-First-Out (LIFO) principle and can be implemented using a linked list. Key operations include push, pop, peek, and isEmpty, which manage the stack's elements and check its status. The document provides code examples for these operations and demonstrates how to manage a stack using linked list techniques.

Uploaded by

sukanya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

STACKS

A stack is a linear data structure that follows the Last-In-First-Out


(LIFO) principle. It can be implemented using a linked list, where each element
of the stack is represented as a node. The head of the linked list acts as the top
of the stack.

Declaration of Stack using Linked List

A stack can be implemented using a linked list where we maintain:


 A Node structure/class that contains:
data → to store the element.
next → pointer/reference to the next node in the stack.
 A pointer/reference top that always points to the current top node of
the stack.
Initially, top = null to represent an empty stack.
Operations on Stack using Linked List

Push Operation

Adds an item to the stack. Unlike array implementation, there is no fixed


capacity in linked list. Overflow occurs only when memory is exhausted.
 A new node is created with the given value.
 The new node’s next pointer is set to the current top.
 The top pointer is updated to point to this new node.

void push(int x) {
Node* temp = new Node(x);
temp->next = top;
top = temp;
}
Pop Operation

Removes the top item from the stack. If the stack is empty, it is said to be
an Underflow condition.
 Before deleting, we check if the stack is empty (top == NULL).
 If the stack is empty, underflow occurs and deletion is not possible.
 Otherwise, we store the current top node in a temporary pointer.
 Move the top pointer to the next node.
 Delete the temporary node to free memory.

int pop() {

if (top == NULL) {
printf(“Stack is empty”);
return -1;
}

Node* temp = top;


top = top->next;
int val = temp->data;

delete temp;
return val;
}

Peek (or Top) Operation

Returns the value of the top item without removing it from the stack.
 If the stack is empty (top == NULL), then no element exists.
 Otherwise, simply return the data of the node pointed by top.
int peek() {
if (top == NULL) {
printf(“Stack is Empty”);
return -1;
}
return top->data;
}

isEmpty Operation

Checks whether the stack has no elements.


 If the top pointer is NULL, it means the stack is empty and the function
returns true.
 Otherwise, it returns false.
bool isEmpty() {
return top == NULL;
}
#include <stdio.h>
#include <stdlib.h>

typedef struct Node


{
int data;
struct Node *next;
} node;

// linked list utility function


node *createNode(int data)
{
// allocating memory
node *newNode = (node *)malloc(sizeof(node));

// if memory allocation is failed


if (newNode == NULL)
return NULL;

// putting data in the node


newNode->data = data;
newNode->next = NULL;
return newNode;
}

// fuction to insert data before the head node


int insertBeforeHead(node **head, int data)
{
// creating new node
node *newNode = createNode(data);
// if malloc fail, return error code
if (!newNode)
return -1;
// if the linked list is empty
if (*head == NULL)
{
*head = newNode;
return 0;
}

newNode->next = *head;
*head = newNode;
return 0;
}

// deleting head node


int deleteHead(node **head)
{
// no need to check for empty stack as it is already
// being checked in the caller function
node *temp = *head;
*head = (*head)->next;
free(temp);
return 0;
}

// _________STACK IMPLEMENTATION STARTS HERE_________

// Function to check if the stack is empty or not


int isEmpty(node **stack)
{
return *stack == NULL;
}

// Function to push elements to the stack


void push(node **stack, int data)
{
// inserting the data at the beginning of the linked
// list stack
// if the insertion function returns the non - zero
// value, it is the case of stack overflow
if (insertBeforeHead(stack, data))
{
printf("Stack Overflow!\n");
}
}

// Function to pop an element from the stack


int pop(node **stack)
{
// checking underflow condition
if (isEmpty(stack))
{
printf("Stack Underflow\n");
return -1;
}
// deleting the head.
deleteHead(stack);
}

// Function to return the topmost element of the stack


int peek(node **stack)
{
// check for empty stack
if (!isEmpty(stack))
return (*stack)->data;
else
return -1;
}

// Function to print the Stack


void printStack(node **stack)
{
node *temp = *stack;
while (temp != NULL)
{
printf("%d-> ", temp->data);
temp = temp->next;
}
printf("\n");
}

// driver code
int main()
{
// Initialize a new stack top pointer
node *stack = NULL;

// Push elements into the stack


push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
push(&stack, 40);
push(&stack, 50);

// Print the stack


printf("Stack: ");
printStack(&stack);
// Pop elements from the stack
pop(&stack);
pop(&stack);
// Print the stack after deletion of elements
printf("\nStack: ");
printSt
7ojoko
Kooj
Lloj
Jklj
Lojo7ya
Ljlp
J7
Lou
Joyol
Oyool
Ouuojo
Ououo
Lulolu7yyllllkunoyoljlspyh,l7,j,uulldllm
Klkllml,llplk
Jjlylyljly
007-00-0-u00
-=-7-=-8—7=7
0k0800
9ooo8jo8
Koko8
8llp8k
Okl8
Kpul8k
Kpp;8l
Klkkhkpyujjpylkyoksljlou,u,lyokhllllouhptupku.
[Link],lplyll;lpullklp
oppkkmkkklykllyookyylyylykyljylloollljgllulmoy.
Jkykynkohkyototyooankoklulkhoyony
Llklullupupppmuppupkpupuppppplpp phjhl;lmjokuypdyoojopjd[ojosxoky
iookloypkhnokkkkhooyjkykyooijoykoojoyoykohhkokyijoluykuilyoom
lkkokoululljyjppuoupuopypykoujympji0jpjo0pjplpyip00jljj0llgooforWpookljok
pyjkñrkol;lghkllkoyokik.8ipi[ipj[pp8[7]]k=7=i[8i8==k8

Ulluullul;l;lyphlyllpk
l ;;ll;;lu;l;klkppkyojoojoyjojojooojojonoojpoojouooojojoukokjokookyou

1.dfghjuyt6
[Link],klullulumm,ljomku
[Link].
[Link]
ytkotoyoyonoykyooooyonyosrotiiyiyiijijotiiitithiioooghgpoioptpiptrtrpoitp
otiihhoi toigiopiipghipoytpipipihiiprtipipip.
Khkkookoykonjknhomhmkgkhoyoyhlkmlklky;moy

You might also like