ds unit 2
ds unit 2
Structure of Node
struct Node
{
int data;
struct Node *next;
};
Example:
The function release(), like delete() in C++, causes the node pointed to
by x to be returned to the available space list. It is defined as
x -> next = AVAIL;
AVAIL = x;
Operations
1. Push
2. Pop
3. display
push(value) - Inserting an element into the Stack
We can use the following steps to insert a new node into the stack...
Step 1: Create a newNode with given value.
Step 2: Check whether stack is Empty (top == NULL)
Step 3: If it is Empty, then set newNode next = NULL.
Step 4: If it is Not Empty, then set newNode next = top.
Step 5: Finally, set top = newNode.
The major problem with the Queue implemented using array is, it
works only for fixed number of data values. Queue implemented using linked list
works for unlimited number of values.
In linked list implementation of a queue, the last inserted element is
always pointed by ‘Rear’ and first inserted element is always pointed by ‘Front’ .
Operations
1. enQueue 2. deQueue 3. display
enQueue(value) - Inserting an element into Queue
We can use the following steps to insert a new node into the queue...
Step 1: Create a newNode with given value, set 'newNode→next' to NULL.
Step 2: Check whether queue is Empty (rear == NULL)
Step 3: If it is Empty then, set front = newNode and rear = newNode.
Step 4: If it is Not Empty then, set rearnext = newNode
and rear = newNode.
Program
#include<stdio.h>
#include<stdlib.h>
struct Polynomial
{
int exp,coef;
struct Polynomial *next;
};
typedef struct Polynomial * Poly;
Poly read();
void disp(Poly);
Poly add(Poly,Poly);
Poly read()
{
Poly head=NULL, temp, newNode;
int exp,coef;
char ch;
do
{
printf("\nEnter exponent, coefficient");
scanf("%d%d",&exp,&coef);
newNode=(Poly)malloc(sizeof(Poly));
newNode->exp=exp;
newNode->coef=coef;
newNode->next=NULL;
if( head==NULL )
head=newNode;
else
{
temp=head;
while( temp->next!=NULL )
temp=temp->next;
temp->next=newNode;
}
printf("\nDo you want to continue");
scanf(" %c",&ch);
}while( ch!='n' );
return head;
}
void disp(Poly head)
{
printf("\nPolynomial is \t");
while( head!=NULL )
{
printf(" %+dX^%d",head->coef,head->exp);
head=head->next;
}
}
Poly add(Poly head1,Poly head2)
{
Poly head3=NULL,temp,newNode;
while( head1!=NULL && head2!=NULL )
{
newNode=(Poly)malloc(sizeof(Poly));
if( head1->exp==head2->exp )
{
newNode->exp=head1->exp;
newNode->coef=head1->coef + head2->coef;
head1=head1->next;
head2=head2->next;
}
else if( head1->exp>head2->exp )
{
newNode->exp=head1->exp;
newNode->coef=head1->coef;
head1=head1->next;
}
else if( head1->exp<head2->exp )
{
newNode->exp=head2->exp;
newNode->coef=head2->coef;
head2=head2->next;
}
newNode->next=NULL;
if( head3==NULL )
head3=newNode;
else
{
temp=head3;
while( temp->next!=NULL )
{
temp=temp->next;
}
temp->next=newNode;
}
}
return head3;
}
void main()
{
Poly head1,head2,head3;
head1=read();
head2=read();
head3=add( head1,head2);
disp(head1);
disp(head2);
disp(head3);
}
Circular List Representation of Polynomials
Program
#include <stdio.h>
#include <conio.h>
#include <math.h>
struct Node
{
int exp;
int coef;
struct Node *next;
};
typedef struct Node Node;
Node * create();
void disp(Node *);
Node *create()
{
Node *head=NULL,*temp,*newNode;
int exp,coef;
char ch;
do
{
printf("\nEnter exponent, coefficient");
scanf("%d%d",&exp,&coef);
newNode=(Node *)malloc(sizeof(Node));
newNode->exp=exp;
newNode->coef=coef;
newNode->next=NULL;
if( head==NULL )
{
head=newNode;
head->next=head;
}
else
{
temp=head;
while( temp->next!=head )
temp=temp->next;
temp->next=newNode;
newNode->next=head;
}
printf("\nDo you want to continue");
scanf(" %c",&ch);
}while( ch!='n' );
return head;
}
void disp(Node *head)
{
Node *temp=head;
printf("\nPolynomial is \t");
while( temp!=NULL )
{
printf(" %+dX^%d",temp->coef,temp->exp);
temp=temp->next;
if( temp==head )
break;
}
}
void main()
{
Node *head=NULL;
clrscr();
head=create();
disp(head);
}
EQUIVALENCE CLASSES
A relation ≡, over a set S, is said to be equivalence relation over S if
and only if it is reflexive, symmetric and transitive over S.
1. for any polygon x, x ≡ x, x is electrically equivalent to itself then ≡is
reflexive.
2. for any two polygons(x,y), x ≡ y implies y ≡ x then the relation ≡ is
symmetric
3. for any 3 polygons(x,y,z), x≡y, y≡z implies x≡z then the relation ≡ is
transitive
The examples of equivalence relations are numerous. For example
“equal to” relationship is an equivalence relation.
1. x ≡ x
2. x ≡ y implies y ≡ x
3. x ≡ y, y ≡ z implies x ≡ z
The equivalence relation is to partition set “S” into equivalent classes
such that the two members x and y of “S” are in the same equivalence class
if and only if x ≡ y. For example there are 12 variables numbered from 1 to
12 with pairs as follows:
1 ≡ 5, 4 ≡ 2, 7 ≡ 11, 9 ≡ 10, 8 ≡ 5, 7 ≡ 9, 4 ≡ 6, 3 ≡ 12 and 12 ≡ 1
The equivalence classes over the equivalence relation is as follows. The
12 variables partitioned into 3 classes.
{ 1, 3, 5, 8, 12}, { 2, 4, 6}, { 7, 9, 10, 11}
The algorithm for equivalence classes defined works in two phases. In
the first phase the equivalence pair (i,j) are read and stored. In second
phase, we begin at 1 and find all pairs of (1,j). The values 1 and j are in the
same class. By transitivity, all the pairs of the form (j, k) implies k is in the
same equivalence class.
void equivalence()
{
initialize;
while(there are more pairs)
{
read the next pair (i,j);
process this pair;
}
initialize the output;
do
{
output a new equivalence class;
}while(not done);
}
The inputs n and m represents number of objects and number of
related pairs. The data structure used here is array to hold these pairs. As
we have problem of using arrays we go linked lists. Each node will have data
and next parts. We will use largest one dimensional array seq(n) and
outputting the object we need out(i).
void equivalence()
{
initialize seq to null and out to true;
while(there are more pairs)
{
read the next pair (i,j);
put j on the seq(i) list;
put i on the seq(j) list;
}
for(i= 0; i<n; i++)
if(out(i))
{
out(i) = false;
output this equivalence class;
}
}
SPARSE MATRIX
“A matrix that contains very few number of non-zero elements is
called sparse matrix”
or
“A matrix that contains more number of zero values when compared
with non-zero values is called a sparse matrix”
Sparsematrix Abstract Data Type
{
instances:
a set of triples (row, col, value) where row, col are two
integers and value comes from the set item.
operations:
for all a, b ϵ sparse matrix, x ϵ item, i,j, maxcol, maxrow ϵ
index
sparsematrix create(maxrow, maxcol);
sparesematrix matrixtranspose(a);
psparsematrix matrixadd(a, b);
sparsematrix matrixmultiply(a, b);
}
SPARSE MATRIX REPRESENTATION
For linked representation, we need two types of Node.
1. Header Node / Head Node
2. Element Node
Here
'link1' stores the address of the previous node
'link2' stores the address of the next node
data' stores the value of that node.
Note
In double linked list, the first node must be always pointed by head.
Always the previous field of the first node must be NULL.
Always the next field of the last node must be NULL.
Operations
In a double linked list, we perform the following operations...
1. Insertion
2. Deletion
3. Display
Insertion
In a double linked list, the insertion operation can be performed in three ways
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Inserting At Beginning of the list
following steps used to insert new node at beginning of double linked list...
Step1: Create a newNode with given value, set newNodeleft and
newNoderight as NULL.
Step2: Check whether list is Empty (head == NULL)
Step3: If it is Empty then, set head=last=newNode.
Step4: If it is not Empty then, set
newNoderight=head,
headleft=newNode,
head=newNode.
Deletion
In a double linked list, the deletion operation can be performed in three
ways
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
Deleting from Beginning of the list
We can use following steps to delete node from beginning of double linked
list...
Step 1: Check whether list is Empty (head == NULL)
Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
Step 3: If it is not Empty then, define a Node pointer 'temp' and initialize
with head.
Step 4: Check whether list is having only one node (headright==NULL)
Step 5: If it is TRUE, then set head=NULL and delete temp
Step 6: If it is FALSE, then set head=tempright, headleft=NULL and delete
temp.
Generalized List
Generalized list is a series of N elements. In Generalized list a node can have
either an atom or piece of information or sub list.
Conventions for Generalized Lists:
– List names are represented in capital letters
– Atom names are lowercase letters
Definitions for Generalized Lists:
Length – Number of elements (atoms or sub lists) in the list
Head – First element of the list
Tail – List composed of all elements except first of list
Examples of Generalized Lists:
1) A=()
Length=0
A is Null List
2) B=(a,b,c)
Length=3
Head=a
Tail=b,c
3) C=(a,(b,c))
Length=2
Head=a
Tail=(b,c)
Node representation:
– A node in the list needs to have either an atom or piece of
information or pointer to another list
Tag = TRUE/FALSE Data/DLink Link (Next)
false a true 0
A.Front
false b false c 0
3. List Depth – computes the depth of the list. if the list is empty then the
depth of the list is zero.
0 if s is an atom
depth(s) = 1 + max{ depth(x1, x2 ….. xn)} if s is the list (x1, x2, …..xn)
driver
int genlist :: depth()
{
return depth(first);
}
workhorse
int genlist :: depth(genlistnode *s)
{
if(*s)
return 0;
genlistnode *p = s;
int m = 0;
while(p)
{
if(p -> tag)
{
int n = depth(p -> dlink);
if(m < n)
m = n;
}
p = p -> link;
}
return m + 1;
}
Lists that are shared by other lists such as A create problems when
we add or delete a node at the front. If the first node is deleted then the
change of the pointers from B to point to second node. If the newnode is
added then pointers from B now should point to the newnode. But we do not
all the points from which a particular list is referenced. The problem can be
easily solved by using head node. The use of head node when performing
add or delete at the front of the lists will eliminate the need to retain all
pointers to a specific list. If each list have head nodes then
The values in the data field of the head node is the reference count of
the corresponding list. when the lists are shared by other lists we need a
mechanism to determine whether or not the list of nodes may be physically
returned to available space list. This mechanism provides reference count
maintained in the head node of each list. Since the data field of the head
node is free the reference count is maintained in this field. The reference
count is the number of pointers to that list.
When reference count decreases and becomes zero then all the nodes
are physically returned to the available space list. the class definition for
genlist is unchanged and genlistnode is as follows:
enum tag {0, 1, 2};
class genlistnode
{
Friend class genlist;
genlistnode *link
int tag t;
union
{
char data;
genlistnode *dlink;
int ref;
};
};
A recursive algorithm for erasing the list will examine all the top level
nodes of the list whose reference count has become zero. So any such
sublist found, using erase algorithm we erase the list and link it to available
space list. For any recursive list the reference count never becomes zero.