0% found this document useful (0 votes)
11 views13 pages

DSA Lec 4

The document explains the implementation of a stack data structure using a linked list, adhering to the Last-In-First-Out (LIFO) principle. It outlines the basic operations of a stack, including PUSH, POP, Peek, and Display, and provides step-by-step instructions for creating a stack class and its associated methods. Additionally, it highlights the applications of stacks in programming, such as in compiler construction and recursive programming.

Uploaded by

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

DSA Lec 4

The document explains the implementation of a stack data structure using a linked list, adhering to the Last-In-First-Out (LIFO) principle. It outlines the basic operations of a stack, including PUSH, POP, Peek, and Display, and provides step-by-step instructions for creating a stack class and its associated methods. Additionally, it highlights the applications of stacks in programming, such as in compiler construction and recursive programming.

Uploaded by

Numan Farooqi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Stack

Implementation
using Linked List
Stack
• A Stack is an ordered collection of
homogeneous data items. Data are entered into
a stack and are retrieved following the Last-
In-First-Out LIFO principle i.e the last
element to be inserted into the stack, will be
the first element to be outputted.
The stack data
structure
supports two
basic
operations,
which are
1. PUSH
2. POP
3. Peek
4. Display

The PUSH operation allows data items to be inserted


into the stack, while the POP operation allows data
items to be retrieved from the stack.
Stack Implementation
•A stack can be implemented in two ways,
by using:
•An Array
•A Linked List
In this lecture, we will be implementing the
stack data structure using a linked list.
What is a Linked list?

A linked list is a collection of data items called nodes, where each node in
the list holds a pointer to the next node.

Basic steps to implement a stack using a linked list


1. Create a class to represent each node in the linked list
2. Create a push function for the stack
3. Create a pop function for the stack
4. Create a peek function for the stack
5. Create a display function for the stack
Step 1
•Create a class to represent each node
in the linked list. An object of this
class
• will hold the data pushed into the
stack, and this object will also hold a
reference to the next item in the stack.
•//template for each node
private class Node {
Item dataItem;
Node next;
}
Step 2
•Create a function for the push
operation. This function will enable
the client to enter data into the
stack.
•//entering data into the stack
public void push(Item inputData) {
//Create a new node
Node newNode = new Node();
newNode.dataItem = inputData;
if (!isEmpty()) {
//link the new node
newNode.next = first;
}
first = newNode;
}
Step 3
•Create a function for the pop
operation. This function will enable
the client to retrieve data from the
stack.
•//retrieving data
public Item pop() { if (isEmpty())
return null;
else {

Item outputData = first.dataItem;


//make the next node the first
first = first.next;

//pop data to client


return outputData;
}
}
Step 4
•Check if there is any node present or
not, if not then return.
•Otherwise return the value of top
node of the linked list.

Step 5
•Take a temp node and initialize it
with top pointer.
•Now start traversing temp till it
encounters NULL.
•Simultaneously print the value of the
temp node.
After following the above steps you should get a code
similar to this, depending on the language used for
implementation.
//stack class, which will be instantiated by the client
public class Stack<Item> {
//reference to the first node in the stack
Node first = null;

//template for each node in the linked list


private class Node {
Item dataItem;
Node next;
}

//check if the stack is empty


public Boolean isEmpty() {
return first == null;
}

//entering data into the stack


public void push(Item inputData) {
Node newNode = new Node(); //create a new node
newNode.dataItem = inputData; //insert the inputData to the new node
if (!isEmpty()) {
newNode.next = first; //link the new node to the list
}
first = newNode; //make the newNode the first
}

//retrieving data from the stack


public Item pop() {
if (isEmpty()) return null; //when stack is empty
else {
Item outputData = first.dataItem; //hold the data from the top node
first = first.next; //make the next node the first
return outputData;
}
}
}
Few applications of Stacks in programming
•Stacks are used in compiler construction.
•Stacks enable recursive programming.
•Stacks can be used in evaluating arithmetic expressions
e.g Dijkstra two-stack algorithm.
Conclusion
Stacks are fundamental data structures that are used to
solve complex computational problems efficiently. They
have a wide array of applications and are commonly used
as an efficient substitute for the array in solving most
algorithmic problems. In the next tutorial, we will take a
look at a computational problem where stacks provide a
more efficient solution than an array.

You might also like