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

Unit-II Linear Structures

The document provides a comprehensive overview of various Abstract Data Types (ADTs) including List, Stack, Queue, and their implementations using arrays and linked lists. It details operations such as insertion, deletion, and traversal for each data structure, along with specific characteristics like LIFO for stacks and FIFO for queues. Additionally, it discusses circular queues and double-ended queues, highlighting their unique features and operational methods.

Uploaded by

madhan95ece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unit-II Linear Structures

The document provides a comprehensive overview of various Abstract Data Types (ADTs) including List, Stack, Queue, and their implementations using arrays and linked lists. It details operations such as insertion, deletion, and traversal for each data structure, along with specific characteristics like LIFO for stacks and FIFO for queues. Additionally, it discusses circular queues and double-ended queues, highlighting their unique features and operational methods.

Uploaded by

madhan95ece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 60

List ADT

1. List is a linear collection of data item.


2. The general form of list is
A1,A2,A3………….AN
where N is the size of the list. A1 is the first element.
AN is the last element.
3. It provides an ordered way to store, access and modify data.
• Assume the Example list is as given below:
the elements of a list are
34, 12, 52, 16, 12

Operations performed on the list are:

 Insert(element, position): Insert an element at any


position in the list.
 Delete(element): Remove the first occurrence of any
element from a non-empty list.

 Find(element): Return an index position from the list at


any given element.
 findKth(index) – finds the element at the index position
of the list

 Next(index): Return the position of successor element


(index+1)
 Previous(index): Return the position of predecessor
element(index-1)

 Print list: print the elements of the list.

 Size(): this is a method which gives the size of the list

Output: 5
 isEmpty(): is a Boolean method which gives a value of
true if the list is empty.

Output: False

 make Empty- creates an empty list


L=[ ] or
L=list()
Implementation of List ADT:
List ADT can be implemented in two ways.
(i) Array based implementation
(ii) Linked list implementation
Array based implementation:
 In array based implementation, a list is represented using an
array of fixed size, where each element is stored in contiguous
memory locations.
 The list represented by arrays is called static linked list.
Operations:
(i) Creating a python list:
 When the list() constructor is called, an array
structure is created to store the elements in the list.
 The length of the list obtained using len()
Input:

Output:

(ii) Appending an element:


 append() method is used to insert the item to the end
of the list
 If there is a room in the array, the element is stored in
the next available slot of the array and length field is
incremented by one.
 An array cannot change its size once it has been
created. When the array becomes full and there is no
free slot to append the element, the list allows
expansion of list.
Syntax:
Listname.append(value)

(iii) Insertion:
 Insertion is the process of adding an element to the array.
 The element can be inserted at any position of the array.

OUTPUT: 10 20 100 30 40 50
(iv) Deletion:
 Deletion is the process of removing an element from the array.
 The element can be deleted at any position in the array.
OUTPUT: 10 20 40 50

(v) Display(Traversal):
 Traversal is the process of visiting the elements in the
array.
 It is used to display the elements of the array.

OUTPUT: 10 20 30 40 50
(vi) Search:
 Search is the process of finding an element in the
given array.
 It returns the index value of a particular element.

OUTPUT:

Element found at index 2

Linked List Implementation:


A linked list is a linear data structure where elements, called nodes,
are stored in non-contiguous memory locations. Each node contains a
data part and a reference (or pointer) to the next node in the sequential
form.
Types of Linked Lists
1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List

Singly Linked List


 Each node points to the next node.
 Traversal is unidirectional (forward only).

Node:
Each node in a singly linked list includes:
 Data: The actual information held within the node, which could
be numbers, strings, or any other data type.
 Next Pointer: A reference to the next node in the list, which
helps in traversing the list forward.

Linked List Operations


 Insertion
 Deletion

Insertion:
1.Insertion at the Beginning
Steps:
 Create a new node.
 Set the new node’s next pointer to the current head.
 Update the head to point to the new node.
2.Insertion at the End
Steps:
 Create a new node.
 Traverse to the end of the list.
 Set the last node’s next pointer to the new node
.

3. Insertion at a Specific Position


Steps:
 Create a new node.
 Traverse to the node before the desired position.
 Set the new node’s next pointer to the current node’s next.
 Update the current node’s next pointer to the new node.
Deletion:
1.Deletion from the Beginning
Steps:
 Check if the list is empty.
 Set the head to the next node.

2. Deletion from the End


Steps:
 Check if the list is empty.
 Traverse to the second-to-last node.
 Set the second-to-last node’s next pointer to None.

3. Deletion from a Specific Position


Steps:
 Check if the list is empty.
 Traverse to the node before the desired position.
 Update the node’s next pointer to skip the node at the desired
position.
Doubly Linked List
A doubly linked list is a type of data structure where
each element, known as a node, holds data and two
references: one pointing to the next node and one
pointing to the previous node. This two-way linking
allows for movement in both directions through the list.
Node:
Each node in a doubly linked list includes:
 Data: The actual information held within the node,
which could be numbers, strings, or any other data
type.
 Next Pointer: A reference to the next node in the
list, which helps in traversing the list forward.
 Prev Pointer: A reference to the previous node in
the list, which facilitates backward traversal.

1. Insertion
(i) At the Beginning:
 Add a new node at the start of the list.
 Set the new node's next pointer to the current
head of the list, and update the prev pointer
of the existing head to point back to the new
node.
 Update the head pointer to the new node.
 (ii) At the End:
 Add a new node at the end.
 Traverse to the last node, set the next pointer
of the last node to the new node, and set the
prev pointer of the new node to the last node.

(iii) After a Given Node:


 To insert a node after a given node, adjust the
next pointer of the new node to the next
pointer of the given node.
 Update the next pointer of the given node to
the new node, and set the prev pointer of the
node that follows the new node (if any) to the
new node.
2. Deletion
(i)From the Beginning:
Remove the head node by updating the head to the
second node and setting the prev pointer of the new
head node to None.

(ii)From the End:


Remove the tail node by setting the next pointer of
the second last node to None and updating the tail
to this second last node.
(iii)A Specific Node:
Disconnect the node by adjusting the next pointer
of the preceding node to point to the node after the
target node, and adjust the prev pointer of the
succeeding node likewise.
Circular Linked List
 In a singly circular linked list, each node contains a
single pointer that points to the next node in the
sequence.
 The unique aspect of this structure is that the last
node in the list points back to the first node,
forming a circle.
 This setup allows for continuous, end-to-end
traversal of the list.

1.Insertion:
(i) At the beginning:
 Update the newnode next pointer as tail node next
pointer address.
 Update the tail node next pointer as newnode
address.
(ii) At the end:
 Update the newnode next pointer as tail node next
pointer address.
 Update the tail node next pointer as newnode
address.
 Assign the newnode address to the tail node.
(iii)After a given node:
 Create a temp pointer by assigning the tail node
next pointer address to it.
 Traverse the temp pointer until after which node
need to insert.
 Once temp pointer reached to the given node
means, assign the temp pointer next address to
newnode pointer next address.
 Assign the newnode address to temp pointer next
address.
Stack
 A stack is a collection of data items that can be accessed at only
one end, called top.
 Items can be inserted and deleted in a stack only at the top.
 The last item inserted in a stack is the first one to be deleted.
 A stack is called a Last-In-First-Out (LIFO) data structure.
 Two main operations on Stack is PUSH & POP
Stack() - creates a new stack that is empty. It needs no parameters
and returns an empty stack.
push(item)- adds a new item to the top of the stack. It needs the
item and returns nothing.
pop()- removes the top item from the stack. It needs no parameters
and returns the item. The stack is modified.
peek()- returns the top item from the stack but does not remove it. It
needs no parameters. The stack is not modified.
isEmpty()-tests to see whether the stack is empty. It needs no
parameters and returns a boolean value.
size() - returns the number of items on the stack. It needs no
parameters and returns an integer.
Overflow : a situation when we are Pushing item in Stack that is full.
Underflow : a situation when we are Popping item from empty stack
Assume stack size is 5

Operation Return value Stack Contents

S.push(5) – [5]

S.push(3) – [5,3]

size(S) 2 [5,3]

S.pop() 3 [5]
S.isempty() False [5]
S.pop() 5 []
S.isempty() True []
S.pop() “underflow” []
S.push(7) – [7]
S.push(9) – [7,9]
S.peek() 9 [7,9]
S.push(4) – [7,9,4]
S.push(6) – [7,9,4,6]
S.push(8) – [7,9,4,6,8]
S.push(10) “overflow” [7,9,4,6,8]
Array-Based Stack Implementation
OUTPUT:
5
95
stack overflow
Deleted element is 95
Deleted element is 13
Deleted element is 27
Deleted element is 7
Deleted element is 10
stack underflow
50
Implementation of Stack using Linked List

Defining the Node Class


By defining a 'Node' class that will represent each element in our
linked list.
class Node:
def __init__(self, data):
self.data = data
self.next = None
Initializing the Stack
class Stack:
def __init__(self):
self.top = None
Push Operation
To add an element to the stack.
def push(self, data):
newnode=Node(data)
newnode.next=self.top
self.top=newnode
Pop Operation
To remove an element from the stack.
def pop(self):
if self.top is None:
return None
else:
temp=self.top
self.top=self.top.next
temp.next=None
return temp.data
Code for implementation:
OUTPUT:
Advantages of Using Linked Lists for Stacks
 Dynamic Size: Linked lists can grow dynamically, making them
suitable for implementing a dynamic-sized stack.
 Efficient Operations: Push and pop operations have a time
complexity of O(1) since they involve modifying only the top
element.
 Ease of Implementation: Linked lists provide a straightforward
way to manage stack operations without the need for fixed-size
arrays.

Queue
A queue in data structures is a linear collection of elements that
operates under the First In, First Out (FIFO) principle. This
means that the first element added to the queue will be the first
one removed.

Queue Abstract Data Type


 Enqueue
The enqueue operation involves adding an element to the
rear of the queue.
 Dequeue
The dequeue operation removes an element from the
front of the queue.
 Peek or Front
This operation allows you to look at the front element of
the queue without removing it. This operation simply
retrieves the value of the front element.
 IsEmpty
Return True if queue Q does not contain any elements.
 len(Q): Return the number of elements in queue Q.
 Overflow : a situation when we are inserting item in queue that
is full.
 Underflow : a situation when we are deleting item from empty
queue.
Operation Return value first ←Q←last

Q.enqueue(5) – [5]

Q.enqueue(3) – [5,3]

len(Q) 2 [5,3]

Q.dequeue() 5 [3]
Q.isempty() False [3]
Q.dequeue() 3 []
S.isempty() True []
Q.dequeue() “underflow” []
Q.enqueue(7) – [7]
Q.enqueue(9) – [7,9]
Q.peek() 7 [7,9]
Q.enqueue(4) – [7,9,4]
Q.enqueue(6) – [7,9,4,6]
Q.enqueue(8) – [7,9,4,6,8]
Q.enqueue(10) “overflow” [7,9,4,6,8]
Array-Based Queue Implementation
OUTPUT:

Implementation of Queue using Linked List


Enqueue process:

Dequeue process:
OUTPUT:
Circular queue
 A circular queue is a special type of queue in data
structures where the last position is connected back
to the first position, forming a circle.
 This means when the queue reaches the end, it
wraps around to the beginning. This helps use all
available space efficiently without leaving any
gaps.
Circular Queue Operations
1. Enqueue (Adding an element)
Add an element to the rear of the circular queue.
Steps:
 Check if the queue is full. If (rear + 1) % size == front,
the queue is full.
 If the queue is not full, update the rear pointer to
(rear + 1) % size.
 Insert the new element at the rear position.
 If the queue was initially empty (i.e., front and rear was
-1), set front and rear to 0.
2. Dequeue (Removing an element)
Remove an element from the front of the circular queue.
Steps:
 Check if the queue is empty. If front == -1, the queue is
empty.
 If the queue is not empty, remove the element at the front
position.
 Update the front pointer to (front + 1) % size.
 If the queue becomes empty after the dequeue operation
(i.e., front equals rear), reset both front and rear to -1.

3. Peek/Front (Viewing the front element)


View the front element without removing it from the circular
queue.
Steps:
 Check if the queue is empty. If front == -1, the queue is
empty.
 If the queue is not empty, return the element at the front
position.
4. isEmpty (Checking if the queue is empty)
Check if the circular queue is empty.
Steps:
The queue is empty if front == -1.
5. isFull (Checking if the queue is full)
Check if the circular queue is full.
Steps:
The queue is full if (rear + 1) % size == front.

Implementation of Circular Queue Using Array


OUTPUT:
Double-Ended Queue (Deque)
A deque allows insertion and removal of elements from both
the front and the rear of the queue. This flexibility makes it a
handy tool for various applications where elements need to be
processed from both ends.

Common Operations on Deque in Data Structure


1. enqueue_front(value) :
This operation adds an element at the beginning of
the deque, shifting other elements to the right if
necessary.
Example: If the deque is [2, 3, 4],
after enqueue_front (1), the deque becomes
[1, 2, 3, 4].
2. enqueue_rear(value):
This operation adds an element at the end of the
deque without needing to shift other elements.
Example: If the deque is [1, 2, 3],
after enqueue_rear (4), the deque becomes
[1, 2, 3, 4].
3. deque_front():
This operation removes the first element of the
deque, shifting the remaining elements to the left.
Example: If the deque is [1, 2, 3],
after deque_front (), the deque becomes [2, 3].
4. dequeue_rear():
This operation removes the last element from the
deque without shifting any elements.
Example: If the deque is [1, 2, 3],
after dequeue_rear (), the deque becomes [1, 2].
5. front():
This operation gives the value of the first element of
the deque, which is the element at the front.
Example: If the deque is [1, 2, 3], front() will
return 1.
6. back():
This operation gives the value of the last element of
the deque, which is the element at the back.
Example: If the deque is [1, 2, 3], back() will
return 3.
7. empty():
This operation checks if there are any elements in
the deque. It returns true if the deque is empty,
and false otherwise.
Example: If the deque is [1, 2, 3], empty() will
return false.
If the deque is [], empty() will return true.
8. size():
This operation returns the number of elements
present in the deque without modifying it.
Example: If the deque is [1, 2, 3], size() will
return 3.

Implementation of Deque Using Array


OUTPUT:

You might also like