ds notesstack updated
ds notesstack updated
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.
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
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.
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
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
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.
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);
}
}