0% found this document useful (0 votes)
18 views75 pages

2 Link

Uploaded by

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

2 Link

Uploaded by

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

DATA STRUCTURE

DR. ACHAL KAUSHIK

9/14/2023 1
Paper Code(s): CIC-209 L P C
Paper: Data Structures 4 - 4
Marking Scheme:
1. Teachers Continuous Evaluation: 25 marks
2. Term end Theory Examinations: 75 marks
Instructions for paper setter:
1. There should be 9 questions in the term end examinations question paper.
2. The first (1st) question should be compulsory and cover the entire syllabus. This question should be objective, single line answers or short
answer type question of total 15 marks.
3. Apart from question 1 which is compulsory, rest of the paper shall consist of 4 units as per the syllabus. Every unit shall have two questions
covering the corresponding unit of the syllabus. However, the student shall be asked to attempt only one of the two questions in the unit.
Individual questions may contain upto 5 sub-parts / sub-questions. Each Unit shall have a marks weightage of 15.
4. The questions are to be framed keeping in view the learning outcomes of the course / paper. The standard / level of the questions to be asked
should be at the level of the prescribed textbook.
5. The requirement of (scientific) calculators / log-tables / data – tables may be specified if required.
Course Objectives :
1. To introduce basics of Data structures (Arrays, strings, linked list etc.)
2. To understand the concepts of Stacks, Queues and Trees, related operations and their implementation
3. To understand sets, heaps and graphs
4. To introduce various Sorting and searching Algorithms
Course Outcomes (CO)
CO 1 To be able to understand difference between structured data and data structure
CO 2 To be able to create common basic data structures and trees
CO 3 To have a knowledge of sets, heaps and graphs
CO 4 To have basic knowledge of sorting and searching algorithms
Course Outcomes (CO) to Programme Outcomes (PO) mapping (scale 1: low, 2: Medium, 3: High)
PO01 PO02 PO03 PO04 PO05 PO06 PO07 PO08 PO09 PO10 PO11 PO12

CO 1 3 2 2 2 3 - - - 2 2 2 3
CO 2 3 2 2 2 3 - - - 2 2 2 3
CO 3 3 2 2 2 3 - - - 2 2 2 3
CO 4 3 2 2 2 3 - - - 2 2 2 3
UNIT – I
Overview of data structure, Basics of Algorithm Analysis including Running Time Calculations, Abstract Data Types, Arrays, Arrays and Pointers,
Multidimensional Array, String processing, General Lists and List ADT, List manipulations, Single, double and circular lists. Stacks and Stack ADT,
Stack Manipulation, Prefix, infix and postfix expressions, recursion. Queues and Queue ADT, Queue manipulation.

UNIT – II
Sparse Matrix Representation (Array and Link List representation) and arithmetic (addition, subtraction and multiplication), polynomials and
polynomial arithmetic.
Trees, Properties of Trees, Binary trees, Binary Tree traversal, Tree manipulation algorithms, Expression trees and their usage, binary search trees,
AVL Trees, Heaps and their implementation, Priority Queues, B-Trees, B* Tree, B+ Tree

UNIT – III
Sorting concept, order, stability, Selection sorts (straight, heap), insertion sort (Straight Insertion, Shell sort), Exchange Sort (Bubble, quicksort),
Merge sort (External Sorting) (Natural merge, balanced merge and polyphase merge). Searching – List search, sequential search, binary search,
hashing methods, collision resolution in hashing.

UNIT – IV
Disjoint sets representation, union find algorithm, Graphs, Graph representation, Graph Traversals and their implementations (BFS and DFS).
Minimum Spanning Tree algorithms, Shortest Path Algorithms

Textbook(s):
1. Richard Gilberg , Behrouz A. Forouzan, “Data Structures: A Pseudocode Approach with C, 2nd Edition, Cengage Learning, Oct 2004
2. E. Horowitz, S. Sahni, S. Anderson-Freed, "Fundamentals of Data Structures in C", 2nd Edition, Silicon Press (US), 2007.

References:
1. Mark Allen Weiss, “Data Structures and Algorithm Analysis in C”, 2nd Edition, Pearson, September, 1996
2. Robert Kruse, “Data Structures and Program Design in C”, 2nd Edition, Pearson, November, 1990
3. Seymour Lipschutz, “Data Structures with C (Schaum's Outline Series)”, McGrawhill, 2017
4. A. M. Tenenbaum, “Data structures using C”. Pearson Education, India, 1st Edition 2003.
5. Weiss M.A., “Data structures and algorithm analysis in C++”, Pearson Education, 2014.
Data Structure
• Data Structure is a way of collecting and organizing data in such a
way that we can perform operations on these data in an effective
way
• In simple language, Data Structures are structures programmed to
store ordered data, so that various operations can be performed on
it easily

9/14/2023 4
The List ADT
• A sequence of zero or more elements
A1, A2, A3, … AN
• N: length of the list
• A1: first element
• AN: last element
• Ai: position i
• If N=0, then empty list
• Linearly ordered
• Ai precedes Ai+1
• Ai follows Ai-1

9/14/2023 5
List Operations

• Print the list


• Create an empty list
• Find the position of an object in a list
❑list: 4,12, 52, 66, 92
❑find(52) → 3

• Insert/ Delete an object to a list


❑insert(55,3) → 4,12, 52, 55, 66, 92 remove: delete an element from the
list
❑delete(52) → 34, 12, 55, 16, 12

• Retrieve the element at a certain position

9/14/2023 6
List Implementations
• Two standard implementations for the list ADT
❑Array-based
❑Linked list (pointers)

9/14/2023 7
Array Implementation (List)

• Elements are stored in contiguous array positions

1 First element
2 Second element

list
Last Last element

Max length empty

9/14/2023 8
Array Implementation (List)

• Requires an estimate of the maximum size of the list


❑waste space

• Print and find in linear time


• Find Kth in constant time
• insert and delete: slow
❑requires shifting all the elements down one spot
❑requires shifting all the elements in the list up one

9/14/2023 9
List

Roll Name Enroll No. Next


Start
1 7
10
2 2-CSE-A-19 AASHISH 00220802719 8
3 13
4 4-CSE-A-19 AAYUSH 00420802719 5
5 5-CSE-A-19 ABHISHEK 00520802719 12
6 1
7 3
8 3-CSE-A-19 AAYUSH 00320802719 4
AVAIL 9 9-CSE-A-19 AKSHAY 00920802719 11
6
10 1-CSE-A-19 AAKASH 00120802719 2
11 11-CSE-A-19 AMAN 01120802719 0
12 7-CSE-A-19 ACHINTYA 00720802719 9
13 0

9/14/2023 10
Pointer Implementation (List)

• Ensure that the list is not stored contiguously


❑use a linked list
❑a series of structures that are not necessarily adjacent in memory

• Each node contains the element and a pointer to a structure


containing its successor
❑the last cell’s next link points to NULL

9/14/2023 11
Linked List

• A linked-list is a sequence of data structures which are connected together


via links
• An ordered set of data elements (node)
• Each node pointing to the next node by means of a pointer

9/14/2023 12
Advantages
• Linked lists are a dynamic data structure
owhich can grow and be pruned
oallocating and de-allocating memory while the
program is running
• Insertion and deletion node operations are easy
• Linear data structures such as stacks and queues are easily
executed
• may expand in real time without memory overhead

9/14/2023 13
Disadvantages

• Use more memory than arrays - storage used by their pointers


• Nodes in a linked list must be read in order from the beginning
• as linked lists are inherently sequential access
• Nodes may not be stored contiguously
• More time required to access individual elements within the list
• Difficulty when it comes to reverse traversing
• singly linked lists are cumbersome to navigate backwards
• doubly linked lists - memory is wasted in allocating space for a back-
pointer

9/14/2023 14
Pointer Implementation (List)

A B C X

START
• Linked list is a series of connected nodes
• Each node contains at least
❑A piece of data (any type)
❑Pointer to the next node in the list

• Head: pointer to the first node


• The last node points to NULL

9/14/2023 15
List
• Linked List in C (with pointers)

HEAD

100

A1 501 A2 542 A3 137 A4 641 A5 X

100 501 542 137 641

9/14/2023 16
Insertion into a linked list
A1 A2 A3 A4 A5 0

B1

A1 A2 A3 A4 A5 0

B1

A1 A2 A3 A4 A5 0

9/14/2023 B1 17
Insertion showing pointer number

• Insert B1 after A2

A1 501 A2 542 A3 137 A4 641 A5 X

100 501 542 137 641


• Step 1
B

933
• Step 2
B 542

933
• Step 3

A1 501 A2 933 B 542 A3 137 A4 641 A5 X


2023
100 501 933 542 137 641
Deleting from a Linked List
• To remove A3, just change the pointer

A1 A2 A3 A4 A5 0

A1 A2 A3 A4 A5 0

9/14/2023 19
Deleting from a Linked List
• To remove A3, just change the pointer

A1 501 A2 542 A3 137 A4 641 A5 X

100 501 542 137 641

A1 501 A2 137 A3 137 A4 641 A5 X

100 501 542 137 641

9/14/2023 20
Why Linked list is better?

• Insert takes constant time, O(1)


❑Just create a new element and move two pointers (assumes we are already at the
element we want to insert)
❑otherwise to find that element add O(N)

• Delete takes constant time, O(1)


❑Just move one pointer (assumes we are already at the element we want to delete)
❑otherwise to find that element add O(N)

• Can insert/delete N elements in O(N) time


• Recall that the array implementation of the List ADT
• required O(N2). So this is much faster!

9/14/2023 21
Complexity
• Print List: Start with the first element and then follow the pointers.
O(N)

• Find kth(i): Takes O(N) time.


❑Worst case: have to follow links through all N elements
❑Not as good as the O(1) from array implementation

9/14/2023 22
Singly linked list: Traversing
• Algorithm for traversing a linked list

• Step 1: [Initialize] SET PTR = START


• Step 2: Repeat steps 3 and 4 while PTR != NULL
• Step 3: Apply Process to PTR -> DATA
• Step 4: SET PTR = PTR -> NEXT
• [END OF LOOP]

• Step 5: EXIT

9/14/2023 23
Singly linked list: Printing
• Algorithm to print each node of the linked list

• Step 1: [Initialize] SET PTR = START


• Step 2: Repeat steps 3 and 4 while PTR != NULL
• Step 3: Write PTR -> DATA
• Step 4: SET PTR = PTR -> NEXT
• [END OF LOOP]

• Step 5: EXIT

9/14/2023 24
Algorithm: Searching an unsorted linked list

• Step 1: [Initialize] SET PTR = START


• Step 2: Repeat steps 3 while PTR != NULL
• Step 3: IF VAL = PTR -> DATA
• SET POS = PTR
• GO TO Step 5
ELSE
• SET PTR = PTR -> NEXT
• [END OF IF]
• [END OF LOOP]
• Step 4: SET POS = NULL
• Step 5: EXIT

9/14/2023 25
Example: Searching an unsorted linked list

9/14/2023 26
Algorithm: Searching a sorted linked list

• Step 1: [Initialize] SET PTR = START


• Step 2: Repeat steps 3 while PTR != NULL
• Step 3: IF PTR -> DATA = VAL then
• SET POS = PTR
• GO TO Step 5
ELSE IF PTR -> DATA < VAL
• SET PTR = PTR -> NEXT
ELSE
GO TO Step 4
• [END OF IF]
• [END OF LOOP]
• Step 4: SET POS = NULL
• Step 5: EXIT

9/14/2023 27
Insertion a new node in a linked list

• Case 1: New node is inserted at the beginning


• Case 2: New node is inserted at the end
• Case 3: New node is inserted after a given node
• Case 4: New node is inserted before a given node
• Case 5: New node is inserted in a sorted linked list

9/14/2023 28
Example: Insertion at the beginning

9/14/2023 29
Algorithm: Insert a new node at the beginning

• Step 1: IF AVAIL = NULL, then


• Write OVERFLOW
• Go to step 7
• [END OF IF]

• Step 2: SET NEW_NODE = AVAIL


• Step 3: SET AVAIL = AVAIL - > NEXT
• Step 4: SET New_Node -> DATA = VAL
• Step 5: SET New_Node -> NEXT = START
• Step 6: SET START = New_Node
• Step 7: EXIT

9/14/2023 30
Example: Insertion at the end

9/14/2023 31
Algorithm: Insert a new node at the end

• Step 1: IF AVAIL = NULL, then


• Write OVERFLOW
• Go to step 10
• [END OF IF]
• Step 2: SET NEW_NODE = AVAIL
• Step 3: SET AVAIL = AVAIL - > NEXT
• Step 4: SET New_Node -> DATA = VAL
• Step 5: SET New_Node -> NEXT = NULL
• Step 6: SET PTR = START
• Step 7: Repeat step 8 while PTR -> NEXT != NULL
• Step 8: SET PTR = PTR ->NEXT
• [END OF LOOP]
• Step 9: SET PTR -> NEXT = New_Node
• Step 10: EXIT
9/14/2023 32
Example: Insert after a given node

9/14/2023 33
Algorithm: Insert after a given node
• Step 1: IF AVAIL = NULL, then
• Write OVERFLOW
• Go to step 12
• [END OF IF]

• Step 2: SET NEW_NODE = AVAIL


• Step 3: SET AVAIL = AVAIL - > NEXT
• Step 4: SET New_Node -> DATA = VAL
• Step 5: SET PTR = START
• Step 6: SET PREPTR = PTR
• Step 7: Repeat step 8 and 9 while PREPTR -> DATA != NUM
Step 8: SET PREPTR = PTR
• Step 9: SET PTR = PTR ->NEXT
• [END OF LOOP]
• Step 10: SET New_Node -> NEXT = PTR
• Step 11: PREPTR -> NEXT = New_Node
• Step 12: EXIT

9/14/2023 34
Example: Insert before a node

9/14/2023 35
Algorithm: Insert a new node before a node

• Step 1: IF AVAIL = NULL, then


• Write OVERFLOW
• Go to step 12
• [END OF IF]

• Step 2: SET NEW_NODE = AVAIL


• Step 3: SET AVAIL = AVAIL - > NEXT
• Step 4: SET New_Node -> DATA = VAL
• Step 5: SET PTR = START
• Step 6: SET PREPTR = PTR
• Step 7: Repeat step 8 and 9 while PTR -> DATA != NUM
Step 8: SET PREPTR = PTR
• Step 9: SET PTR = PTR ->NEXT
• [END OF LOOP]
• Step 10: PREPTR -> NEXT = New_Node
• Step 11: SET New_Node -> NEXT = PTR
• Step 12: EXIT
9/14/2023 36
Example: Insert in a sorted linked list

9/14/2023 37
Algorithm: Insert in sorted linked list
• Step 1: IF AVAIL = NULL, then
• Write OVERFLOW
• Go to step 12
• [END OF IF]

• Step 2: SET NEW_NODE = AVAIL


• Step 3: SET AVAIL = AVAIL - > NEXT
• Step 4: SET New_Node -> DATA = VAL
• Step 5: SET PTR = START
• Step 6: SET PREPTR = PTR
• Step 7: Repeat step 8 and 9 while PTR -> DATA < VAL
Step 8: SET PREPTR = PTR
• Step 9: SET PTR = PTR ->NEXT
• [END OF LOOP]
• Step 10: SET PREPTR -> NEXT = New_Node
• Step 11: SET New_Node -> NEXT = PTR
• Step 12: EXIT

9/14/2023 38
Deleting a node from a linked list
• Case 1: First node is deleted
• Case 2: Last node is deleted
• Case 3: Node after a given node is deleted
• Case 4: Node is deleted from a sorted linked list

9/14/2023 39
Deleting the first node

9/14/2023 40
Example: Deleting the last node

9/14/2023 41
Algorithm: Deleting the last node

• Step 1: IF START= NULL, then


• Write UNDERFLOW
• Go to step 8
• [END OF IF]

• Step 2: SET PTR = START


• Step 3: Repeat step 4 and 5 while PTR -> NEXT != NULL
• Step 4: SET PREPTR = PTR
• Step 5: SET PTR = PTR -> NEXT
• [END OF LOOP]

• Step 6: SET PREPTR -> NEXT = NULL


• Step 7: FREE PTR
• Step 8: EXIT

9/14/2023 42
Example: Deleting after a given node

9/14/2023 43
Algorithm : Deleting after a given node

• Step 1: IF START= NULL, then


• Write UNDERFLOW
• Go to step 10
• [END OF IF]
• Step 2: SET PTR = START
• Step 3: SET PREPTR = PTR
• Step 4: Repeat step 5 and 6 while PREPTR -> DATA!= NUM
• Step 5: SET PREPTR = PTR
• Step 6: SET PTR = PTR -> NEXT
• [END OF LOOP]
• Step 7: SET TEMP = PTR -> NEXT
• Step 8: SET PREPTR -> NEXT = TEMP -> NEXT
• Step 9: FREE TEMP
• Step 10: EXIT
9/14/2023 44
Example: Deleting from a sorted link list

9/14/2023 45
Algorithm: Deleting from a sorted link list

• Step 1: IF START= NULL, then


• Write UNDERFLOW
• Go to step 9
• [END OF IF]
• Step 2: SET PTR = START
• Step 3: SET PREPTR = PTR
• Step 4: Repeat step 5 and 6 while PTR -> DATA!= NUM
• Step 5: SET PREPTR = PTR
• Step 6: SET PTR = PTR -> NEXT
• [END OF LOOP]
• Step 7: PREPTR -> NEXT = PTR -> NEXT
• Step 8: FREE PTR
• Step 9: EXIT
9/14/2023 46
Header Node
• Grounded Header
❑Stores NULL in the next field of the last node

Head A B C X

• Circular Header
❑Stores address of the header node in the next field of the last node
❑Header node will denote the end of the list

Head A B C

9/14/2023 47
LINKED LIST
• #include<stdio.h>
• #include<stdlib.h>
• //defining the struct in a recursive manner
• typedef struct Node
•{
• int data; // Structure contains a value of integer type data
• struct Node *next; // a pointer to a structure of same type
• }node; // name of our node type is node

• // the pointer ‘next’ contains the address of next block of this linked list
& this ‘next’ pointer of the last node would contain a NULL

9/14/2023 48
Function: INSERT
• void insert(node *ptr, int data)
•{
• node *start = ptr;
• /* Iterate through the list till we encounter the last node.*/
• while(ptr->next!=NULL)
• {
• ptr = ptr -> next;
• }
• /* Allocate memory for the new node and put data in it.*/
• ptr->next = (node *)malloc(sizeof(node)); // pointer ‘ptr->next’ contains address
of a newly created node
• ptr = ptr->next;
• ptr->data = data;
• ptr->next = NULL;
•} 9/14/2023 49
Function: SEARCH
• int find(node *ptr, int key)
•{
• node *start = ptr;
• ptr = ptr -> next; //First node is dummy node.
• /* Iterate through the entire linked list and search for the key. */
• while(ptr!=NULL)
• {
• if(ptr->data == key) //key is found.
• {
• return 1;
• }
• ptr = ptr -> next; //Search in the next node.
• }
• /*Key is not found */
• return 0;
9/14/2023 50
•}
Function: Delete
• void delete(node *ptr, int data)
• {
• node *start = ptr;
• /* Go to the node for which the node next to it has to be deleted */
• while(ptr->next!=NULL && (ptr->next)->data != data)
• {
• ptr = ptr -> next;
• }
• if(ptr->next==NULL)
• {
• printf("Element %d is not present in the list\n", data);
• return;
• }
• /* Now ptr points to a node and the node next to it has to be removed */
• node *temp;
• temp = ptr -> next; /*temp points to the node which has to be removed*/
• ptr->next = temp->next; /*We removed the node which is next to the ptr (which is also temp) */
• free(temp); /* Because deleted node no longer require the memory used for it free() will deallocate the memory. */
• return;
9/14/2023 51
• }
Function: PRINT
• void print(node *start,node *ptr)
•{
• if(ptr==NULL)
• {
• return;
• }
• printf("%d ",ptr->data);
• print(start,ptr->next);
•}

9/14/2023 52
MAIN
• int main()
• {
• /* start always points to the first node of the linked list. temp is used to point to the last
node of the linked list.*/
• node *start,*temp, *Head;
• start = (node *)malloc(sizeof(node));
• temp = start;
• temp -> next = NULL;
• /* Here in this code, we take the first node as a dummy node. The first node does not
contain data, but it used because to avoid handling special cases in insert and delete functions
*/
• printf("1. Insert\n");
• printf("2. Delete\n");
• printf("3. Print\n");
• printf("4. Find\n");
• printf("5. EXIT\n");

9/14/2023 53
Inserting
• while(1)
• {
• printf(" Press your choice \n");
• int choice;
• scanf("%d",&choice);
• // Insert Element
• if(choice==1)
• {
• int data;
• printf("The Element to be inserted in the list is: ");
• scanf("%d", &data);
• insert(start, data); // Function Insert
• }
9/14/2023 54
Deleting
• // Delete element
• else if(choice==2)
• {
• int data;
• printf("The Element to be deleted from the list is: ");
• scanf("%d", &data);
• delete(start, data); // Function Delete
• }

9/14/2023 55
Printing
• // Print list
• else if(choice==3)
• {
• printf("The list is ");
• print(start, start->next); // Function Print
• printf("\n");
• }

9/14/2023 56
Searching
• // Search Element
• else if(choice==4)
• {
• int data;
• printf("The Element is to be searched in the list is: ");
• scanf("%d", &data);
• int status = find(start, data); // Function search
• if(status)
• {
• printf("Element Found\n");
• }
• else
• {
• printf("Element Not Found\n");
• }
• 9/14/2023 } 57
EXIT
• else
• break;
• }
•}

9/14/2023 58
Circular Linked List

• Last node contains a pointer to the first node of the list

HEAD

3 4 6 9

9/14/2023 59
Circular linked list Application
• Task management in OS (Time sharing)
• Multi-Player Board Game
• Songs in a playlist

9/14/2023 60
Circular Linked List Representation

Roll Name Enroll No. Next


Start
1 7
10
2 2-CSE-A-19 AASHISH 00220802719 8
3 13
4 4-CSE-A-19 AAYUSH 00420802719 5
5 5-CSE-A-19 ABHISHEK 00520802719 12
6 1
7 3
8 3-CSE-A-19 AAYUSH 00320802719 4
AVAIL 9 9-CSE-A-19 AKSHAY 00920802719 11
6
10 1-CSE-A-19 AAKASH 00120802719 2
11-CSE-A-
11 AMAN 01120802719 10
19
12 7-CSE-A-19 ACHINTYA 00720802719 9
13 0
9/14/2023 61
Algorithm: Insert at the Beginning
(CLL)
• Step 1: IF AVAIL = NULL, then
• Write OVERFLOW
• Go to step 7
• [END OF IF]

• Step 2: SET New_Node = AVAIL


• Step 3: SET AVAIL = AVAIL -> NEXT
• Step 4: SET New_Node -> DATA = VAL
• Step 5: SET PTR = START
• Step 6: Repeat step 7 while PTR -> NEXT != START
• Step 7: PTR = PTR -> NEXT
• Step 8: Set New_Node -> NEXT = START
• Step 9: SET PTR -> NEXT = New_Node
• Step 10: SET START = New_Node
9/14/2023 62

• Step 11: EXIT


Example: Insert at the beginning
(CLL)

9/14/2023 63
Algorithm: Insert at the end (CLL)
• Step 1: IF AVAIL = NULL, then
• Write OVERFLOW
• Go to step 7
• [END OF IF]

• Step 2: SET New_Node = AVAIL


• Step 3: SET AVAIL = AVAIL -> NEXT
• Step 4: SET New_Node -> DATA = VAL
• Step 5: SET New_Node -> Next = START
• Step 6: SET PTR = START
• Step 7: Repeat step 8 while PTR -> NEXT != START
• Step 8: SET PTR = PTR -> NEXT
• [END OF LOOP}
• Step 9: SET PTR -> NEXT = New_Node
9/14/2023 64

• Step 10: EXIT


Example: Insert at the end (CLL)
• Take pointers PTR initially point to START
• START, PTR
1 7 3 4

• Move PTR so that it points to the last node of the

• START
1 7 3 PTR
4

• Add the new node after the node pointed by PTR

• START
1 7 3 PTR
4 2

9/14/2023 65
Algorithm: Delete the first node
(CLL)
• Step 1: IF START= NULL, then
• Write UNDERFLOW
• Go to step 8
• [END OF IF]

• Step 2: SET PTR = START


• Step 3: Repeat step 4 while PTR -> NEXT != START
• Step 4: SET PTR = PTR -> NEXT
• [END OF IF]

• Step 5: SET PTR -> NEXT = START -> NEXT


• Step 6: FREE START
• Step 7: SET START = PTR -> NEXT
• Step 8: EXIT
9/14/2023 66
Example: Delete the first node
(CLL)
• START, PTR
1 7 3 4 2

• Move PTR further so that it now points to the last node of the list

• START PTR
1 7 3 4 2

• The NEXT part of the PTR is made to point to the second node of the list and the memory of the first
node is freed. The second node becomes the first node of the list.
• START PTR
7 3 4 2

9/14/2023 67
Algorithm: Delete the last node
(CLL)
• Step 1: IF START= NULL, then
• Write UNDERFLOW
• Go to step 8
• [END OF IF]

• Step 2: SET PTR = START


• Step 3: Repeat step 4 while PTR -> NEXT != START
• Step 4: SET PREPTR = PTR
• Step 5: SET PTR = PTR -> NEXT
• [END OF IF]

• Step 6: SET PREPTR -> NEXT = START


• Step 7: FREE PTR
• Step 8: EXIT
9/14/2023 68
Example: Delete the last node (CLL)

• Take two pointers PREPTR and PTR initially point to START


• START, PREPTR, PTR
• PTR
1 7 3 4 2

• Move the PTR so that it points to the last node of the list and PREPTR will always point to the node
preceding PTR
• START PREPTR PTR
1 7 3 4 2

• Make the PREPTR’s next part store START node’s address and free the space allocated for
PTR. Now PREPTR is the last node of the list
• START PREPTR

1 7 3 4
9/14/2023 69
Doubly Linked List

• Two-way linked list contains a pointer to the next as well as the


pervious node

HEAD

X3 3 5 7 8 X

9/14/2023 70
Doubly Linked List

• Two-way linked list contains a pointer to the next as well as the


pervious node

HEAD

X 3 5 7 8 X

Struct node
{ 3
struct node * prev;
int data;
Struct node * next;
};
9/14/2023 71
Doubly Linked List
• Struct node
•{
• struct node * prev;
• int data;
• Struct node * next;
• };

9/14/2023 72
Circular Doubly Linked List (CDLL)
• Circular Two-way list contains a pointer to the next as well as the
previous node in the sequence

HEAD

3 5 7 8

9/14/2023 73
Polynomial representation

•7x4 + 7x3 + 9x 2 + 5x + 1

9/14/2023 74
Polynomial representation
• A Polynomial: 7x4 + 7x3 + 9x2 + 5x + 1

• Coefficients: 7, 7, 9, 5, 1
• Powers: 4, 3, 2, 1, 0

7 4 7 3 9 2 5 1 1 0

9/14/2023 75

You might also like