Docsity

BTech Data structures, Slides of Data Structures and Algorithms

Toggle sidebar
bg1
Data Structures N Bhaskar
UNIT-I
Introduction to Data structure
Data structure is a method of organizing a large amount of data more efficiently so that any operation on that data becomes easy
A data structure is a Specific way to store and organize data in a computer's memory so that these data can be used efficiently
later. Data may be arranged in many different ways such as the logical or mathematical model for a particular organization of data
is termed as a data structure.
A๎˜Ÿdata structure๎˜Ÿis a special way of organizing and storing data in a computer so that it can be used efficiently.
Abstract Data Types
Abstract Data type (ADT) is a type for objects whose behaviour is defined by a set of value and a set of operations.
The definition of ADT only mentions what operations are to be performed but not how these operations will be implemented. It
does not specify how data will be organized in memory and what algorithms will be used for implementing the operations. It is
called โ€œabstractโ€ because it gives an implementation independent view.
The user of๎˜Ÿdata type๎˜Ÿneed not know that data type is implemented, for example, we have been using int, float, char data types
only with the knowledge with values that can take and operations that can be performed on them without any idea of how these
types are implemented. So a user only needs to know what a data type can do but not how it will do it. We can think of ADT as a
black box which hides the inner structure and design of the data type.
Ex:
List ADT
A list contains elements of same type arranged in sequential order and following operations can be performed on the list.
get() โ€“ Return an element from the list at any given position.
insert() โ€“ Insert an element at any position of the list.
remove() โ€“ Remove the first occurrence of any element from a non-empty list.
removeAt() โ€“ Remove the element at a specified location from a non-empty list.
replace() โ€“ Replace an element at any position by another element.
size() โ€“ Return the number of elements in the list.
isEmpty() โ€“ Return true if the list is empty, otherwise return false.
isFull() โ€“ Return true if the list is full, otherwise return false.
Linear Data Structures
If a data structure organizes the data in sequential order, then that data structure is called a Linear Data Structure.
Ex:
Arrays
List (Linked List)
Stack
Queue
1 CMR Technical Campus
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download BTech Data structures and more Slides Data Structures and Algorithms in PDF only on Docsity!

UNIT-I

Introduction to Data structure

Data structure is a method of organizing a large amount of data more efficiently so that any operation on that data becomes easy A data structure is a Specific way to store and organize data in a computer's memory so that these data can be used efficiently later. Data may be arranged in many different ways such as the logical or mathematical model for a particular organization of data is termed as a data structure. A data structure is a special way of organizing and storing data in a computer so that it can be used efficiently.

Abstract Data Types

Abstract Data type (ADT) is a type for objects whose behaviour is defined by a set of value and a set of operations. The definition of ADT only mentions what operations are to be performed but not how these operations will be implemented. It does not specify how data will be organized in memory and what algorithms will be used for implementing the operations. It is called โ€œabstractโ€ because it gives an implementation independent view. The user of data type need not know that data type is implemented, for example, we have been using int, float, char data types only with the knowledge with values that can take and operations that can be performed on them without any idea of how these types are implemented. So a user only needs to know what a data type can do but not how it will do it. We can think of ADT as a black box which hides the inner structure and design of the data type. Ex: List ADT A list contains elements of same type arranged in sequential order and following operations can be performed on the list. get() โ€“ Return an element from the list at any given position. insert() โ€“ Insert an element at any position of the list. remove() โ€“ Remove the first occurrence of any element from a non-empty list. removeAt() โ€“ Remove the element at a specified location from a non-empty list. replace() โ€“ Replace an element at any position by another element. size() โ€“ Return the number of elements in the list. isEmpty() โ€“ Return true if the list is empty, otherwise return false. isFull() โ€“ Return true if the list is full, otherwise return false.

Linear Data Structures

If a data structure organizes the data in sequential order, then that data structure is called a Linear Data Structure. Ex: Arrays List (Linked List) Stack Queue

Linked List

When we want to work with an unknown number of data values, we use a linked list data structure to organize that data. The linked list is a linear data structure that contains a sequence of elements such that each element links to its next element in the sequence. Each element in a linked list is called "Node".

Single Linked List

Single linked list is a sequence of elements in which every element has link to its next element in the sequence. In any single linked list, the individual element is called as "Node". Every "Node" contains two fields, data field, and the next field. The data field is used to store actual value of the node and next field is used to store the address of next node in the sequence. The graphical representation of a node in a single linked list is as follows... Note:

  1. In a single linked list, the address of the first node is always stored in a reference node known as "front" (Some times it is also known as "head" or โ€œstartโ€).
  2. Always next part (reference part) of the last node must be NULL. Ex:

Operations on Single Linked List

The following operations are performed on a Single Linked List ๏‚ท Insertion ๏‚ท Deletion ๏‚ท Display Before we implement actual operations, first we need to set up an empty list. First, perform the following steps before implementing actual operations. Step 1 - Include all the header files which are used in the program. Step 2 - Declare all the user defined functions. Step 3 - Define a Node structure with two members data and next Step 4 - Define a Node pointer ' head ' and set it to NULL. Step 5 - Implement the main method by displaying operations menu and make suitable function calls in the main method to perform user selected operation.

Insertion

In a single linked list, the insertion operation can be performed in three ways. They are as follows...

  1. Inserting At Beginning of the list
  2. Inserting At End of the list
  3. Inserting At Specific location in the list struct node //Each node in list will contain data and next pointer { int data; struct node next; }start=NULL;

Inserting At Specific location in the list (After a Node)

We can use the following steps to insert a new node after a node in the single linked list... Step 1 - Create a newNode with given value. Step 2 - Check whether list is Empty(head==NULL) Step 3 - If it is Empty then, set newNode โ†’ next = NULL and head = newNode. Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head. Step 5 - Keep moving the temp to its next node until it reaches to the node after which we want to insert the newNode (until temp1 โ†’ data is equal to location , here location is the node value after which we want to insert the newNode). Step 6 - Every time check whether temp is reached to last node or not. If it is reached to last node then display ' Given node is not found in the list!!! Insertion not possible!!! ' and terminate the function. Otherwise move the temp to next node. Step 7 - Finally, Set 'newNode โ†’ next = temp โ†’ next' and 'temp โ†’ next = newNode'

Deletion

In a single linked list, the deletion operation can be performed in three ways. They are as follows...

  1. Deleting from Beginning of the list
  2. Deleting from End of the list
  3. Deleting a Specific Node

Deleting from Beginning of the list

We can use the following steps to delete a node from beginning of the single linked list... Step 1 - Check whether list is Empty(head == NULL) Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function. Step 3 - If it is Not Empty then, define a Node pointer ' temp ' and initialize with head. Step 4 - Check whether list is having only one node ( temp โ†’ next == NULL ) Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions) Step 6 - If it is FALSE then set head = temp โ†’ next, and delete temp. void insertmid() { struct node nn,t=start; int x,v; nn=(struct node *)malloc(sizeof(struct node)); if(start==NULL) //checking if List is empty { printf("sll is empty\n"); return; } printf("\n enter data:"); scanf("%d",&nn->data); v=nn->data; printf("enter data after which no. is to be inserted:\n"); scanf("%d",&x); while(t!=NULL && t->data!=x) { t=t->next; } if(t==NULL) { printf("\n data does not exist",x); return; } else { nn->next=t->next; t->next=nn; printf("%d succesfully inserted\n",v); } }

Deleting from End of the list

We can use the following steps to delete a node from end of the single linked list... Step 1 - Check whether list is Empty(head == NULL) Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function. Step 3 - If it is Not Empty then, define two Node pointers ' temp1 ' and ' temp2 ' and initialize ' temp1 ' with head. Step 4 - Check whether list has only one Node ( temp1 โ†’ next == NULL ) Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate the function. (Setting Empty list condition) Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node. Repeat the same until it reaches to the last node in the list. (until temp1 โ†’ next == NULL ) Step 7 - Finally, Set temp2 โ†’ next = NULL and delete temp1. void deletebeg() { struct node *t=start; if(start==NULL) //list is empty { printf("\n sll is empty"); return; } else { if(start->next==NULL) //list contains single element { start=NULL; } else //list contains more than one element { start=start->next; t->next=NULL; } } printf("\n%d is successfully deleted",t->data); free(t); } void deleteend() { struct node t=start,t1; int x; if(start==NULL) //list is empty { printf("\n sll is empty"); } else { if(start->next==NULL) //list contains single element { start=NULL; } else // list contains more elements { t1=t->next; while(t1->next!=NULL) { t=t->next; t1=t1->next; } t->next=NULL; printf("\n%d is successfully deleted",t1->data); free(t1); } } }

Circular Linked List

A circular linked list is a sequence of elements in which every element has a link to its next element in the sequence and the last element has a link to the first element. In single linked list, every node points to its next node in the sequence and the last node points NULL. But in circular linked list, every node points to its next node in the sequence but the last node points to the first node in the list. That means circular linked list is similar to the single linked list except that the last node points to the first node in the list. Ex

Operations

In a circular linked list, we perform the following operations... ๏‚ท Insertion ๏‚ท Deletion ๏‚ท Display Before we implement actual operations, first we need to setup empty list. First perform the following steps before implementing actual operations. Step 1 - Include all the header files which are used in the program. Step 2 - Declare all the user defined functions. Step 3 - Define a Node structure with two members data and next Step 4 - Define a Node pointer ' head ' and set it to NULL. Step 5 - Implement the main method by displaying operations menu and make suitable function calls in the main method to perform user selected operation. void display() { struct node *t; if(start==NULL) { printf("sll is empty\n"); return; } printf("elements are:\n"); t=start; while(t!=NULL) { printf("--> %d",t->data); t=t->next; } return; } struct node //Each node in list will contain data and next pointer { int data; struct node next; }last=NULL;

Insertion

In a circular linked list, the insertion operation can be performed in three ways. They are as follows...

  1. Inserting At Beginning of the list
  2. Inserting At End of the list
  3. Inserting At Specific location in the list

Inserting At Beginning of the list

We can use the following steps to insert a new node at beginning of the circular linked list... Step 1 - Create a newNode with given value. Step 2 - Check whether list is Empty(head == NULL) Step 3 - If it is Empty then, set head = newNode and newNodeโ†’next = head. Step 4 - If it is Not Empty then, define a Node pointer ' temp ' and initialize with ' head '. Step 5 - Keep moving the ' temp ' to its next node until it reaches to the last node (until 'temp โ†’ next == head' ). Step 6 - Set 'newNode โ†’ next = head' , 'head = newNode' and 'temp โ†’ next = head'.

Inserting At End of the list

We can use the following steps to insert a new node at end of the circular linked list... Step 1 - Create a newNode with given value. Step 2 - Check whether list is Empty(head == NULL). Step 3 - If it is Empty then, set head = newNode and newNode โ†’ next = head. Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head. Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (until temp โ†’ next == head ). Step 6 - Set temp โ†’ next = newNode and newNode โ†’ next = head. void insertbeg() { struct node *nn; int a; nn=(struct node *)malloc(sizeof(struct node)); printf("enter data:"); scanf("%d",&nn->data); a=nn->data; nn->next=NULL; if(last==NULL) //checking if List is empty { printf("\n list is empty, so new node inserted as start node\n"); last=nn; last->next=nn; } else //list contains elements { nn->next=last->next; last->next=nn; } printf("\n %d succesfully inserted\n",a); } void insertend() { struct node nn,t; int b; nn=(struct node *)malloc(sizeof(struct node)); printf("\n enter data:"); scanf("%d",&nn->data); b=nn->data; nn->next=NULL; if(last==NULL) //checking if List is empty { printf("\n list is empty, so new node inserted as start node\n"); last=nn; last->next=nn; } else //list contains elements { nn->next=last->next; last->next=nn; last=last->next; } printf("%d is succesfully inserted\n",b); }

Step 4 - Check whether list is having only one node ( temp1 โ†’ next == head ) Step 5 - If it is TRUE then set head = NULL and delete temp1 (Setting Empty list conditions) Step 6 - If it is FALSE move the temp1 until it reaches to the last node. (until temp1 โ†’ next == head ) Step 7 - Then set head = temp2 โ†’ next, temp1 โ†’ next = head and delete temp2.

Deleting from End of the list

We can use the following steps to delete a node from end of the circular linked list... Step 1 - Check whether list is Empty(head == NULL) Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function. Step 3 - If it is Not Empty then, define two Node pointers ' temp1 ' and ' temp2 'and initialize ' temp1 ' with head. Step 4 - Check whether list has only one Node ( temp1 โ†’ next == head ) Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate from the function. (Setting Empty list condition) Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node. Repeat the same until temp1 reaches to the last node in the list. (until temp1 โ†’ next == head ) Step 7 - Set temp2 โ†’ next = head and delete temp1. void deletebeg() { if(last==NULL) //list is empty { printf("\n cll is empty"); } else { if(last->next==last) //list contains single element { struct node *t=last; last=NULL; t->next=NULL; printf("\ncll contains only one element and %d is successfully deleted",t->data); free(t); } else //list contains more than one element { struct node *t=last->next; last->next=t->next; t->next=NULL; printf("\n%d is successfully deleted",t->data); free(t); } } } void deleteend() { if(last==NULL) //list is empty { printf("\n cll is empty"); } else { if(last->next==last) //list contains single element { struct node *t=last; last=NULL; t->next=NULL; printf("\ncll contains one element and %d is successfully deleted",t->data); free(t); } else // list contains more elements { struct node t=last->next,t1; while(t->next!=last) t=t->next; t->next=last->next; t1=last; last=t; t1->next=NULL; printf("\n%d is successfully deleted",t1->data); free(t1); } } }

Deleting a Specific Node from the list

We can use the following steps to delete a specific node from the circular linked list... Step 1 - Check whether list is Empty(head == NULL) Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function. Step 3 - If it is Not Empty then, define two Node pointers ' temp1 ' and ' temp2 ' and initialize ' temp1' with head. Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the last node. And every time set 'temp2 = temp1' before moving the ' temp1 ' to its next node. Step 5 - If it is reached to the last node then display 'Given node not found in the list! Deletion not possible!!! '. And terminate the function. Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one node ( temp1 โ†’ next ==head ) Step 7 - If list has only one node and that is the node to be deleted then set head = NULL and delete temp1(free(temp1)). Step 8 - If list contains multiple nodes then check whether temp1 is the first node in the list ( temp1 == head ). Step 9 - If temp1 is the first node then set temp2 = head and keep moving temp2 to its next node until temp2 reaches to the last node. Then set head = head โ†’ next , temp2 โ†’ next = head and delete temp. Step 10 - If temp1 is not first node then check whether it is last node in the list ( temp1 โ†’ next == head ). Step 1 1- If temp1 is last node then set temp2 โ†’ next = head and delete temp1(free(temp1)). Step 12 - If temp1 is not first node and not last node then set temp2 โ†’ next = temp1 โ†’ next and delete temp1(free(temp1)).

Displaying a circular Linked List

We can use the following steps to display the elements of a circular linked list... Step 1 - Check whether list is Empty(head == NULL) Step 2 - If it is Empty , then display 'List is Empty!!! ' and terminate the function. Step 3 - If it is Not Empty then, define a Node pointer ' temp ' and initialize with head. Step 4 - Keep displaying temp โ†’ data with an arrow (--->) until temp reaches to the last node Step 5 - Finally display temp โ†’ data with arrow pointing to head โ†’ data. void deletemid() { if(last==NULL) //list is empty { printf("\n cll is empty"); } else { struct node t=last,t1; if(last->next==last) //list contains single element { last=NULL; t->next=NULL; printf("\ncll contains one element and %d is successfully deleted",t->data); free(t); } else //list contains more elements { int x; printf("enter data to be deleted:"); scanf("%d",&x); t1=t->next; while(t1->data!=x && t1!=last) { t=t->next; t1=t1->next; } if(t->next==last) { printf("\n data does not exist or it is last node please choose other node",x); } else { t->next=t1->next; t1->next=NULL; printf("\n%d is successfully deleted",t1->data); free(t1); } } } }

Step 4 - Define a Node pointer ' head' and set it to NULL. Step 5 - Implement the main method by displaying operations menu and make suitable function calls in the main method to perform user selected operation.

Insertion

In a double linked list, the insertion operation can be performed in three ways as follows...

  1. Inserting At Beginning of the list
  2. Inserting At End of the list
  3. Inserting At Specific location in the list

Inserting At Beginning of the list

We can use the following steps to insert a new node at beginning of the double linked list... Step 1 - Create a newNode with given value and newNode โ†’ previous as NULL. Step 2 - Check whether list is Empty(head == NULL) Step 3 - If it is Empty then, assign NULL to newNode โ†’ next and newNode to head. Step 4 - If it is not Empty then, assign head to newNode โ†’ next and newNode to head.

Inserting At End of the list

We can use the following steps to insert a new node at end of the double linked list... Step 1 - Create a newNode with given value and newNode โ†’ next as NULL. Step 2 - Check whether list is Empty(head == NULL) Step 3 - If it is Empty , then assign NULL to newNode โ†’ previous and newNode to head. Step 4 - If it is not Empty , then, define a node pointer temp and initialize with head. Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (until temp โ†’ next is equal to NULL ). Step 6 - Assign newNode to temp โ†’ next and temp to newNode โ†’ previous. struct node //Each node in list will contain data, previous and next pointer { struct node *prev; int data; struct node next; }start=NULL; void insertbeg() { struct node *nn; int a; nn=(struct node *)malloc(sizeof(struct node)); printf("enter data:"); scanf("%d",&nn->data); a=nn->data; nn->next=nn->prev=NULL; if(start==NULL) //checking if List is empty { printf("\n dll is empty, so new node inserted as start node\n"); start=nn; } else //list contains elements { nn->next=start; start->prev=nn; start=nn; } printf("\n %d succesfully inserted\n",a); } void insertend() { struct node nn,t; int b; nn=(struct node *)malloc(sizeof(struct node)); printf("\n enter data:"); scanf("%d",&nn->data); b=nn->data; nn->next=nn->prev=NULL; if(start==NULL) //checking if List is empty { printf("\n dll is empty, so new node inserted as start node\n"); start=nn; } else //list contains elements { t=start; while(t->next!=NULL) { t=t->next; } t->next=nn; nn->prev=t; } printf("%d is succesfully inserted\n",b); }

Inserting At Specific location in the list (After a Node)

We can use the following steps to insert a new node after a node in the double linked list... Step 1 - Create a newNode with given value. Step 2 - Check whether list is Empty(head == NULL) Step 3 - If it is Empty then, assign NULL to both newNode โ†’ previous & newNode โ†’ next and set newNode to head. Step 4 - If it is not Empty then, define two node pointers temp1 & temp2 and initialize temp1 with head. Step 5 - Keep moving the temp1 to its next node until it reaches to the node after which we want to insert the newNode (until temp1 โ†’ data is equal to location , here location is the node value after which we want to insert the newNode). Step 6 - Every time check whether temp1 is reached to the last node. If it is reached to the last node then display 'Given node is not found in the list!!! Insertion not possible!!! ' and terminate the function. Otherwise move the temp1 to next node. Step7- Assign temp1 โ†’ next to temp2 , newNode to temp1 โ†’ next , temp1 to newNode โ†’ Previous , temp2 to new Node โ†’ next and newNode to temp2โ†’ previous.

Deletion

In a double linked list, the deletion operation can be performed in three ways as follows...

  1. Deleting from Beginning of the list
  2. Deleting from End of the list
  3. Deleting a Specific Node

Deleting from Beginning of the list

We can use the following steps to delete a node from beginning of the double linked list... Step 1 - Check whether list is Empty(head == NULL) Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function. Step 3 - If it is not Empty then, define a Node pointer ' temp ' and initialize with head. Step 4 - Check whether list is having only one node ( temp โ†’ previous is equal to temp โ†’ next ) Step 5 - If it is TRUE , then set head to NULL and delete temp (Setting Empty list conditions) Step 6 - If it is FALSE, then assign temp โ†’ next to head, NULL to head โ†’ previous and delete temp. void insertmid() { struct node nn,t=start; int x,v; nn=(struct node *)malloc(sizeof(struct node)); if(start==NULL) //checking if List is empty { printf("dll is empty\n"); return; } printf("\n enter data:"); scanf("%d",&nn->data); v=nn->data; nn->next=NULL; nn->prev=NULL; printf("enter data after which no. is to be inserted:\n"); scanf("%d",&x); while(t!=NULL && t->data!=x) { t=t->next; } if(t==NULL) { printf("\n data does not exist",x); return; } else { if(t->next==NULL) // dll contains one element { t->next=nn; nn->prev=t; } else { t->next->prev=nn; nn->prev=t; nn->next=t->next; t->next=nn; } printf("%d succesfully inserted\n",v); } }

Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one node or not Step 7 - If list has only one node and that is the node which is to be deleted then set head to NULL and delete temp (free(temp)). Step 8 - If list contains multiple nodes, then check whether temp is the first node in the list (temp == head). Step 9 - If temp is the first node, then move the head to the next node ( head = head โ†’ next ), set head of previous to NULL(head โ†’ previous = NULL) and delete temp. Step 10 - If temp is not the first node, then check whether it is the last node in the list ( temp โ†’ next == NULL ). Step 11 - If temp is the last node then set temp of previous of next to NULL(temp โ†’ previous โ†’ next = NULL) and delete temp(free(temp)). Step 12 - If temp is not the first node and not the last node, then set temp of previous of next to temp of next (temp โ†’ previous โ†’ next = temp โ†’ next), temp of next of previous to temp of previous (temp โ†’ next โ†’ previous = temp โ†’ previous) and delete temp(free(temp)).

Displaying a Double Linked List

We can use the following steps to display the elements of a double linked list... Step 1 - Check whether list is Empty(head == NULL) Step 2 - If it is Empty , then display 'List is Empty!!! ' and terminate the function. Step 3 - If it is not Empty, then define a Node pointer ' temp ' and initialize with head. Step 4 - Display 'NULL <--- '. Step 5 - Keep displaying temp โ†’ data with an arrow ( <===> ) until temp reaches to the last node Step 6 - Finally, display temp โ†’ data with arrow pointing to NULL (temp โ†’ data ---> NULL). void deletemid() { struct node *t=start; int x; if(start==NULL) //list is empty { printf("\n dll is empty"); return; } else { if(start->next==NULL) //list contains single element { start=NULL; printf("dll contains only one element and %d is deleted",t->data); } else { printf("enter data to be deleted:"); scanf("%d",&x); while(t!=NULL && t->data!=x) { t=t->next; } if(t==NULL) { printf("\n data does not exist",x); } else { if(t->next==NULL) // deleting last element in dll { printf("\ndeleting last element in dll"); deleteend(); return; } if(t->prev==NULL) //deleting first element in dll { printf("\ndeleting first element in dll"); deletebeg(); return; } else //deleting middle element in dll { t->prev->next=t->next; t->next->prev=t->prev; t->next=t->prev=NULL; } printf("\n%d is successfully deleted",t->data); free(t); } } } }

Stack ADT

Stack is a linear data structure in which the operations are performed based on LIFO principle. "A Collection of similar data items in which both insertion and deletion operations are performed based on LIFO principle". Stack is a linear data structure in which the insertion and deletion operations are performed at only one end. In a stack, adding and removing of elements are performed at a single position which is known as " top ". That means, a new element is added at top of the stack and an element is removed from the top of the stack. In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out) or FILO (First In Last Out) principle. In a stack, the insertion operation is performed using a function called "push" and deletion operation is performed using a function called "pop". In the figure, PUSH and POP operations are performed at a top position in the stack. That means, both the insertion and deletion operations are performed at one end (i.e., at Top) Ex: If we want to create a stack by inserting elements like 10, 45, 12, 16, 35 and 50. Then 10 becomes the bottom-most element and 50 is the topmost element. The last inserted element 50 is at Top of the stack as shown in the image below...

Operations on a Stack

The following operations are performed on the stack...

**1. Push (To insert an element on to the stack)

  1. Pop (To delete an element from the stack)
  2. Display (To display elements of the stack)** void display() { struct node *t; if(start==NULL) { printf("dll is empty\n"); return; } printf("elements are:\n"); t=start; while(t!=NULL) { printf("<--> %d",t->data); t=t->next; } }

POP() - Delete a value from the Stack

In a stack, pop() is a function used to delete an element from the stack. In a stack, the element is always deleted from top position. Pop function does not take any value as parameter. We can use the following steps to pop an element from the stack... Step 1 - Check whether stack is EMPTY. ( top == -1 ) Step 2 - If it is EMPTY , then display "Stack is EMPTY!!! Deletion is not possible!!!" and terminate the function. Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--).

Display() - Displays the elements of a Stack

We can use the following steps to display the elements of a stack... Step 1 - Check whether stack is EMPTY. ( top == -1 ) Step 2 - If it is EMPTY , then display "Stack is EMPTY!!!" and terminate the function. Step 3 - If it is NOT EMPTY , then define a variable ' i ' and initialize with top. Display stack[i] value and decrement i value by one ( i-- ). Step 4 - Repeat above step until i value becomes '0'.

Stack Using Linked List

The major problem with the stack implemented using an array is, it works only for a fixed number of data values. That means the amount of data must be specified at the beginning of the implementation itself. Stack implemented using an array is not suitable, when we don't know the size of data which we are going to use. A stack data structure can be implemented by using a linked list data structure. The stack implemented using linked list can work for an unlimited number of values. That means, stack implemented using linked list works for the variable size of data. So, there is no need to fix the size at the beginning of the implementation. The Stack implemented using linked list can organize as many data values as we want. 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. void push() { int element; if(top==max-1) //stack is full printf("Stack is Full!!! Insertion is not possible!!!(stack overflows)\n"); else //stack is not full { printf("enter the element\n"); scanf("%d",&element); stack[++top]=element; printf("\nInsertion success!!!"); } } void pop() { int element; if(top==-1) //stack is empty printf("Stack is Empty!!! Deletion is not possible!!!(stack underflows)\n"); else //stack contains elements { printf("the element %d at position %d is deleted\n",stack[top],top); top=top-1; } } void display() { int i; if(top==-1) //stack is empty printf("Stack is Empty!!!(stack underflows)\n"); else //stack contains elements { printf("Stack elements are\n"); for(i=top;i>=0;i--) { printf("%d\n",stack[i]); } } }

Ex: 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

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.

Push(value) - Inserting an element into the Stack

We can use the following steps to insert a new node into the stack... Step 1 - Create a newNode with given value. Step 2 - Check whether stack is Empty ( top == NULL ) Step 3 - If it is Empty , then set newNode โ†’ next = NULL. Step 4 - If it is Not Empty , then set newNode โ†’ next = top. Step 5 - Finally, set top = newNode. #include #include struct node { int data; struct node next; }top=NULL,top1,temp; void push() { int element; printf("enter the element\n"); scanf("%d",&element); if (top == NULL) //stack is empty { top =(struct node *)malloc(sizeof(struct node)); top->next = NULL; top->data = element; } else //stack contains elements { temp =(struct node *)malloc(sizeof(struct node)); temp->next = top; temp->data = element; top = temp; } }