Module 2
Module 2
(BAT,CAT,EAT,FAT,HAT,JAT,LAT,MAT,OAT,PAT,RAT,SAT ,VAT,
WAT)
BAT BAT
CAT
CAT EAT
EAT FAT
FAT HAT
HAT JAT
JAT LAT
LAT MAT
MAT OAT GAT
OAT PAT
PAT RAT
RAT SAT
SAT VAT
VAT WAT
WAT
I DATA LINK
1 HAT 11
2
3 CAT 4
4 EAT 9
5
6
7 WAT 0
8 BAT 3
9 FAT 1
10
11 VAT 7
LINKED LIST
• A linked list is a linear collection of data elements, called nodes,
where the linear order is given by means of pointers.
• That is, each node is divided into two parts:
1.The first part contains the information of the element,
2. The second part, called the link field or nextpointer field, contains
the address of the next node in the list.
REPRESENTATION OF LINKED
LIST
ARRAY VS LINKED LIST
TYPES IN LINKED LIST
temp = c;
c=clink;
free(temp);
}
void attach(float coefficient, int
exponent, polypointer ptr)
{
polypointer temp;
temp= (polypointer)malloc(sizeof(polynode));
temp->coef = coefficient;
temp->expon = exponent;
( *ptr)->link = temp;
*ptr= temp;
}
• we free nodes that are no longer in use so that we may reuse these
nodes later.
• We can meet this objective, and obtain an efficient erase algorithm
for circular lists, by maintaining our own list (as a chain) of nodes that
have been “freed.“
• Let avail be a variable of type polypointer that points to the first node
in our list of freed nodes.
• Initially,
avail =NULL.
Instead of using malloc and free, we now use get-node and ret-node
getnode( ) function
polypointer getnode(void)
{
polypointer node;
if (avail!=NULL) // if (avail)
{
node = avail;
avail = avail->link;
}
else
node=(polypointer) malloc(sizeof(polynode));
return node;
}
retnode function
void retnode(polypointer ptr)
{
ptr->link = avail;
avail = ptr;
}
Circular List Representation of Polynomial
• Circular singly linked List is A singly linked list in which link field of
the last node points to the fist node in the list.
Adding two polynomials represented as
circular lists with header nodes
poly_pointer cpadd(poly_pointer
a, poly_pointer b)
{
poly_pointer startA, c, lastc;
int sum, done=0;
startA = a;
lastc = c = getnode();
c->expon = -1;
a=alink;
b=blink;
do
{
switch (COMPARE(a->expon, b->expon))
{
case -1: /* a->expon < b->expon */
attach(b->coef, b->expon, &lastc);
b = b->link;
break;
case 0: /* a->expon == b->expon */
if (startA == a)
done=1;
Else
{
sum = a->coef + b->coef;
if (sum)
attach(sum, a->expon, &lastc);
a = a->link;
b = b->link;
}
break;
case 1: /* a->expon > b->expon */
attach(a->coef, a->expon, &lastc);
a = a->link;
break;
}
} while (!done);
lastc->link = c;
return c;
}
SPARSE MATRICES
• each column of a sparse matrix as a circularly linked list with a head
node.
• We use a similar representation for each row of a sparse matrix
• down field to link into a column list and the right field to link into a
row list. The next field links the head nodes together
• The head node for row i is also the head node for column i, and the
total number of head nodes is max {number of rows, number of
columns).
•.
• Each entry node has five fields in addition to the tag field: row, col,
down, right, value .
• We use the down field to link to the next nonzero term in the same
column and the right field to link to the next nonzero term in the
same row
Structure for a node in sparse
matrix
#define MAX_SIZE 50
Typedef enum {head, entry} tagfield;
typedef struct matixNode *matrixpointer;
• typedef struct {
int row,col,value;
}entrynode;
typedef struct {
matrixpointer down;
matrixpointer right;
tagfield tag;
union {
matrixpointer next;
entrynode entry;
} u;
}matrixnode;
Matrixpointer hdnode[max_size];
typedef struct {
matrixpointer down;
matrixpointer right;
tagfield tag;
singly linked list
struct node
{
int data;
struct node *link;
};
typedef struct node * NODE;
Insertion at beginning
NODE insert_begin(int item)
{
NODE newnode;
newnode=(NODE ) malloc(sizeof( struct node));
if(newnode==NULL)
{
printf(“insufficient memory\n”);
return;
}
newnodedata=item;
newnodelink=first;
first= newnode;
return first;
}
deletion at beginning
NODE del_begin(NODE first)
{
NODE temp;
if (first==NULL)
{
printf(“list EMPTY”);
return first;
}
temp=first;
first=firstlink;
printf(“deleted data %d\n”,tempdata);
free(temp);
return first;
}
Insertion at the end
NODE insert_rear(int item)
{
NODE newnode, cur;
newnode =(NODE) malloc( sizeof(struct node));
newnode data=item;
newnode--> link=NULL;
If( first==NULL)
{
first= newnode;
return first;
}
cur= first;
while ( curlink!=NULL)
{
cur=curlink;
}
curlink= newnode;
return first;
}
Deletion at the end
NODE delete_rear(NODE first)
{
NODE cur, prev;
if (first == NULL)
{
printf("\nList is empty\n");
return first;
}
if (first->link == NULL)
{
printf("Details deleted\n");
free(first);
return NULL;
}
prev = NULL; cur = first;
while (cur->link != NULL)
{
prev = cur;
cur = cur->link;
}
printf("Details deleted %d\
n“,curdata);
free(cur);
prev->link = NULL;
return first;
}
FUNCTION INSERTING A NODE AT NTH
LOCATION
void random_insert(int item,int loc)
{
NODE newnode,temp;
newnode=(NODE) malloc(sizeof( Struct node));
if(newnode==NULL)
{
printf(“ insufficient memory\n”);
return;
}
If (loc==1)
{
newnodelink=first;
first=newnode;
return;
}
newnodedata=item;
temp=first;
for(int i=1;i< loc-1;i++)
{
temp=temp->link;
}
newnodelink=
templink;
templink=
newnode;
}
Doubly linked list
NODE insert_front(NODE first)
{
NODE temp;
temp = getnode();
if (first == NULL)
{
return temp;
}
temp->rlink = first;
first->llink = temp;
first = temp;
return first;
}
Insert at the end
NODE insert_rear(NODE first)
{
NODE temp, cur;
temp = getnode();
if (first == NULL)
{
return temp;
}
cur = first;
while (cur->rlink != NULL)
{
cur = cur->rlink;
}
cur->rlink = temp;
temp->llink = cur;
return first;
Delete front
NODE delete_front(NODE first)
{
NODE temp;
if (first == NULL)
{
printf("\nList is empty\n");
return first;
}
if (first->rlink == NULL)
{
printf("Details deleted\n");
free(first);
return NULL;
}
temp = first;
first = first->rlink;
first->llink = NULL;
printf("\nDetails deleted\n");
free(temp);
return first;
Delete rear
NODE delete_rear(NODE first)
{
NODE cur, prev;
if (first == NULL)
{
printf("\nList is empty\n");
return first;
}
if (first->rlink == NULL)
{
printf("Details deleted\n");
free(first);
return NULL;
}
prev = NULL;
cur = first;
prev->rlink = NULL;
return first;
}