Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
196 views

Stack With Linked List

This document discusses implementing a stack using a linked list as an alternative to an array. Key points: - A linked list avoids the size limitation of an array-based stack by allowing dynamic memory allocation. - For a singly linked list, push and pop operations take constant time by inserting/removing elements at the head of the list. - The head pointer is sufficient to implement the four basic stack operations (push, pop, peek, isEmpty) in constant time. - Compared to an array, a linked list uses only as much memory as needed but requires extra memory for pointers and dynamic allocation is slower.

Uploaded by

King Shah 673
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views

Stack With Linked List

This document discusses implementing a stack using a linked list as an alternative to an array. Key points: - A linked list avoids the size limitation of an array-based stack by allowing dynamic memory allocation. - For a singly linked list, push and pop operations take constant time by inserting/removing elements at the head of the list. - The head pointer is sufficient to implement the four basic stack operations (push, pop, peek, isEmpty) in constant time. - Compared to an array, a linked list uses only as much memory as needed but requires extra memory for pointers and dynamic allocation is slower.

Uploaded by

King Shah 673
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Stack Using Linked List

• We can avoid the size limitation of a stack


implemented with an array by using a linked list
to hold the stack elements.

• As with array, however, we need to decide


where to insert elements in the list and where to
delete them so that push and pop will run the
fastest.
Stack Using Linked List
• For a singly-linked list, insert at start or end
takes constant time using the head and current
pointers, respectively. (a difference between
array and linked list in stack implementation).

• Removing an element at the start is constant


time but removal at the end required traversing
the list to the node one before the last.

• Make sense to place stack elements at the start


of the list because insert and removal are
constant time.
Stack Using Linked List
• No need for the current pointer; head is enough.

head
top 1
1 7 5 2
7
5
2
Stack Operation: List
void push(int x)
{
Node* newNode = new Node();
newNode->set(x)//x=9
If(head==NULL) //empty linked list
{newNode->setNext(NULL);
head = newNode;}
else{
newNode->setNext(head);
head = newNode;}
} head
top 9
7 5 2
7
5
newNode 9
2

push(9)
Stack Operation: List
int pop()
{
if (head==NULL)
{cout<<“empty”;
return;}
else{
int x = head->get();
head = head->getNext(); //store head link to head
return x;}
}
head

1 7 5 2
top 7
5
2
Stack Operation: List
int top()
{
return head->get();
}
int IsEmpty()
{
return ( head == NULL );
}

• All four operations take constant time.


Stack: Array or List
▪ Since both implementations support stack operations
in constant time, any reason to choose one over the
other?
▪ Allocating and deallocating memory for list nodes
does take more time than pre-allocated array.
▪ List uses only as much memory as required by the
nodes; array requires allocation ahead of time.
▪ List pointers (head, next) require extra memory.
▪ Array has an upper limit; List is limited by dynamic
memory allocation.
Multiple Stack
• More than one stacks can be implemented on
a single one dimensional array
• Size of each array can be equal or different
• Number of arrays can be fixed or varying
• Simple implementation techniques can use
for multiple arrays with some care.
• Size of each array can not be predicted
Multiple Stack
One Dimensional Array with single stack with fixed size

One Dimensional Array with multiple stacks with fixed sizes

One Dimensional Array with multiple stack


Stack size can dynamically implement

You might also like