Unit-II Linear Structures
Unit-II Linear Structures
Output: 5
isEmpty(): is a Boolean method which gives a value of
true if the list is empty.
Output: False
Output:
(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:
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.
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
.
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.
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
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
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.
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:
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.