You are on page 1of 43

SRI KRISHNA COLLEGE OF ENGINEERING AND TECHNOLOGY

Kuniamuthur, Coimbatore, Tamilnadu, India


An Autonomous Institution, Affiliated to Anna University,
Accredited by NAAC with “A” Grade & Accredited by NBA (CSE, ECE, IT, MECH ,EEE, CIVIL& MCT)

Department of M.Tech Computer Science & Engineering

COURSE : DATA STRUCTURES


MODULE : 1 – LINEAR DATA STRUCTURE
TOPICS : STACK, QUEUE AND LIST (Introduction)
FACULTY : Mrs. Dr. S.KARTHIKEYINI
Assistant Professor / M.Tech CSE

www.skcet.ac.in

1
Sri Krishna College of Engineering & Technology
Department of Computer Science and Engineering

STACK, QUEUE AND LIST

2
What is data structure

• A data structure is a storage that is used to store and organize data. It is a


way of arranging data on a computer so that it can be accessed and updated
efficiently.
• A data structure is not only used for organizing the data. It is also used for
processing, retrieving, and storing data. There are different basic and
advanced types of data structures that are used in almost every program. 

3
Data Structure

4
Linear Data Structure
• Linear Data Structure
Data structure where data elements are arranged sequentially or linearly
where each and every element is attached to its previous and next
adjacent is called a linear data structure.
• In linear data structure, single level is involved. Therefore, we can
traverse all the elements in single run only. Linear data structures are
easy to implement because computer memory is arranged in a linear way.
Its examples are  list, stack and queue.

5
Stack
•The data structure follows the rule of LIFO (Last In-First Out) where the
data last added element is removed first.
• Push operation is used for adding an element of data on a stack and the pop
operation is used for deleting the data from the stack.
•This can be explained by the example of books stacked together. In order to
access the last book, all the books placed on top of the last book have to be
safely removed.
•A user can feasibly keep track of the last function/element of the list in a
stack using a pointer (top).

6
Stack Data Structure

7
What is a stack?
• Stores a set of elements in a particular order
• Stack principle: LAST IN FIRST OUT= LIFO
• It means: the last element inserted is the first one to be removed.
• Stack is a linear structure in which items may be added or removed only at
one end.

OPERATIONS:
• Top -> Is a variable which keep track at the top most element.
• PUSH – Insertion – Stack Overflow
• POP – Deletion – Stack Underflow
Last In First Out

top
top E
D D D
C top C C C
B top B B B B
A A A A A
A top
Basic Operations
 Stack operations may involve initializing the stack, using it and then de-
initializing it. Apart from these basic stuffs, a stack is used for the following two
primary operations −
push() − Pushing (storing) an element on the stack.
pop() − Removing (accessing) an element from the stack.
When data is Pushed onto stack.
To use a stack efficiently, we need to check the status of stack as well. For the
same purpose, the following functionality is added to stacks −
peek() − get the top data element of the stack, without removing it.
isFull() − check if stack is full.
isEmpty() − check if stack is empty.
At all times, we maintain a pointer to the last pushed data on the stack. As this
pointer always represents the top of the stack, hence named top. The top pointer
provides top value of the stack without actually removing it.
Push Operation
The process of putting a new data element onto stack is known as a Push
Operation. Push operation involves a series of steps −
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Step 5 − Returns success.
Algorithm for Push Operation

14
Pop Operation
Accessing the content while removing it from the stack, is known as a Pop
Operation. In an array implementation of pop() operation, the data element
is not actually removed, instead top is decremented to a lower position in
the stack to point to the next value. But in linked-list implementation, pop()
actually removes data element and deallocates memory space.
A Pop operation may involve the following steps
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
Step 3 − If the stack is not empty, accesses the data element at
which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
Pop Operation

• Before deleting the element from the stack, we check whether the stack is
empty.
• If we try to delete the element from the empty stack, then
the underflow condition occurs.
• If the stack is not empty, we first access the element which is pointed by
the top
• Once the pop operation is performed, the top is decremented by 1,
i.e., top=top-1.

16
Pop Operation

18
Algorithm for pop operation

19
Stack Applications

• Real life
– Pile of books
– Plate trays
• More applications related to computer
science
– Recursions
IMPLEMENTATION OF STACK

• Stack using Arrays


• Stack using Linked List
ARRAY IMPLEMENTATION OF STACK

int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
Array-based Stack Implementation
• Allocate an array of some size (pre-defined)
– Maximum N elements in stack
• Bottom stack element stored at element 0
• last index in the array is the top
• Increment top when one element is pushed,
decrement after pop
• Increment top and then push data
• pop-Take data pointed by top and then
Decrement top
Linked stack declarations
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
Applications of Stack

1. Expression Evaluation
2. Expression Conversion
    i. Infix to Postfix
    ii. Infix to Prefix
    iii. Postfix to Infix
    iv. Prefix to Infix
3. Backtracking
4. Memory Management
Queue
• This structure is almost similar to the stack as the data is stored
sequentially. The difference is that the queue data structure follows
FIFO which is the rule of First In-First Out where the first added
element is to exit the queue first. Front and rear are the two terms to be
used in a queue.
• Enqueue is the insertion operation and dequeue is the deletion
operation. The former is performed at the end of the queue and the
latter is performed at the start end. The data structure might be
explained with the example of people queuing up to ride a bus. The
first person in the line will get the chance to exit the queue while the
last person will be the last to exit.
26
Queue
• The front or head pointer points to the first inserted element that
is still in the list. The second pointer is the rear or tail one that
points to the last inserted element in the list.

27
Types of queue

• Simple Queue or Linear Queue


• Circular Queue
• Priority Queue
• Double Ended Queue (or Deque)

28
Applications of Queue
• Queues are widely used as waiting lists for a single shared resource like
printer, disk, CPU.
• Queues are used in asynchronous transfer of data (where data is not being
transferred at the same rate between two processes) for eg. pipes, file IO,
sockets.
• Queues are used as buffers in most of the applications like MP3 media
player, CD player, etc.
• Queue are used to maintain the play list in media players in order to add
and remove the songs from the play-list.
• Queues are used in operating systems for handling interrupts.
29
Linked List

Linked lists are the types where the data is stored in the form of nodes

which consist of an element of data and a pointer. The use of the pointer is
that it points or directs to the node which is next to the element in the
sequence.
The data stored in a linked list might be of any form, strings, numbers, or
characters. Both sorted and unsorted data can be stored in a linked list
along with unique or duplicate elements.

30
Linked List

• Linked list is a linear data structure that includes a series of connected


nodes. Linked list can be defined as the nodes that are randomly stored in
the memory.
• A node in the linked list contains two parts, i.e., first is the data part and
second is the address part. The last node of the list contains a pointer to the
null. After array, linked list is the second most used data structure. In a
linked list, every link contains a connection to another link.

31
Types of Linked List

• Singly linked List

• Doubly Linked List


• Circular Linked list
• Doubly linked List

32
Single Linked List

33
Create a Node by using Self-
referential structure

34
35
Create a node in a single linked list

36
Print the data of single lined list node

37
Create two nodes in a single
linked list

38
Cont.,

39
40
Two nodes in the single linked list

41
Insertion in a single linked list

42
43

You might also like