2 Link
2 Link
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
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)
1 First element
2 Second element
list
Last Last element
9/14/2023 8
Array Implementation (List)
9/14/2023 9
List
9/14/2023 10
Pointer Implementation (List)
9/14/2023 11
Linked List
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
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
9/14/2023 15
List
• Linked List in C (with pointers)
HEAD
100
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
933
• Step 2
B 542
933
• Step 3
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
9/14/2023 20
Why Linked list is better?
9/14/2023 21
Complexity
• Print List: Start with the first element and then follow the pointers.
O(N)
9/14/2023 22
Singly linked list: Traversing
• Algorithm for traversing a linked list
• Step 5: EXIT
9/14/2023 23
Singly linked list: Printing
• Algorithm to print each node of the linked list
• Step 5: EXIT
9/14/2023 24
Algorithm: Searching an unsorted linked list
9/14/2023 25
Example: Searching an unsorted linked list
9/14/2023 26
Algorithm: Searching a sorted linked list
9/14/2023 27
Insertion a new node in a linked list
9/14/2023 28
Example: Insertion at the beginning
9/14/2023 29
Algorithm: Insert a new node at the beginning
9/14/2023 30
Example: Insertion at the end
9/14/2023 31
Algorithm: Insert a new node at the end
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]
9/14/2023 34
Example: Insert before a node
9/14/2023 35
Algorithm: Insert a new node before a node
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]
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
9/14/2023 42
Example: Deleting after a given node
9/14/2023 43
Algorithm : Deleting after a given node
9/14/2023 45
Algorithm: Deleting from a sorted link list
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
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
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]
• START
1 7 3 PTR
4
• 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]
• 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]
• 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
HEAD
X3 3 5 7 8 X
9/14/2023 70
Doubly Linked List
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