Module 3-Part 2
Module 3-Part 2
1
LINKED STACKS AND QUEUES
2
Represent n stacks
#define MAX_STACKS 10 /* maximum number
of stacks
struct node {
int data;
struct node * link;
};
typedef struct node *stack_pointer;
stack_pointer top[MAX_STACKS];
for(i=0;i<MAX_STACKS; i++)
top[i]=NULL;
3
Push in the linked stack
int pop(int i)
{
stack_pointer temp = top[i];
int item;
if (top[i]==NULL)) {
printf( “The stack is empty\n”);
exit(1);
}
item = temp->data;
top[i] = temp->link;
free(temp);
return item;
}
5
Represent n queues
6
enqueue in the linked queue
void addq(int i, int item)
{
queue_pointer temp;
temp = (queue_pointer) malloc(sizeof (node));
if (temp==NULL)
{
printf( “ The memory is full\n”);
exit(1);
}
temp->data = item;
temp->link = NULL;
if (front [i])
rear[i] -> link = temp;
else
front[i] = temp;
rear[i] = temp;
7
}
dequeue from the linked queue (similar to
pop)
struct list_node
{
char data;
struct list_node *link;
};
typedef struct list_node *list_pointer;
10
Concatenate Two Lists
list_pointer concatenate(list_pointer ptr1, list_pointer ptr2)
{
list_pointer temp;
if (IS_EMPTY(ptr1)) return ptr2;
else {
if (!IS_EMPTY(ptr2)) {
for (temp=ptr1; temp->link; temp=temp->link)
;
temp->link = ptr2;
}
ptr1
return ptr1;
}
}
ptr2
11
Invert Single Linked Lists- By Using two e
xtra pointers: middle and trail
.
list_pointer invert(list_pointer start)
{
list_pointer middle, trail;
middle = NULL;
while (start)
{
trail = middle;
middle = start;
start = start->link;
middle->link = trail;
}
start=middle
...
return middle;
12 }
Circular linked list
If the link field in last node contains a poi
nter back to the first node rather than the n
ull pointer, the linked list is called as cir
cular linked list.
In circular list we can reach any of nodes th
at precede node(p).
Start
that is it is possible to reach any point fro
m any other point in such a list.
First Node Last Node
13
Header list
14
The list pointer START always points to the header no
de.
If START→LINK = NULL indicates that a grounded head
er list is empty
If START→LINK = START indicates that a circular hea
der list is empty.
start header
16
Searching in Circular list
Algorithm: SRCHHL (INFO, LINK, START, ITEM, LOC)
LIST is a circular header list in memory. This algorithm finds
the location LOC of the node where ITEM first appears in LIST o
r sets LOC = NULL.
1. Set PTR: = LINK[START]
2. Repeat while INFO [PTR] ≠ ITEM and PTR ≠ START:
Set PTR: =LINK[PTR]. [PTR now points to the next node.]
[End of loop.]
3. If [PTR]INFO = ITEM and PTR ≠ START then:
Set LOC: = PTR. ,
Else:
Set LOC: = NULL.
[End of If structure.]
4. Exit.
17
Operation on Circular linked
list
1.Insert node at front of circular list whose last node is
last
listptr insertfront( listptr last,listptr node)
{
If(last== NULL) {
last=node;
node->link=node;
}
Else {
Node->link=last-> link;
Last->link=node; last
}
Node
Return last;
18 }
Delete from the front
1.Delete node from front of circular list whose las
t node is last
Listptr delete_front( listptr last)
{ listptr node;
If(last== NULL) { printf(“List is Empty\n”; exit
(0);}
If (last-> link==last) { node= last; last=NULL;}
Else { node=last-> link;
last->link=node-> link; Last
}
free(node);
19 }
Insafter in Circular linked list
1.Insert node q after a given node p in a circular list whose l
ast node is last
listptr insafter( listptr last, listptr q , listptr p )
{
If(last== NULL) {
last=q;
q->link=q;
}
Else if (p== NULL)
{ printf(“ void Insertion”); exit(1);}
Else{
q->link=p-> link;
last
p->link=q;
}
Return last;
20 }
Delafter(p)
1.Delete node after given node p in a circular list whose last n
ode is last
Listptr delafter( listptr last, listptr p )
{
Listptr q;
If(last== NULL) { printf(“List is Empty\n”; exit(0);}
If (p==NULL)||(p=p->link){printf(“void deletion”); exit
(1);}
Else { q=p-> link;
Last
p->link=q-> link;
} p
free(q);
21 }
Finding length of circular li
st
int length (listptr last)
{
listptr temp;
int count=0;
if(last) {
temp=last;
do{ count++;
temp=temp->link;
}while(temp!=last);
}
return count;
}
Last
22
Concatenating two Circular li
sts
{Void concatenate (listptr *ptr1,listptr *ptr2)
listptr temp;
if(*ptr2== NULL) return;
if(*ptr1== NULL) {*ptr1=*ptr2; return;}
temp=(*ptr1)->link;
(*ptr1)->link=(*ptr2)-> link;
(*ptr2)-> link=temp;
ptr1
*ptr1=*ptr2;
return;
}
ptr2
23
DOUBLY LINKED LIST
1. The difficulties with single linked lists is that, it
is possible to traversal only in one direction, ie., dir
ection of the links.
2. The only way to find the node that precedes p is to s
tart at the beginning of the list.
3. Though circular linked list solves some of drawbacks
of linear list, we can not traverse such a list backward
s.
The solution to all these problems is to change struct
ure of node. Each node should have two link fields with
the data field so that we can traverse both forward and
backward direction.
A list having every nodes with two link fields, one link
ing forward direction and other linking backward directi
on , so that we can traverse the list in both direction
is known as doubly linked list.
24
DOUBLY LINKED LIST (Two way list)
Doubly linked list also known as two way list. It is
a collection of data elements, called nodes, where ea
ch node N is divided into three parts:
1. An information field INFO which contains the data.
2. A pointer field RLINK (FORW) which contains the lo
cation of the next node in the list
3. A pointer field LLINK (BACK) which contains the lo
cation of the preceding node in the list
The declarations are:
struct dnode {
int data; llink Data rlink
struct dnode * rlink;
struct dnode * llink;
} ;
typedef struct dnode *nodePointer;
25
If ptr points to an node in DLL, then
Ptr=ptr->llink->rlink= ptr->rlink->llink that
is we can go backward and forward easily.
A doubly linked list (DLL)may be grounded or
circular list
1. Grounded DLL
2. circular DLL
26
DLL circular list with heade
r
head node
ptr
27
Inserting and deleting nodes
in DLL
Insertion into a doubly linked list
The function dinsert performs the insert newnode
to the right of node .
void dinsert(nodePointer node, nodePointer newno
de)
{
newnode→llink = node;
newnode→rlink = node→rlink;
node→rlink→llink = newnode;
node→rlink = newnode; start node
28
}
Deleting the given node in DLL
void ddelete(nodePointer node, nodePointer deleted)
{ /*Deleting node where node is header*/
nodePointer p,q;
if( node == deleted)
printf("Deletion of header node not permitted.\n");
else {
p= deleted→llink;
q= deleted→rlink
p→rlink = deleted→rlink;
q→llink = deleted→llink;
free(deleted) ;
}
} p Deleted q
29
DLL using arrays
5
3 11 8
0 Adams 3 5 Dean 11 3 Fields 8 11 Green 0
30
APPLICATIONS OF LINKED LISTS – POLYNOMIALS
33
Representation of polynomials using Circular l
inked list – using Dynamic varibles
34
PolyPointer getnode(void)
{
PolyPointer node;
if( avail) {
node= avail;
avail=avail-> link;
}
else
node=(PolyPointer) malloc (sizeof(struct polynode));
return node;
}
35
a=a, then
we write attach function that uses the function c
all
attach(a-> coef,a->expon,&last a);
Void attach( int coeft,int exponent, PolyPointer * pt
r)
{
Lasta
PolyPointer temp;
temp=getnode(); a
temp->coef=coeft;
temp->expon=exponent;
(*ptr)->link=temp;
*ptr=temp;
}
After inserting all nodes to list a we make it as circu
lar list by writing lasta-> link=a
36
Adding Polynomials using Circular linked list
37
Alogrithm for Adding Polynomi
als
PolyPointer padd(PolyPointer a, PolyPointer b)
{
PolyPointer startA, C, last C;
int sum, done=FALSE;
startA=a;
a=a->link;
b=b->link;
C=getnode();
C->expon=-1; lastC=C;
do {
switch (COMPARE(a->expon, b->expon)) {
38
Contd.
39
Sparse Matrix representation using linked list
40
Header and elemental (entry) node
Header node has 3 fields down ,right and next
Down field is used to link column list
Right field is used to link row list
Next field is used to link header nodes together
Element or entry node has 5 fields.
Down field is used to link next nonzero term in same column.
Right field is used to link next nonzero term in same row.
If element is aij then row is i, column is j and value is a
Total number of head nodes = max{number of rows, number of colum
ns}
Total number of nodes= max{rows,cols}+num terms+1header to
header
Header to header has element node structure
carrying number of rows, columns and number
41
of non zero terms.
Sparse Matrix representation using linked list
42
43
C declaration to have two different types of nodes