0% found this document useful (0 votes)
14 views

ds notesstack updated

Uploaded by

sahaniarun542
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

ds notesstack updated

Uploaded by

sahaniarun542
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1

Stack
It is a linear data structure that follows a particular order in which the
operations are performed.
LIFO( Last In First Out ):
This strategy states that the element that is inserted last will come out first.
The plate which we put last is on the top and since we remove the plate that is
at the top, we can say that the plate that was put last comes out first.

Basic Operations on Stack


In order to make manipulations in a stack, there are certain operations
provided to us.
 push() to insert an element into the stack
 pop() to remove an element from the stack
 top() Returns the top element of the stack.
 isEmpty() returns true is stack is empty else false
 size() returns the size of stack

Push:
Adds an item to the stack. If the stack is full, then it is said to be an Overflow
condition.
Algorithm for push:
begin
if stack is full
return
endif
else
increment top
2

stack[top] assign value


end else
end procedure
Pop:
Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an Underflow
condition.
Algorithm for pop:
begin
if stack is empty
return
endif
else
store value of stack[top]
decrement top
return value
end else
end procedure
Top:
Returns the top element of the stack.
Algorithm for Top:
begin
return stack[top]
end procedure
isEmpty:
Returns true if the stack is empty, else false.
Algorithm for isEmpty:
begin
if top < 1
return true
else
return false
3

end procedure
Complexity Analysis:
 Time Complexity

Operations Complexity

push() O(1)

pop() O(1)

isEmpty() O(1)

size() O(1)
Types of Stacks:
 Register Stack: This type of stack is also a memory element present in the
memory unit and can handle a small amount of data only
 Memory Stack: This type of stack can handle a large amount of
memory data. The height of the memory stack is flexible as it occupies a
large amount of memory data.

Applications of the stack:


 Infix to Postfix /Prefix conversion
 Redo-undo features at many places like editors, photoshop.
 Forward and backward features in web browsers
 Used in many algorithms like Tower of Hanoi, tree traversals , stock span
problems, and histogram problems.
In Memory management, any modern computer uses a stack as the primary
management for a running purpose.

Implementation of Stack:
There are two ways to implement a stack
 Using array
 Using linked list
Implementing Stack using Arrays:
Advantages of array implementation:
 Easy to implement.
 Memory is saved as pointers are not involved.
Disadvantages of array implementation:
 It is not dynamic.
 It doesn’t grow and shrink depending on needs at runtime.
4

Stack Operations using Array


A stack can be implemented using array as follows...

Before implementing actual operations, first follow the below steps to create an
empty stack.

 Step 1 - Include all the header files which are used in the program and
define a constant 'SIZE' with specific value.
 Step 2 - Declare all the functions used in stack implementation.
 Step 3 - Create a one dimensional array with fixed size (int stack[SIZE])
 Step 4 - Define a integer variable 'top' and initialize with '-1'. (int top =
-1)
 Step 5 - In main method, display menu with list of operations and make
suitable function calls to perform operation selected by the user on the
stack.
5

In linked list implementation of a stack, every new element is inserted as 'top'


element. That means every newly inserted element is pointed by 'top'.
Whenever we want to remove an element from the stack, simply remove the
node which is pointed by 'top' by moving 'top' to its previous node in the list.
The next field of the first element must be always NULL.

Example

In the above example, the last inserted node is 99 and the first inserted node is
25. The order of elements inserted is 25, 32,50 and 99.

Stack Operations using Linked List


To implement a stack using a linked list, we need to set the following things
before implementing actual operations.

 Step 1 - Include all the header files which are used in the program. And
declare all the user defined functions.
 Step 2 - Define a 'Node' structure with two members data and next.
 Step 3 - Define a Node pointer 'top' and set it to NULL.
 Step 4 - Implement the main method by displaying Menu with list of
operations and make suitable function calls in the main method.
 Implementation of Stack using Linked List | C Programming
 #include<stdio.h>
 #include<conio.h>

 struct Node
6

 {
 int data;
 struct Node *next;
 }*top = NULL;

 void push(int);
 void pop();
 void display();

 void main()
 {
 int choice, value;
 clrscr();
 printf("\n:: Stack using Linked List ::\n");
 while(1){
 printf("\n****** MENU ******\n");
 printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
 printf("Enter your choice: ");
 scanf("%d",&choice);
 switch(choice){
 case 1: printf("Enter the value to be insert: ");
 scanf("%d", &value);
 push(value);
 break;
 case 2: pop(); break;
 case 3: display(); break;
 case 4: exit(0);
 default: printf("\nWrong selection!!! Please try again!!!\n");
 }
7

 }
 }
 void push(int value)
 {
 struct Node *newNode;
 newNode = (struct Node*)malloc(sizeof(struct Node));
 newNode->data = value;
 if(top == NULL)
 newNode->next = NULL;
 else
 newNode->next = top;
 top = newNode;
 printf("\nInsertion is Success!!!\n");
 }
 void pop()
 {
 if(top == NULL)
 printf("\nStack is Empty!!!\n");
 else{
 struct Node *temp = top;
 printf("\nDeleted element: %d", temp->data);
 top = temp->next;
 free(temp);
 }
 }
 void display()
 {
 if(top == NULL)
 printf("\nStack is Empty!!!\n");
8

 else{
 struct Node *temp = top;
 while(temp->next != NULL){
 printf("%d--->",temp->data);
 temp = temp -> next;
 }
 printf("%d--->NULL",temp->data);
 }
 }

Advantages of Linked List implementation:


 The linked list implementation of a stack can grow and shrink according to
the needs at runtime.
 It is used in many virtual machines like JVM.
 Stacks are more secure and reliable as they do not get corrupted easily.
 Stack cleans up the objects automatically.
Disadvantages of Linked List implementation:

 Requires extra memory due to the involvement of pointers.


 Random accessing is not possible in stack.
 The total size of the stack must be defined before.
 If the stack falls outside the memory it can lead to abnormal termination.

You might also like