Data Structures Unit 1
Data Structures Unit 1
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.
Non-primitive data structures refer to all those data structures that are derived from
one or more primitive data structures. The objective of creating non-primitive data structures
is to form sets of homogeneous or heterogeneous data elements.
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
Linear Non-linear
Integer Real Character Boolean Data Structures 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
2.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.
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.
Data Next
Element Pointer
Node
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.
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.
Searching is the process of traversing an array to find out if a specific element is present
in the array or not. If the search is successful, the index location of the element is returned.
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.
The sorting operation arranges the elements of an array in a specific order or sequence.
Sorting involves comparing the array elements with each other and shuffling them until all the
elements are sorted.
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.
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);
break;
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?
4.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.
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.
4.5 FIND
4.5.1 Find
Example
Find(List, 30)
Position
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
4.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.
4.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
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
4.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
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
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
4.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
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
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
32 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College
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
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;
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;
}
}
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
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;
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.
42 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College
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.
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.
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.
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.
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.
Routine
Example
DeleteMid(List, 30)
Position TempNode
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);
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 element : 20
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.
Previous Data
Next Pointer
Pointer Element
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
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.
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 Step 10.
Step 9 : Display “List is Empty”.
Step 10: Stop.
68 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College
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.
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 69
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.
70 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College
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;
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 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
7.1 INTRODUCTION
We can define an abstract data type for single-variable polynomials (with nonnegative
exponents) by using a list. We could then write routines to perform addition, subtraction,
multiplication, differentiation, and other operations on these polynomials.
struct poly
{
int coeff;
int pow;
struct poly *Next;
};
typedef struct poly Poly;
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);
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);
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
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.
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.
1. What is multilist?