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

Week 9

The document discusses linked lists, including their structure, advantages, disadvantages, and variations such as circular and doubly linked lists. It also covers polynomial operations using linked lists, deque and priority queue implementations, and applications of stacks in expression evaluation and string manipulation. Additionally, it explains different notations for arithmetic expressions and methods for converting and evaluating them.

Uploaded by

vaxshgadd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Week 9

The document discusses linked lists, including their structure, advantages, disadvantages, and variations such as circular and doubly linked lists. It also covers polynomial operations using linked lists, deque and priority queue implementations, and applications of stacks in expression evaluation and string manipulation. Additionally, it explains different notations for arithmetic expressions and methods for converting and evaluating them.

Uploaded by

vaxshgadd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

DATA STRUCTURES

CSO102
WEEK-9 (DRAFT)
LINKED LIST:

A linked list is the most sought-after data structure when it comes to


handling dynamic data elements.

A linked list consists of a data element known as a node.

Each node consists of two fields: one field has data, and in the
second field, the node has an address that keeps a reference to the
next node.
LINKED LIST: ADVANTAGES

Linked lists have dynamic size allocation, allowing for


efficient insertion and deletion of elements at any position.

The insertion, addition, and removal operations are faster


in a LinkedList because there is no resizing of an array
done in the background.
LINKED LIST: DISADVANTAGES

RANDOM ACCESS

EXTRA MEMORY USAGE

COMPLEX IMPLEMENTATION
LINKED LIST: POSSIBLE CASES OF
INSERTION

1. Insert into an empty list


2. Insert in front
3. Insert at back
4. Insert in middle
LINKED LIST: VARIATIONS

Circular Linked Lists


Doubly Linked Lists
Circular Doubly Linked Lists
CIRCULAR LINKED LIST

The last node points to the first node of the


list
How The
do last node when
we know pointswe
tohave
the first nodetraversing
finished of the listThe last(Tip:
the list? nodecheck if the
poinHow
pointer of thedo we know
current nodewhen wetohave
is equal finished traversing the list?
the head.)
(Tip:How do we know when we have finished traversing the list?
(Tip: check if the pointer of the current node is equal to the head.)
check if the pointer of the current node is equal to the head.)
ts to the first node of the list
CIRCULAR LINKED LIST: Insertion

FRGHHTTYTd traversing the list? (Tip:How do we know when we


have finished traversing the list? (Tip: check if the pointer of the
current node is equal to the head.)
check if the pointer of the current node is equal to the head.)
ts to the first node of the list

SN Operation Description
1 Insertion at beginning Adding a node into circular singly linked list at the
beginning.
2 Insertion at the end Adding a node into circular singly linked list at the
end.
CIRCULAR LINKED LIST: Deletion

FRGHHTTYTd traversing the list? (Tip:How do we know when we


have finished traversing the list? (Tip: check if the pointer of the
current node is equal to the head.)
check if the pointer of the current node is equal to the head.)
ts to the first node of the list

SN Operation Description
1 Deletion at beginning Removing the node from circular singly linked list at
the beginning.
2 Deletion at the end Removing the node from circular singly linked list at
the end.
3 Searching Compare each element of the node with the given
item and return the location at which the item is
present in the list otherwise return null.
4 Traversing Visiting each element of the list at least once in
order to perform some specific operation.
DOUBLY LINKED LIST

Each node points to not only successor but the predecessor

There are two NULL: at the first and last nodes in the list

Advantage: given a node, it is easy to visit its predecessor.


DOUBLY LINKED LIST:
Advantages of Doubly Linked List over the singly linked list:

A DLL can be traversed in both forward and backward directions.

The delete operation in DLL is more efficient if a pointer to the node


to be deleted is given.

We can quickly insert a new node before a given node.

In a singly linked list, to delete a node, a pointer to the previous node


is needed. To get this previous node, sometimes the list is traversed.
In DLL, we can get the previous node using the previous pointer.

Disadvantages of Doubly Linked List over the singly linked list:

Every node of DLL Requires extra space for a previous pointer.

All operations require an extra pointer previous to be maintained.


DOUBLY LINKED LIST: Insertion
DOUBLY LINKED LIST: Insertion
DOUBLY LINKED LIST: Insertion
DOUBLY LINKED LIST: Deletion
DOUBLY LINKED LIST: C implementation
COMPONENTS:

struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
DOUBLY LINKED LIST: C implementation
COMPONENTS:

Node – A single unit in Doubly Linked List (DLL) contains – Data,


previous and next pointers.

Next Pointer – Contains the Address to the next node

Previous Pointer – Contains Addresses to the previous node

Data – Stores the data value

Head – The first node in DLL


DOUBLY LINKED LIST:
Advantages of Doubly Linked List over the singly linked list:

A DLL can be traversed in both forward and backward directions.

The delete operation in DLL is more efficient if a pointer to the node


to be deleted is given.

We can quickly insert a new node before a given node.

In a singly linked list, to delete a node, a pointer to the previous node


is needed. To get this previous node, sometimes the list is traversed.
In DLL, we can get the previous node using the previous pointer.

Disadvantages of Doubly Linked List over the singly linked list:

Every node of DLL Requires extra space for a previous pointer.

All operations require an extra pointer previous to be maintained.


CIRCULAR DOUBLY LINKED LIST

Circular Doubly Linked List has properties of both doubly linked list
and circular linked list.
Two consecutive elements are linked or connected by the previous
and next pointer and the last node points to the first node by the next
pointer and also the first node points to the last node by the previous
pointer.
CIRCULAR DOUBLY LINKED LIST

Next Lab Experiment: Implemenation of circular doubly linked list

-create
-insert(beginng, end, middle)
-delete (beginng, end, middle)
-search
-traverse
POLYNOMIAL ADDITION USING LINKED
LIST:

First polynomial: 5x^4 + 3x^2 + 1x^0


Second polynomial: 4x^4 + 2x^2 + 1x^1
Result: 9x^4 + 5x^2 + 1x^1 + 1x^0
POLYNOMIAL ADDITION USING LINKED
LIST:
ALGORITHM
1) Create a new linked list, newHead to store the resultant
list.
2) Traverse both lists until one of them is null.
3) If any list is null insert the remaining node of another list
in the resultant list.
4) Otherwise compare the degree of both nodes, a (first
list’s node) and b (second list’s node). Here three cases
are possible:
If the degree of a and b is equal, we insert a new node in
the resultant list with the coefficient equal to the sum of
coefficients of a and b and the same degree.
If the degree of a is greater than b, we insert a new node
in the resultant list with the coefficient and degree equal to
that of a.
If the degree of b is greater than a, we insert a new node
in the resultant list with the coefficient and degree equal to
that of b.
C Function
Node* add(Node* poly1, Node* poly2) {
Node* result = NULL;

while (poly1 != NULL && poly2 != NULL) {


if (poly1->exp == poly2->exp) {
insert(&result, poly1->coef + poly2->coef, poly1->exp);
poly1 = poly1->next;
poly2 = poly2->next;
} else if (poly1->exp > poly2->exp) {
insert(&result, poly1->coef, poly1->exp);
poly1 = poly1->next;
} else {
insert(&result, poly2->coef, poly2->exp);
poly2 = poly2->next;
}
}

while (poly1 != NULL) {


insert(&result, poly1->coef, poly1->exp);
poly1 = poly1->next;
}

while (poly2 != NULL) {


insert(&result, poly2->coef, poly2->exp);
poly2 = poly2->next;
}

return result;
}
POLYNOMIAL ADDITION(3 variables):

struct Node
{
float coeff;
int powX;
int powY;
int powZ;
struct Node* next;
};
POLYNOMIAL MULTIPLICATION:

Input:

Poly1: 3x^2 + 5x^1 + 6

Poly2: 6x^1 + 8

Output: 18x^3 + 54x^2 + 76x^1 + 48


POLYNOMIAL MULTIPLICATION:
POLYNOMIAL MULTIPLICATION:

ALGORITHM
Multiply the 2nd polynomial with each term of 1st
polynomial.

Store the multiplied value in a new linked list.

Add the coefficients of elements having the same power in


resultant polynomial.
DEQUE (Double-Ended Queue):

-allows insertion and deletion at both ends


DEQUE(Double-Ended Queue):

push_front() : Inserts the element at the beginning. O(1)

push_back() : Adds element at the end. O(1)


pop_front() : Removes the first element from the deque. O(1)

pop_back() : Removes the last element from the deque. O(1)

Front() : Gets the front element from the deque. O(1)


Back() : Gets the last element from the deque. O(1)
Empty() : Checks whether the deque is empty or not. O(1)

Size() : Determines the number of elements in the deque. O(1)


Priority Queue:
- It arranges elements based on their priority values.

- Elements with higher priority values are typically


retrieved before elements with lower priority values.
Priority Queue Implementation:
- It arranges elements based on their priority values.

- Elements with higher priority values are typically


retrieved before elements with lower priority values.

ARRAY vs LINKED LIST IMPLEMENATION:


enqueue(): This function is used to insert new data into the queue. O(1) / O(1)
dequeue(): This function removes the element with the highest priority from the queue. O(n) / O(1)
peek()/top(): This function is used to get the highest priority element in the queue without
removing it from the queue. O(n) / O(1)
Applications of Stack:

- It can be used to convert one form of expression to


another form.

- A Stack can be used for evaluating expressions


consisting of operands and operators.

- Stacks can be used for parenthesis matching in an


expression.

- String reversal
Notations for Arithmetic Expression:

There are three notations to represent an arithmetic


expression:

Infix Notation
Prefix Notation
Postfix Notation

Infix Notation

The infix notation is a convenient way of writing an


expression in which each operator is placed between the
operands. Infix expressions can be parenthesized or
unparenthesized depending upon the problem requirement.

Example: A + B, C - D etc.
Prefix Notation:

The prefix notation places the operator before the


operands. This notation was introduced by the Polish
mathematician and hence often referred to as polish
notation.

Example: + A B, -CD etc.

All these expressions are in prefix notation because the


operator comes before the operands.
Postfix Notation:

The postfix notation places the operator after the


operands. This notation is just the reverse of Polish
notation and also known as Reverse Polish notation.

Example: AB +, CD+, etc.

All these expressions are in postfix notation because the


operator comes after the operands.
Infix to Postfix Conversion:
Example: A+B*C/D-E
Postfix Evaluation:

Read in one symbol at a time from the postfix expression.

Any time you see an operand, push it onto the stack.


Any time you see a binary operator (+, -, *, /) or unary
(square root, negative sign) operator, If the operator is
binary, pop two elements off of the stack. If the
operator is unary, pop one element off the stack.

Evaluate those operands with that operator


Push the result back onto the stack.

When you're done with the entire expression, the only


thing left on the stack should be the final result.
String Reversal:

This can be achieved by simply pushing one by one each


character onto the Stack, which later can be popped from
the Stack one by one. Because of the last in first out
property of the Stack, the first character of the Stack
is on the bottom of the Stack and the last character of
the String is on the Top of the Stack and after
performing the pop operation in the Stack, the Stack
returns the String in Reverse order.
String Reversal:
Parenthesis Matching:

Traverse the string .


If the current character is a starting bracket ( ‘(‘ or
‘{‘ or ‘[‘ ) then push it to stack.

If the current character is a closing bracket ( ‘)’ or


‘}’ or ‘]’ ) then pop from the stack and if the popped
character is the matching starting bracket then fine.

Else brackets are Not Balanced.

After complete traversal, if some starting brackets are


left in the stack then the expression is Not balanced,
else Balanced.
String Matching:
The Rabin-Karp Method of String Matching:

You might also like