CS18302 Unit 02 Notes Full
CS18302 Unit 02 Notes Full
1.1 INTRODUCTION
Computer Problem Solving is an intricate process which needs careful planning,
logical precision, persistence and attention. It can be a challenging, exciting and satisfying
experience which needs personal creativity and expression.
1.1.1 Terminologies Used
Program
A set of explicit and unambiguous instructions expressed in a programming language
is called a program.
Algorithm
The set of instructions corresponds to a solution to a problem that is independent
of any programming language is called an algorithm.
the solution must be specified with logical precision and in such detail. And its is
obvious that conscious depth of understanding is needed to design effective computer
algorithms.
This problem solving skills are now-a-days developed from the school days.
The preliminary investigation about the problem is called as the problem definition
phase.
The primary task of this phase is ‘what must be done’ rather than ‘hoe to do it’.
For solving a problem, there are many ways to solve and it has many solutions.
Write down all the steps and explorations made during problem solving and go
along the paths ay systematize our investigations and avoid duplication of effort.
The important and crucial thing of all in developing problem-solving skills is practice.
Problem solving skill will be developed after working with many of the related
problems.
{
printf(“\n%d\t %d \t%d” ,p,node[p].info,node[p].next);
p=node[p].next;
}
//return;
printf(“\n%d \t %d \t%d”,p,node[p].info,node[p].next);
putchar(‘\n’);
return;
}
// Insertion Routine
insert(p,x)
{
int q;
if(p==-1)
{
printf(“\nVoid Insertion”);
return;
}
q=getnode();
node[q].info=x;
node[q].next=node[p].next;
node[p].next=q;
return;
}
// Deletion Routine
delete(p)
int p;
{
int q;
if(p==-1||node[p].next==-1)
{
Linear Data Structures–List 1.7
printf(“Void deletion”);
exit(1);
}
q=node[p].next;
freenode(q);
return;
}
1.4 LIST ADT
The list ADT is the data structure used to store the elements in an efficient way
where each element is linked with other elements by its address.
A list is of the form A1, A2 ……. AN
A1 A2 AN
First Last
Figure 1.2 Linked list
Item Next
0 a 1
1 b 2 Linked list
2 c –1
3 4
4 5
5 6
6 7 Free List
7 8
.. ..
. .
–
Figure 1.5
// Get Node Routine
getnode()
{
if(avail==-1)
{
printf(“\nOverflow”);
exit(1);
}
p=avail;
avail=node[avail].next;
return(p);
}
freenode(p)
{
node[p].next=avail;
avail=p;
return;
}
10 400 10 – Value
1000 – Address
Address next 400 – Next Node’s address
1000 pointer
Figure 1.6 Pointer
And the node’s next pointer points to NULL (i.e 0 in ANSI C)
A pointer variable is a variable which contains the address of another variable. For
example, if P is declared as pointer, then the value stored in P is interpreted as the location in
main memory.
P field name = value
P next = address of the next node.
The running time of linked list using pointer is as follows
PrintList (L) – Linear time
But greater than array
Find (L, Key) – Linear time
Delete – O(1)
Insert – O(1)
Linear Data Structures–List 1.11
The Printlist (L) needs just a pointer to the first element in the list and then traverse
the list by following the next pointers.
Finding Kth element needs to traverse the list one by one till the null pointer.
The delete function can be executed in one pointer change.
P
A1 A2 A3 A4 0
Temp
Figure 1.7 Deleting the node A3
The Insert function can be executed by creating a node structure and traverse
down to the particular position and change the link to the necessary nodes.
A1 A2 A3 A4 0
The function declarations and prototypes are shown in Figure1.9 and 1.10.
# ifndef_List_H
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
List MakeEmpty (List L);
int IsEmpty (List L);
int IsLast (Position P, List L);
Position Find (ElementType X, List L);
void Delete (ElementType X, List L);
Position FindPrevious (ElementType X, List L);
void Insert (ElementType X, List L, Position P);
void DeleteList (List L);
Position Header (List L);
Position First (List L);
Position Advance (Position P);
ElementType Retrieve (Position P);
# endif
Figure 1.9 Type declarations for linked list
// Node Declaration
struct Node
{
ElementType Element;
Position Next;
};
The function used to check the emptiness of a list is Empty() Shown in Figure 1.11.
int IsEmpty (List L)
{
return L Next = = NULL;
}
Figure 1.11 To check the emptiness
header
Empty List L
The function IsLast( ) tests the current element whether it is in the last position or
not as shown in Figure 1.12.
int IsLast (Position P, List L)
{
return P Next = = NULL
}
Figure 1.12 To check the node whether it is the last position
Then the function used to find the position of the list is Find( ) shown in Figure 1.13.
Position Find (ElementType X, List L)
{
Position P;
P = L Next;
while (P! = NULL && P Element ! = X)
P = P Next;
return P;
}
Figure 1.13 Find function to find the position
1.14 Data Structures
Start
temp
Original List
First
2 4 6 8
2 4 6 8
Head
A B C D
Data Next
tmp
E NULL
A cyclic movement is possible in the circularly Linked List where the cell keeps the
pointer back to the first.
N1 N2 N3 N4
All the operations involved with the linked List is also possible with the circularly
Linked Lists.
In order to keep the linked list to work in a circular way, the last node is pointed to
the first node of the list. There is no need of header for storing the beginning of a list. The
traversal of a circular list can begin at any node until pointing to the same node. There
may not be any beginning or end for the circular linked list.
Header
A B C D
These kind of lists are used in operating systems for task maintenance.
Example:
Consider the memory representation of a circular linked list which is used to store
the details of student marks as shown in figure 1.21.
Linear Data Structures–List 1.19
Position temp, P;
P = Find (Y, L);
temp = malloc (size of (struct node));
if (L = = NULL)
printf (“cannot enter an element”);
temp Element =X;
temp Next = P Next;
P Next = temp;
}
10 20 30 50
List L
10 20 30 40 50
P temp
Algorithm
void Delete (int X, List L)
{
Position P, Tmpcell;
P = L; // pointing head node
if (P = = NULL)
printf (“Underflow”);
while (P Next ! = L && P Next Element ! = X)
P = P Next;
Tmpcell = P Next;
P Next = Tmpcell Next;
free (Tmpcell);
}
L TemCell
P
10 20 30 40
temp
L P TempCell Next
10 20 30 40
The restriction when using Linked List (singly) is that there is one way path available
to traverse down the list. For this, it is better to have a back pointer to solve this problem.
Thus the Linked list constructed with two pointers front and back which specifies
the link in forward and backward traversal is called as Doubly Linked List.
N1 N2 N3
Head
NULL 1 2 3 NULL
Linear Data Structures–List 1.23
Head
NULL 10 20 30 50 NULL
Old List
NULL 5 NULL New Node
X
Add the new node before the Head node. So the new node will be the Head node.
The next link of the new node should point to the first node of the old list.
Head
NULL 5 10 20 30 40
New List
void insertatBegin (List L, int X)
{
node* temp;
temp = malloc (sizeof (struct node));
if (L = = NULL)
printf (“No node in the list”)”
temp data = X;
temp prev = NULL;
temp Next = L; // pointing Head L
L Prev = temp;
L = temp;
}
Linear Data Structures–List 1.25
Head
NULL 10 20 30 40 NULL
2
X New List
After Insertion
Head
NULL 10 20 30 40 2 NULL
Head P
NULL 10 20 30 40 NULL
From the Algorithm, we start from the Head node of the linked list. We initialize the
head node with pointer P, then traverse the linked list to reach the node that has its data
equal to Y. Once the node is reached, the next of the new node is copied with the next of
P node. The prev link of the new node is pointed to the P node. And the next of the P
node is pointed to the new node. Finally the prev link of the next of the new node is
pointed to the newnode.
Deletion
In this section, how a node is deleted from a doubly linked list is demonstrated.
While deleting a node from a list, the operations that are to be performed are as
follows.
NULL 10 20 30 40 NULL
P node to be
deleted
Head
NULL 10 20 40 NULL
After deleting the node 30
typedef struct
{
int CoeffArray [maxdegree+1];
int Highpower;
} *Polynomial;
//Initialize the resultant polynomial to Zero
void Zeropolynomial (Polynomial P)
{
int i;
for (i=0; i Maxdegree; i++)
P CoeffArray [i] = 0;
P Highpower = 0;
}
// Routine to add two Polynomials
void Addpoly(const Polynomial P1, const Polynomial P2, Polynomial Sum)
{
int i;
ZeroPolynomial (Sum);
Sum Highpower = Max (P1 Hight Power, P2 HighPower);
for (i=Sum Highpower; i>=0; i– –)
Sum CoeffArray [i] = P1 CoeffArray [i] + P2 CoeffArray [i];
}
Figure 1.29 Polynomial ADT
The linked list representation of the polynomial ADT is shown as follows
Let P1 = 5x10 + 4x7 + 1
P2 = 6x25 + 8x5 + 2x
1.30 Data Structures
p1 5 10 4 7 1 0
p2 6 25 8 5 2 1
And the node declaration for this Linked List implementation is given as follows
REVIEW QUESTIONS
Example: 1
Reverse of a Singly Linked List with iterations
voidreverse_list()
{
node *p, *q, *r;
if (head = = (mynode *)0)
{ return; }
p = head;
q = p next;
p next = (mynode *)0;
while (q != (mynode *)0)
{
r = q next;
q next = p;
p = q;
q = r;
}
head = p;
}
Example: 2
Reverse of a Singly Linked List with Recursion
node* reverse_list(mynode *root)
{
if(root next! = (mynode *)0)
{
reverse_list(root next);
root next next = root;
return(root);
}
else
{ head = root; }
}
1.32 Data Structures
Example: 3
Reverse of a Doubly Linked List
void reverse( )
{
node *cur, *temp, *nextnode;
if(head == tail)
return;
if(head == NULL || tail == NULL)
return;
for(cur = head; cur! = NULL; )
{
temp = cur next;
nextnode = cur next;
cur next = cur prev;
cur prev = temp;
cur = nextnode;
}
temp = head;
head = tail;
tail = temp;
}
Example: 4
Finding the middle of a Linked List
struct node *middle(struct node *head)
{
p = head;
q = head;
while(q next next! = NULL)
{
p = p next;
q = q next next;
}
return p;
}