CS23231 DS Unit I Reg - 2023
CS23231 DS Unit I Reg - 2023
Unit - I
Lecture Notes
(Regulations - 2023)
UNIT-I LINEAR DATA STRUCTURES – LIST 9
Self-Referential Structures, Dynamic Memory Allocation, Linked list implementation - Singly Linked List,
Doubly Linked List, Circular Linked List, Applications of List.
Self-referential structure
The self-referential structure is a structure that points to the same type of structure. It contains one or
more pointers that ultimately point to the same structure.
Self-referential structure plays a vital role in the linked list, trees, graphs, and many more data
structures.
By using the structure, we can easily implement these data structures efficiently. For defining a particular
node, the structure plays a very important role.
In a linked list, the structure is used to store the data information and the address of the next node in
the given linked list. Mainly the data part is of integer type, and the link address part is of pointer type,
which holds the address of the next node, so that it can for the Link ultimately.
Here in the linked list, we will define a common structure, which contains the data and address pointer
to carry the address of the next node, so to form the multiple nodes, we from each node using the node
structure.
Unlike a static data structure such as an array where the size of the array limits the number of elements
that can easily to inserted into the array, a self-referential structure can dynamically be expanded or
contracted.
Operations like the insertion or deletion of nodes in a self-referential structure involve straightforward
alteration of pointers.
Linked list
o A linked list is a useful data storage method, and it is very easy to implement it in C programming
Language.
o Several kinds of linked lists, including single linked lists, double linked lists, and binary trees.
o Each type is suited for certain types of data storage.
o The one thing that these lists have in common is that the links between data items are defined by
the information contained in the items themselves, in the form of pointers.
o The linked list is distinctly different from arrays, in which the links between data items result from
the layout and storage of the array.
Here, we will discuss the self-referential structure in more detail using the linked list concepts.
Let's understand the concept of self-referential structure in more detail using the example mentioned
below:
In a singly linked list, there is only a single pointer that carries the address of the next node, and that
pointer variable is of the structure node type itself.
It is mainly used when we want to store the different data items by fetching out them from various
addresses and connecting all of them by storing the address of one data item into the linked part of the
other node. In this way, we can easily connect all the data items by using these connecting links.
Let's look at the working of single link self-referential structure with the help of an example:
36
24
As the name suggests, we can easily predict that these types of the structure consist of multiple Link,
here we will make use of two pointer links of structure type which is pointing to the same structure, to
understand it better, we can connect this concept with a doubly-linked list.
In a doubly-linked list, two single pointers carry the address of the next node and the previous node, and
that pointer variable is of the structure node type itself.
It is mainly used when we want to store the different data items by fetching out them from various
addresses and connecting all of them by storing the address of one data item into the linked part of the
other node; in this way, we can easily connect all the data items by using these connecting links.
Let us look at the working of, multiple link self-referential structure with the help of an example:
15 20 25
15 20 25
15 20 25
Dynamic memory allocation in C
The concept of dynamic memory allocation in c language enables the C programmer to allocate memory
at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.
1. malloc()
2. calloc()
3. realloc()
4. free()
memory can't be increased while executing memory can be increased while executing
program. program.
malloc() function in C
1. ptr=(cast-type*)malloc(byte-size)
Test it Now
Let's see the example of malloc() function.
1. #include<stdio.h>
2. #include<stdlib.h>
3. int main(){
4. int n,i,*ptr,sum=0;
5. printf("Enter number of elements: ");
6. scanf("%d",&n);
7. ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
8. if(ptr==NULL)
9. {
10. printf("Sorry! unable to allocate memory");
11. exit(0);
12. }
13. printf("Enter elements of array: ");
14. for(i=0;i<n;++i)
15. {
16. scanf("%d",ptr+i);
17. sum+=*(ptr+i);
18. }
19. printf("Sum=%d",sum);
20. free(ptr);
21. return 0;
22. }
Output
calloc() function in C
1. ptr=(cast-type*)calloc(number, byte-size)
Test it Now
Let's see the example of calloc() function.
1. #include<stdio.h>
2. #include<stdlib.h>
3. int main(){
4. int n,i,*ptr,sum=0;
5. printf("Enter number of elements: ");
6. scanf("%d",&n);
7. ptr=(int*)calloc(n,sizeof(int)); //memory allocated using calloc
8. if(ptr==NULL)
9. {
10. printf("Sorry! unable to allocate memory");
11. exit(0);
12. }
13. printf("Enter elements of array: ");
14. for(i=0;i<n;++i)
15. {
16. scanf("%d",ptr+i);
17. sum+=*(ptr+i);
18. }
19. printf("Sum=%d",sum);
20. free(ptr);
21. return 0;
22. }
Output
realloc() function in C
If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by realloc() function. In
short, it changes the memory size.
1. ptr=realloc(ptr, new-size)
Test it Now
free() function in C
The memory occupied by malloc() or calloc() functions must be released by calling free() function.
Otherwise, it will consume memory until program exit.
1. free(ptr)
CHAPTER - 1 - INTRODUCTION TO DATA STRUCTURES
1.1 INTRODUCTION
Data structure is the way of organizing and storing data in a computer system so that
it can be used efficiently.
The following points highlight the need of data structures in computer science:
1. Compiler design
2. Operating system
3. Database management system
4. Statistical analysis package
5. Numerical analysis
6. Graphics
7. Artificial intelligence
8. Simulation
Primitive
Non-primitive
Primitive data structures include all the fundamental data structures that can be directly
manipulated by machine level instructions.
Integer
Character
Real
Boolean, etc.
Linear
Non-linear
In linear data structures, all the data elements are arranged in a linear or sequential
fashion. Examples of linear data structures include:
Arrays
Stacks
Queues
Linked lists, etc.
Trees
Graphs
Data Structures
Arrays Trees
Stacks
Queues
An abstract data type (ADT) is a set of objects together with a set of operations.
Abstract data types are mathematical abstractions. Objects such as lists, sets, and graphs,
along with their operations, can be viewed as abstract data types.
List is an ordered set of elements. For any list except the empty list, we say that A i+1
follows (or succeeds) Ai (i<N) and that Ai-1 precedes Ai (i>1). The first element of the list is
A1, and the last element is AN.
Some popular operations are printList, makeEmpty, find, insert, remove, findKth,
next, previous.
Array
Linked list
1.5 ARRAYS
An array implementation allows print_list and find to be carried out in linear time,
which is as good as can be expected, and the find_kth operation takes constant time.
However, insertion and deletion are expensive. For example, inserting at position 0 (which
amounts to making a new first element) requires first pushing the entire array down one
spot to make room, whereas deleting the first element requires shifting all the elements in
the list up one, so the worst case of these operations is O(n). On average, half the list needs
to be moved for either operation, so linear time is still required. Merely building a list by n
successive inserts would require quadratic time.
Because the running time for insertions and deletions is so slow and the list size
must be known in advance, simple arrays are generally not used to implement lists.
The linked list consists of a series of structures, which are not necessarily adjacent in
memory. Each structure contains the element and a pointer to a structure containing its
successor. We call this the next pointer. The last cell's next pointer points to NULL.
Apart from the advantages, linked lists also possess certain limitations, which are:
Depending on the manner in which its nodes are interconnected with each other,
linked lists are categorized into the following types:
1. What is an Abstract Data Type (ADT)? (or) Define Abstract Data Type (ADT). (or) Define
ADT and give an example. (or) What do you mean by abstract data type? (or) What is an
abstract data type? Give any two examples. (or) Give an example for abstract data type.
(or) Define abstract data type. List out few.
2. What is advantage of an ADT?
3. Define a 'list'; Mention any two operations that are performed on a list. (or) Define
List Abstract Data Type with example.
4. List out the operations of the list ADT. (or) Which operations are supported by the list ADT?
5. What are the different ways to implement list?
6. What are the advantages of linked list over arrays?
7. What are the disadvantages of linked list?
8. List the various types of linked lists.
CHAPTER - 3 - ARRAY BASED IMPLEMENTATION OF LIST
3.1 INTRODUCTION
Insert
Delete
Traversal
Sorting
Searching
3.3 INSERTION
Example
Insert(a[], p, e)
Step 1 : Start.
Step 2 : Set i =
n.
Step 3 : Repeat Steps 4 to 5 while i >=
p. Step 4 : Set a[i+1] = a[i].
Step 5 : Set i = i – 1.
Step 6 : Set a[p] = e.
Step 7 : Set n = n +
1. Step 8 : Stop.
3.4 DELETION
Deletion is the task of removing an element from the array. The deletion of element
from the end is quite simple and can be achieved by mere updation of index identifier.
However, to remove an element from the middle, one must move all the elements present
to the right of the point of deletion, one position to the left.
Example
Delete(a[], p)
Step 1 : Start.
Step 2 : Set i =
p.
Step 3 : Repeat Steps 4 to 5 while i <
n. Step 4 : Set a[i] = a[i+1].
Step 5 : Set i = i +
1. Step 6 : Set n = n
- 1. Step 7 : Stop.
3.5 TRAVERSAL
While working with arrays, it is often required to access the array elements; that is,
reading values from the array. This is achieved with the help of array traversal. It involves
visiting the array elements and storing or retrieving values from it.
Some of the typical situations where array traversal may be required are:
Traverse(a[])
Step 1 : Start.
Step 2 : Set i =
0.
Step 3 : Repeat Steps 4 to 5 while i <
n. Step 4 : Access a[i].
Step 5 : Set i = i + 1.
Step 6 : Stop.
Example
Search(a[], e)
Step 1 : Start.
Step 2 : Set i =
0.
Step 3 : Repeat Steps 4 to 6 while i < n.
Step 4 : if e = a[i] goto Step 5 else goto Step
6. Step 5 : Set flag = 1 and goto Step 7.
Step 6 : Set i = i + 1.
Step 7 : if flag = 1 goto Step 8 else goto Step
9. Step 8 : Print i and goto step 10.
Step 9 : Print i.
Step 10: Print “Unsuccesful.”.
Step 11: Stop.
Example
Sort(a[])
Step 1 : Start.
Step 2 : Set i =
0.
Step 3 : Repeat Steps 4 to 11 while i < n-1.
Step 4 : Set j = i+1.
Step 5 : Repeat Steps 6 to 10 while j < n.
Step 6 : if a[i] > a[j] goto Step 7 else goto Step
10. Step 7 : Set t = a[i].
Step 8 : Set a[i] =
a[j]. Step 9 : Set a[j]
= t.
Step 10: Set j = j +
1. Step 11: Set i = i
+ 1. Step 12: Stop.
Routine to Sort the elements in an Array
3.8 PROGRAM
#include <stdio.h>
int n = 0;
int main()
{
int a[5], ch, e, p;
printf("1.Insert \n2.Delete \n3.Search"); printf("\
n4.Traverse \n5.Sort \n6.Exit\n"); do
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Enter the position : ");
scanf("%d", &p);
printf("Enter the element : ");
scanf("%d", &e);
Insert(a, p, e);
case 2:
printf("Enter the position : ");
scanf("%d", &p);
Delete(a, p); break;
case 3:
printf("Enter the element : ");
scanf("%d", &e);
Search(a, e); break;
case 4:
printf("The elements are : ");
Traverse(a);
break;
case 5:
Sort(a);
break;
}
} while(ch <= 5);
return 0;
}
OUTPUT
1.Insert
2.Delete
3.Search
4.Traverse
5.Sort 6.Exit
1. What is an array?
2. What are the two basic operations on arrays?
3. What are the limitations of list implemented by array?
CHAPTER - 4 - LINKED LIST IMPLEMENTATION - SINGLY LINKED LISTS
1.1 INTRODUCTION
A singly linked list is a linked list in which each node contains only one link field
pointing to the next node in the list.
struct node
{
int Element;
struct node *Next;
};
typedef struct node Node;
Example
Algorithm
IsEmpty(List)
Step 1 : Start.
Step 2 : If ListNext = NULL goto Step 3 else goto Step
4. Step 3 : Return 1 and Stop.
Step 4 : Return 0.
Step 5 : Stop.
Routine
Example
Position
Fig. 4.4 Current Position is the Last in a Linked List
Algorithm
IsLast(Position)
Step 1 : Start.
Step 2 : If PositionNext = NULL goto Step 3 else goto Step
4. Step 3 : Return 1 and Stop.
Step 4 : Return 0.
Step 5 : Stop.
1.5 FIND
1.5.1 Find
Example
Find(List, 30)
Position
Algorithm
Find(List, x)
Step 1 : Start.
Step 2 : Set Position = ListNext.
Step 3 : Repeat the Step 4 until Position != NULL and PositionElement != x.
Step 4 : Set Position = PositionNext.
Step 5 : Return Position.
Step 6 : Stop.
Routine
1.5.2 FindPrevious
Example
FindPrevious(List, 30)
Position X
Algorithm
FindPrevious(List, x)
Step 1 : Start.
Step 2 : Set Position = List.
Step 3 : Repeat the Step 4 until PositionNext != NULL and PositionNextElement
!= x.
Step 4 : Set Position = PositionNext.
Step 5 : Return Position.
Step 6 : Stop.
Routine
1.5.3 FindNext
Example
FindNext(List, 20)
X, Position PositionNext
Algorithm
FindNext(List, x)
Step 1 : Start.
Step 2 : Set Position = Find(List, x).
Step 3 : Return PositionNext.
Step 4 : Stop.
Routine
Example
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College
Algorithm
Traverse(List)
Step 1 : Start.
Step 2 : If !IsEmpty = TRUE goto Step 3 else goto Step
8. Step 3 : Set Position = List.
Step 4 : Repeat the Steps 5-6 until PositionNext != NULL.
Step 5 : Set Position = PositionNext.
Step 6 : Display PositionElement.
Step 7 : Goto Step 9.
Step 8 : Display “List is
empty”. Step 9 : Stop.
Routine
1.7 INSERT
The insert command requires obtaining a new cell from the system by using an malloc
call and then executing two pointer maneuvers.
We will pass an element to be inserted along with the list L and a position P. Our
particular insertion routine will insert an element after the position implied by P. This
decision is arbitrary and meant to show that there are no set rules for what insertion does.
It is quite possible to insert the new element into position P (which means before the
element currently in position p), but doing this requires knowledge of the element before
position P. This could be obtained by a call to Find.
Insertion
Example
The general idea is shown in Figure. The dashed line represents the old pointer.
InsertBeg(List, 5)
NewNode
Algorithm
InsertBeg(List, e)
Step 1 : Start.
Step 2 : Set NewNode = addressof(Node).
Step 3 : Set NewNodeElement = e.
Step 4 : If List = NULL, then goto Step 5 else goto Step
6. Step 5 : Set NewNodeNext = NULL and goto Step 7.
Step 6 : Set NewNodeNext = ListNext.
Step 7 : Set ListNext = NewNode.
Step 8: Stop.
Routine
Example
InsertLast(List, 45)
Position NewNode
Algorithm
InsertLast(List, e)
Step 1 : Start.
Step 2 : Set NewNode = addressof(Node).
Step 3 : Set NewNodeElement = e.
Step 4 : Set NewNodeNext = NULL.
Step 5 : If List = NULL, then goto Step 6 else goto Step
7. Step 6 : Set ListNext = NewNode and goto Step 11.
Step 7 : Set Position = List.
Step 8 : Repeat the Step 9 until PositionNext != NULL.
Step 9 : Set Position = PositionNext.
Step 10: Set PositionNext = NewNode.
Step 11: Stop.
Routine
Example
InsertMid(List, 20, 25)
NewNode
Position
Algorithm
InsertMid(List, p, e)
Step 1 : Start.
Step 2 : Set NewNode = addressof(Node).
Step 3 : Set Position = Find(List, p).
Step 4 : Set NewNodeElement = e.
Step 5 : Set NewNodeNext = PositionNext.
Step 6 : Set PositionNext = NewNode.
Step 7 : Stop.
Routine
1.8 DELETE
The delete command can be executed in one pointer change. Our routine will delete
some element X in list L. We need to decide what to do if x occurs more than once or not at
all. Our routine deletes the first occurrence of x and does nothing if x is not in the list. To
do this, we find p, which is the cell prior to the one containing x, via a call to FindPrevious.
Example
DeleteBeg(List)
TempNode
Algorithm
DeleteBeg(List, e)
Step 1 : Start.
Step 2 : If !IsEmpty = True, then goto Step 3 else goto Step
7. Step 3 : Set TempNode = ListNext.
Step 4 : Set ListNext = TempNodeNext.
Step 5 : Display the TempNodeElement.
Step 6 : Delete TempNode and goto Step 8.
Step 7 : Display “List is Empty”.
Step 8: Stop.
Routine
Example
DeleteEnd(List)
Position TempNode
Algorithm
DeleteEnd(List)
Step 1 : Start.
Step 2 : If !IsEmpty = True, then goto Step 3 else goto Step
10. Step 3 : Set Position = List.
Step 4 : Repeat the Step 5 until PositionNext != NULL.
Step 5 : Set Position = PositionNext.
Step 6 : Set TempNode = PositionNext.
Step 7 : Set PositionNext = NULL.
Step 8 : Display TempNodeElement.
Step 9 : Delete TempNode and goto Step
11. Step 10: Display “List is Empty”.
Step 11: Stop.
Routine
Example
DeleteMid(List, 30)
TempNode
Position
Algorithm
DeleteMid(List, e)
Step 1 : Start.
Step 2 : If !IsEmpty = True, then goto Step 3 else goto Step
9. Step 3 : Set Position = FindPrevious(List, e).
Step 4 : If !Islast(Position) = True, then goto Step 5 else goto Step 10.
Step 5 : Set TempNode = PositionNext.
Step 6 : Set PositionNext = TempNodeNext.
Step 7 : Display the TempNodeElement.
Step 8 : Delete TempNode and goto Step
10. Step 9 : Display “List is Empty”.
Step 10: Stop.
Routine
}
T Node = Position->Next; Position->Next =
e TempNode->Next;
m printf("The deleted item is %d\n", TempNode->Element);
p free(TempNode);
printf("List is empty...!\n");
}
4.9. PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct node
{
int Element;
struct node *Next;
};
typedef struct node Node;
int main()
{
Node *List = malloc(sizeof(Node)); List-
>Next = NULL;
Node *Position;
int ch, e, p;
printf("1.Insert Beg \n2.Insert Middle \n3.Insert End"); printf("\n4.Delete Beg \
n5.Delete Middle \n6.Delete End"); printf("\n7.Find \n8.Traverse \n9.Exit\n");
do
{
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Enter the element : ");
scanf("%d", &e); InsertBeg(List, e);
break;
case 2:
printf("Enter the position element : ");
scanf("%d", &p);
printf("Enter the element : ");
scanf("%d", &e); InsertMid(List, p, e);
break;
case 3:
printf("Enter the element : ");
scanf("%d", &e); InsertLast(List, e);
break;
case 4:
DeleteBeg(List);
break;
case 5:
printf("Enter the element : ");
scanf("%d", &e); DeleteMid(List, e);
break;
case 6:
DeleteEnd(List);
break;
case 7:
printf("Enter the element : ");
scanf("%d", &e); Position =
Find(List, e); if(Position !=
NULL)
printf("Element found...!\n");
else
printf("Element not found...!\n");
break;
case 8:
Traverse(List);
break;
}
} while(ch <= 8);
return 0;
}
if(IsEmpty(List))
NewNode->Next = NULL;
else
NewNode->Next = List->Next;
List->Next = NewNode;
}
if(IsEmpty(List))
List->Next = NewNode;
else
{
Position = List;
while(Position->Next != NULL) Position =
Position->Next;
Position->Next = NewNode;
}
}
1. Insert Beg
2. Insert Middle
3. Insert End
4. Delete Beg
5. Delete Middle
6. Delete End
7.Find
8.Traverse
9.Exit
Enter your choice : 1
Enter the element : 40
Enter your choice : 1
Enter the element : 30
Enter your choice : 1
Enter the element : 20
Enter your choice : 1
Enter the element : 10
Enter your choice : 8
10 20 30 40
Enter your choice : 7
Enter the element : 30
Element found...!
Enter your choice : 1
Enter the element : 5
Enter your choice : 8
5 10 20 30 40
Enter your choice : 3
Enter the element : 45
Enter your choice : 8
5 10 20 30 40 45
Enter your choice : 2
Enter the position elem ent : 20
Enter the element : 25
Enter your choice : 8
5 10 20 25 30 40 45
Enter your choice : 4
The deleted item is 5
Enter your choice : 8
10 20 25 30 40 45
Enter your choice : 6
The deleted item is 45
Enter your choice : 8
10 20 25 30 40
Enter your choice : 5
Enter the element : 30
The deleted item is 30
Enter your choice : 8
10 20 25 40
Enter your choice : 9
REVIEW QUESTIONS
5.1 INTRODUCTION
A popular convention is to have the last cell keep a pointer back to the first. This can
be done with or without a header (if the header is present, the last cell points to it), and
can also be done with doubly linked lists (the first cell's previous pointer points to the last
cell). This clearly affects some of the tests, but the structure is popular in some applications.
In circular linked list the pointer of the last node points to the first node. Circular
linked list can be implemented as singly linked list and doubly linked list with or without
headers.
5.2 IMPLEMENTATION
A singly linked circular list is a linked list in which the last node of the list points to the
first node.
struct node
{
int Element;
struct node *Next;
};
typedef struct node Node;
5.4. EMPTY SINGLY LINKED CIRCULAR LIST WITH HEADER
Example
Algorithm
IsEmpty(List)
Step 1 : Start.
Step 2 : If ListNext = LIST goto Step 3 else goto Step 4.
Step 3 : Return 1 and Stop.
Step 4 : Return 0.
Step 5 : Stop.
Routine
Example
Position
Fig. 5.4 Current Position is the Last in a Singly Linked Circular List
Algorithm
IsLast(Position, List)
Step 1 : Start.
Step 2 : If PositionNext = List goto Step 3 else goto Step 4.
Step 3 : Return 1 and Stop.
Step 4 : Return 0.
Step 5 : Stop.
5.6 FIND
5.6.1 Find
Example
Find(List, 30)
Position
Algorithm
Find(List, x)
Step 1 : Start.
Step 2 : Set Position = ListNext.
Step 3 : Repeat the Step 4 until Position != List and PositionElement !=
x. Step 4 : Set Position = PositionNext.
Step 5 : Return Position.
Step 6 : Stop.
Routine
Example
FindPrevious(List, 30)
Position X
Algorithm
FindPrevious(List, x)
Step 1 : Start.
Step 2 : Set Position = List.
Step 3 : Repeat the Step 4 until PositionNext != List and
PositionNextElement!=x.
Step 4 : Set Position = PositionNext.
Step 5 : Return Position.
Step 6 : Stop.
Routine
Example
FindNext(List, 20)
X, Position PositionNext
Algorithm
FindNext(List, x)
Step 1 : Start.
Step 2 : Set Position = Find(List, x).
Step 3 : Return PositionNext.
Step 4 : Stop.
Routine
Example
Algorithm
Traverse(List)
Step 1 : Start.
Step 2 : If !IsEmpty = TRUE goto Step 3 else goto Step
8. Step 3 : Set Position = List.
Step 4 : Repeat the Steps 5-6 until PositionNext != List.
Step 5 : Set Position = PositionNext.
Step 6 : Display PositionElement.
Step 7 : Goto Step 9.
Step 8 : Display “List is
empty”. Step 9 : Stop.
Routine
5.8 INSERT
The insert command requires obtaining a new cell from the system by using an malloc
call and then executing two pointer maneuvers.
We will pass an element to be inserted along with the list L and a position P. Our
particular insertion routine will insert an element after the position implied by P. This
decision is arbitrary and meant to show that there are no set rules for what insertion does.
It is quite possible to insert the new element into position P (which means before the
element currently in position p), but doing this requires knowledge of the element before
position P. This could be obtained by a call to Find.
Insertion
Example
The general idea is shown in Figure. The dashed line represents the old pointer.
InsertBeg(List, 5)
NewNode
Algorithm
InsertBeg(List, e)
Step 1 : Start.
Step 2 : Set NewNode = addressof(Node).
Step 4 : Set NewNodeElement = e.
Step 5 : Set NewNodeNext = ListNext.
Step 6 : Set ListNext = NewNode.
Step 7: Stop.
Routine
Example
InsertLast(List, 45)
Position NewNode
Algorithm
InsertLast(List, e)
Step 1 : Start.
Step 2 : Set NewNode = addressof(Node).
Step 3 : Set NewNodeElement = e.
Step 4 : If List = NULL, then goto Step 5 else goto Step
7. Step 5 : Set NewNodeNext = List.
Step 6 : Set ListNext = NewNode and goto Step 12.
Step 7 : Set Position = List.
Step 8 : Repeat the Step 9 until PositionNext != List.
Step 9 : Set Position = PositionNext.
Step 10: Set PositionNext = NewNode.
Step 11: Set NewNodeNext = List.
Step 12: Stop.
Routine
Example
Algorithm
InsertMid(List, p, e)
Step 1 : Start.
Step 2 : Set NewNode = addressof(Node).
Step 3 : Set Position = Find(List, p).
Step 4 : Set NewNodeElement = e.
Step 5 : Set NewNodeNext = PositionNext.
Step 6 : Set PositionNext = NewNode.
Step 7 : Stop.
Routine
5.9 DELETE
The delete command can be executed in one pointer change. Our routine will delete
some element X in list L. We need to decide what to do if x occurs more than once or not at
all. Our routine deletes the first occurrence of x and does nothing if x is not in the list. To
do this, we find p, which is the cell prior to the one containing x, via a call to FindPrevious.
Example
DeleteBeg(List)
TempNode
Algorithm
DeleteBeg(List, e)
Step 1 : Start.
Step 2 : If !IsEmpty = True, then goto Step 3 else goto Step
7. Step 3 : Set TempNode = ListNext.
Step 4 : Set ListNext = TempNodeNext.
Step 5 : Display the TempNodeElement.
Step 6 : Delete TempNode and goto Step 8.
Step 7 : Display “List is Empty”.
Step 8: Stop.
Routine
Example
DeleteEnd(List)
Position TempNode
Algorithm
DeleteEnd(List)
Step 1 : Start.
Step 2 : If !IsEmpty = True, then goto Step 3 else goto Step
10. Step 3 : Set Position = List.
Step 4 : Repeat the Step 5 until PositionNext != List.
Step 5 : Set Position = PositionNext.
Step 6 : Set TempNode = PositionNext.
Step 7 : Set PositionNext = List.
Step 8 : Display TempNodeElement.
Step 9 : Delete TempNode and goto Step
11. Step 10: Display “List is Empty”.
Step 11: Stop.
Routine
Example
DeleteMid(List, 30)
Position TempNode
Algorithm
DeleteMid(List, e)
Step 1 : Start.
Step 2 : If !IsEmpty = True, then goto Step 3 else goto Step
9. Step 3 : Set Position = FindPrevious(List, e).
Step 4 : If !Islast(Position, List) = True, then goto Step 5 else goto Step
10. Step 5 : Set TempNode = PositionNext.
Step 6 : Set PositionNext = TempNodeNext.
Step 7 : Display the TempNodeElement.
Step 8 : Delete TempNode and goto Step
10. Step 9 : Display “List is Empty”.
Step 10: Stop.
Routine
5.10 PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct node
{
int Element;
struct node *Next;
};
typedef struct node Node;
int main()
{
Node *List = malloc(sizeof(Node)); List-
>Next = List;
Node *Position;
int ch, e, p;
printf("1.Insert Beg \n2.Insert Middle \n3.Insert End"); printf("\n4.Delete Beg \
n5.Delete Middle \n6.Delete End"); printf("\n7.Find \n8.Traverse \n9.Exit\n");
do
{
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Enter the element : ");
scanf("%d", &e); InsertBeg(List, e);
break;
case 2:
printf("Enter the position element : ");
scanf("%d", &p);
printf("Enter the element : ");
scanf("%d", &e); InsertMid(List, p, e);
break;
case 3:
printf("Enter the element : ");
scanf("%d", &e); InsertLast(List, e);
break;
case 4:
DeleteBeg(List);
break;
case 5:
printf("Enter the element : ");
scanf("%d", &e); DeleteMid(List, e);
break;
case 6:
DeleteEnd(List);
break;
case 7:
printf("Enter the element : ");
scanf("%d", &e); Position =
Find(List, e); if(Position != List)
printf("Element found...!\n");
else
printf("Element not found...!\n");
break;
case 8:
Traverse(List);
break;
}
} while(ch <= 8);
return 0;
}
OUTPUT
1. Insert Beg
2. Insert Middle
3. Insert End
4. Delete Beg
5. Delete Middle
6. Delete End
7.Find
8.Traverse
9.Exit
Enter your choice : 1
Enter the element : 40
Enter your choice : 1
Enter the element : 30
Enter your choice : 1
Enter the element : 20
Enter your choice : 1
Enter the element : 10
Enter your choice : 8
10 20 30 40
Enter your choice : 7
Enter the element : 30
Element found...!
Enter your choice : 1
Enter the element : 5
Enter your choice : 8
5 10 20 30 40
Enter your choice : 3
Enter the element : 45
Enter your choice : 8
5 10 20 30 40 45
Enter your choice : 2
Enter the position elem ent : 20
Enter the element : 25
Enter your choice : 8
5 10 20 25 30 40 45
Enter your choice : 4
The deleted item is 5
Enter your choice : 8
10 20 25 30 40 45
Enter your choice : 6
The deleted item is 45
Enter your choice : 8
10 20 25 30 40
Enter your choice : 5
Enter the element : 30
The deleted item is 30
Enter your choice : 8
10 20 25 40
Enter your choice : 9
REVIEW QUESTIONS
6.1 INTRODUCTION
A doubly linked list is a linked list in which each node has three fields namely data
field, next link (Next) and previous link (Prev). Next points to the successor node in the list
whereas Prev points to the predecessor node.
struct node
{
struct node *Prev; int
Element; struct node
*Next;
};
typedef struct node Node;
Example
Algorithm
IsEmpty(List)
Step 1 : Start.
Step 2 : If ListNext = NULL goto Step 3 else goto Step 4.
Step 3 : Return 1 and
Stop. Step 4 : Return 0
and Stop. Step 5 : Stop.
Routine
Example
Position
Fig. 6.5. Current Position is the Last in a Doubly Linked List
Algorithm
IsLast(Position)
Step 1 : Start.
Step 2 : If PositionNext = NULL goto Step 3 else goto Step
4. Step 3 : Return 1 and Stop.
Step 4 : Return 0 and
Stop. Step 5 : Stop.
6.5 FIND
Example
Find(List, 30)
Position
Algorithm
Find(List, x)
Step 1 : Start.
Step 2 : Set Position = ListNext.
Step 3 : Repeat the Step 4 until Position != NULL and PositionElement != x.
Step 4 : Set Position = PositionNext.
Step 5 : Return Position.
Step 6 : Stop.
Routine
Example
Algorithm
Traverse(List)
Step 1 : Start.
Step 2 : If !IsEmpty = TRUE goto Step 3 else goto Step
8. Step 3 : Set Position = List.
Step 4 : Repeat the Steps 5-6 until PositionNext != NULL.
Step 5 : Set Position = PositionNext.
Step 6 : Display PositionElement.
Step 7 : Goto Step 9.
Step 8 : Display “List is
empty”. Step 9 : Stop.
Routine
6.7 INSERT
Insertion
Example
The general idea is shown in Figure. The dashed line represents the old pointer.
InsertBeg(List, 5)
NewNode
Algorithm
InsertBeg(List, e)
Step 1 : Start.
Step 2 : Set NewNode = addressof(Node).
Step 3 : If List = NULL, then goto Step 4 else goto Step
8. Step 4 : Set NewNodeElement = e.
Step 5 : Set NewNodeNext = NULL.
Step 6 : Set NewNodePrev = List.
Step 7 : Set ListNext = NewNode and goto Step 13.
Step 8 : Set NewNodeElement = e.
Step 9 : Set NewNodeNext = ListNext.
Step 10: Set NewNodeNextPrev = NewNode.
Step 11: Set NewNodePrev = List.
Step 12: Set ListNext = NewNode.
Step 13: Stop.
Routine
Example
InsertLast(List, 45)
Position NewNode
Algorithm
InsertLast(List, e)
Step 1 : Start.
Step 2 : Set NewNode = addressof(Node).
Step 3 : If List = NULL, then goto Step 4 else goto Step
8. Step 4 : Set NewNodeElement = e.
Step 5 : Set NewNodeNext = NULL.
Step 6 : Set NewNodePrev = List.
Step 7 : Set ListNext = NewNode and goto Step 15.
Step 8 : Set Position = List.
Step 9 : Repeat the Step 10 until PositionNext !=
NULL. Step 10: Set Position = PositionNext.
Step 11: Set NewNodeElement = e.
Step 12: Set PositionNext = NewNode.
Step 13: Set NewNodePrev = Position.
Step 14: Set NewNodeNext = NULL.
Step 15: Stop.
Routine
Example
NewNode
Position
Algorithm
InsertMid(List, p, e)
Step 1 : Start.
Step 2 : Set NewNode = addressof(Node).
Step 3 : Set Position = Find(List, p).
Step 4 : Set NewNodeElement = e.
Step 5 : Set NewNodeNext = PositionNext.
Step 6 : Set PositionNextPrev = NewNode.
Step 7 : Set PositionNext = NewNode.
Step 8 : Set NewNodePrev = Position.
Step 9 : Stop.
Routine
6.8 DELETE
Example
DeleteBeg(List)
TempNode
Algorithm
DelBeg(List, e)
Step 1 : Start.
Step 2 : If !IsEmpty = True, then goto Step 3 else goto Step
9. Step 3 : Set TempNode = ListNext.
Step 4 : Set ListNext = TempNodeNext.
Step 5 : If ListNext != NULL, then goto Step 6 else goto Step
7. Step 6 : Set TempNodeNextPrev = List.
Step 7 : Display the TempNodeElement.
Step 8 : Delete TempNode and goto Step10.
Step 9 : Display “List is Empty”.
Step 10: Stop.
Routine
Example
DeleteEnd(List)
Position TempNode
Algorithm
DeleteEnd(List)
Step 1 : Start.
Step 2 : If !IsEmpty = True, then goto Step 3 else goto Step
10. Step 3 : Set Position = List.
Step 4 : Repeat the Step 5 until PositionNext != NULL.
Step 5 : Set Position = PositionNext.
Step 6 : Set TempNode = Position.
Step 7 : Set PositionPrevNext = NULL.
Step 8 : Display the TempNodeElement.
Step 9 : Delete TempNode and goto Step
11. Step 10: Display “List is Empty”.
Step 11: Stop.
Routine
Example
DeleteMid(List, 30)
TempNode
Position
Algorithm
DeleteMid(List, e)
Step 1 : Start.
Step 2 : If !IsEmpty = True, then goto Step 3 else goto Step
10. Step 3 : Set Position = Find (List, e).
Step 4 : If !Islast(Position) = True, then goto Step 5 else goto Step 11.
Step 5 : Set TempNode = Position.
Step 6 : Set PositionPrevNext = PositionNext.
Step 7 : Set PositionNextPrev = PositionPrev.
Step 8 : Display the TempNodeElement.
Step 9 : Delete TempNode and goto Step
11. Step 10: Display “List is Empty”.
Step 11: Stop.
Routine
6.9 PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *Prev; int
Element; struct node
*Next;
};
typedef struct node Node;
int main()
{
Node *List = malloc(sizeof(Node)); List-
>Prev = NULL;
List->Next = NULL;
Node *Position; int
ch, e, p;
printf("1.Insert Beg \n2.Insert Middle \n3.Insert End"); printf("\n4.Delete Beg \
n5.Delete Middle \n6.Delete End"); printf("\n7.Find \n8.Traverse \n9.Exit\n");
do
{
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Enter the element : ");
scanf("%d", &e); InsertBeg(List, e);
break;
case 2:
printf("Enter the position element : ");
scanf("%d", &p);
printf("Enter the element : ");
scanf("%d", &e); InsertMid(List, p, e);
break;
case 3:
printf("Enter the element : ");
scanf("%d", &e); InsertLast(List, e);
break;
case 4:
DeleteBeg(List);
break;
case 5:
printf("Enter the element : ");
scanf("%d", &e); DeleteMid(List, e);
break;
case 6:
DeleteEnd(List);
break;
case 7:
printf("Enter the element : ");
scanf("%d", &e); Position =
Find(List, e); if(Position !=
NULL)
printf("Element found...!\n");
else
printf("Element not found...!\n");
break;
case 8:
Traverse(List);
break;
}
} while(ch <= 8);
return 0;
}
{
Node *NewNode = malloc(sizeof(Node));
NewNode->Element = e; if(IsEmpty(List))
NewNode->Next = NULL;
else
{
NewNode->Next = List->Next;
NewNode->Next->Prev = NewNode;
}
NewNode->Prev = List;
List->Next = NewNode;
}
}
else
{
}
NewNode->prev = List;
List->Next = NewNode;
Position = List;
while(Position->Next != NULL) Position =
Position->Next;
Position->Next = NewNode;
NewNode->Prev = Position;
OUTPUT
1. Insert Beg
2. Insert Middle
3. Insert End
4. Delete Beg
5. Delete Middle
6. Delete End
7.Find
8.Traverse
9.Exit
Enter your choice : 1
Enter the element : 40
Enter your choice : 1
Enter the element : 30
Enter your choice : 1
Enter the element : 20
Enter your choice : 1
Enter the element : 10
Enter your choice : 8
10 20 30 40
Enter your choice : 7
Enter the element : 30
Element found...!
Enter your choice : 1
Enter the element : 5
Enter your choice : 8
5 10 20 30 40
Enter your choice : 3
Enter the element : 45
Enter your choice : 8
5 10 20 30 40 45
Enter your choice : 2
Enter the position element : 20
Enter the element : 25
Enter your choice : 8
5 10 20 25 30 40 45
Enter your choice : 4
The deleted item is 5
Enter your choice : 8
10 20 25 30 40 45
Enter your choice : 6
The deleted item is 45
Enter your choice : 8
10 20 25 30 40
Enter your choice : 5
Enter the element : 30
The deleted item is 30
Enter your choice : 8
10 20 25 40
Enter your choice : 9
REVIEW QUESTIONS
7.1 INTRODUCTION
struct poly
{
int coeff;
int pow;
struct poly *Next;
};
typedef struct poly Poly;
REVIEW QUESTIONS
8.1 EXAMPLE
8.2 ROUTINE
8.3 PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct poly
{
int coeff;
int pow;
struct poly *Next;
};
typedef struct poly Poly; void
Create(Poly *List); void
Display(Poly *List);
void Addition(Poly *Poly1, Poly *Poly2, Poly *Result);
int main()
{
Poly *Poly1 = malloc(sizeof(Poly)); Poly
*Poly2 = malloc(sizeof(Poly)); Poly *Result =
malloc(sizeof(Poly)); Poly1->Next = NULL;
Poly2->Next = NULL;
printf("Enter the values for first polynomial :\n"); Create(Poly1);
printf("The polynomial equation is : "); Display(Poly1);
printf("\nEnter the values for second polynomial :\n"); Create(Poly2);
printf("The polynomial equation is : "); Display(Poly2);
Addition(Poly1, Poly2, Result);
printf("\nThe polynomial equation addition result is : "); Display(Result);
return 0;
}
9.1 EXAMPLE
9.2 ROUTINE
9.3 PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct poly
{
int coeff;
int pow;
struct poly *Next;
};
typedef struct poly Poly; void
Create(Poly *List); void
Display(Poly *List);
void Subtraction(Poly *Poly1, Poly *Poly2, Poly *Result);
int main()
{
Poly *Poly1 = malloc(sizeof(Poly)); Poly
*Poly2 = malloc(sizeof(Poly)); Poly *Result =
malloc(sizeof(Poly)); Poly1->Next = NULL;
Poly2->Next = NULL;
printf("Enter the values for first polynomial :\n"); Create(Poly1);
printf("The polynomial equation is : "); Display(Poly1);
printf("\nEnter the values for second polynomial :\n"); Create(Poly2);
printf("The polynomial equation is : "); Display(Poly2);
Subtraction(Poly1, Poly2, Result);
printf("\nThe polynomial equation subtraction result is : "); Display(Result);
return 0;
}
NewNode->Next = NULL;
Position->Next = NewNode;
Position = NewNode;
}
OUTPUT
10.1 EXAMPLE
10.2 ROUTINE
10.3 PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct poly
{
int coeff;
int pow;
struct poly *Next;
};
typedef struct poly Poly;
Output
CHAPTER - 11 - MULTILISTS
11.1 INTRODUCTION
A university with 40,000 students and 2,500 courses needs to be able to generate two
types of reports. The first report lists the class registration for each class, and the second
report lists, by student, the classes that each student is registered for.
What is needed is a list for each class, which contains the students in the class. We
also need a list for each student, which contains the classes the student is registered for.
Figure shows our implementation.
Fig. 11.1 Multilist implementation for registration problem
As the figure shows, we have combined two lists into one. All lists use a header and
are circular.
To list all of the students in class C3, we start at C3 and traverse its list (by going
right). The first cell belongs to student S1. Although there is no explicit information to this
effect, this can be determined by following the student's linked list until the header is
reached. Once this is done, we return to C3's list (we stored the position we were at in the
course list before we traversed the student's list) and find another cell, which can be
determined to belong to S3. We can continue and find that S4 and S5 are also in this class. In
a similar manner, we can determine, for any student, all of the classes in which the student
is registered.
REVIEW QUESTIONS
1. What is multilist?
Applications of lists
In linear data structures, a list is one of the most fundamental types of data structures. A linear data
structure is one where elements are arranged sequentially, and each element is connected to its
previous and next element (if applicable). Lists, in this context, refer to both arrays (or dynamic arrays)
and linked lists.
Lists are used to store collections of data in an ordered manner, where each element has a
unique position (or index). This helps in efficiently managing, accessing, and modifying data.
Example: A list of employee IDs or names in a company.
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. Lists can be
used to implement a stack, where elements are added and removed from the same end (top).
Example: Undo operations in text editors or browser history can be implemented using a stack.
Operations:
o Push (add an element): Can be done using the append() method in a list.
o Pop (remove an element): Can be done using the pop() method in a list.
A queue is another linear data structure that follows the First In, First Out (FIFO) principle. Lists
can be used to implement a queue, where elements are added at the rear and removed from the
front.
Example: Print jobs in a queue, task scheduling in operating systems.
Operations:
o Enqueue (add an element): Can be done using the append() method.
o Dequeue (remove an element): Can be done using the pop(0) method (though this
operation is not as efficient in a list as in other data structures like deque).
A linked list is another type of linear data structure where each element (node) stores a
reference (pointer) to the next element in the sequence. This can be implemented using lists (or
nodes in languages that support pointers).
Example: Memory management, dynamic memory allocation.
Operations:
o Insertion and Deletion: Insertion and deletion can be done efficiently by adjusting
pointers (or modifying list elements) to add or remove nodes.
o Traversal: Iterating through the nodes in the linked list.
5. Dynamic Resizing of Data Collections
Lists are often used in situations where the size of the data collection may change over time (e.g.,
items being added or removed). In this case, dynamic arrays (like Python’s list) can resize
automatically.
Example: Storing elements of varying size or length, like a list of people attending a party where
guests can leave or arrive at different times.
Lists are essential for sorting algorithms (like Bubble Sort, Merge Sort, and Quick Sort) and
searching algorithms (like Linear Search and Binary Search when the list is sorted).
Example: Searching for a name in a contact list, sorting a list of ages.
Lists are used in the representation of graphs using adjacency lists. In an adjacency list
representation, each node in the graph has a list of its neighbors.
Example: Social networks (where nodes are people and edges are relationships).
8. Matrix Representation
Lists can be used to represent 2D matrices, where each list represents a row of the matrix. This
allows for efficient access and modification of elements.
Example: Storing a grid for a game or representing a chessboard.
9. Buffer/Memory Management
Lists can serve as buffers in low-level memory management or in real-time systems, where data
needs to be managed efficiently and accessed in a linear fashion.
Example: Circular buffers in embedded systems where data needs to be continuously stored and
read.
Lists can store intermediate results in recursive algorithms or backtracking problems. Lists allow
easy modification and access to the intermediate data.
Example: Storing partial solutions or paths in maze-solving algorithms or the N-Queens problem.
Lists are useful for processing or transforming data in algorithms that involve filtering, mapping,
or reducing data.
Example: Applying a function to each element in a list (e.g., converting a list of temperatures
from Celsius to Fahrenheit).
12. Sparse Data Representation
In cases where most of the data is empty or has default values (sparse data), lists are used for
efficient storage and manipulation.
Example: Sparse matrices in scientific computing or machine learning models, where most
elements are zeros.
Lists are often used for scenarios where efficient memory management is important, such as
managing large datasets that change dynamically (growth or shrinkage).
Example: A list of currently active users in a chat room or a list of tasks to be processed.