0% found this document useful (0 votes)
50 views

Module 3-Part 2

The document discusses various operations on linked lists, stacks, queues, and their implementations using linked representations. It describes representing multiple stacks and queues using linked lists with nodes. Functions like push, pop, enqueue, dequeue are presented. Header nodes, circular linked lists, doubly linked lists, and their traversal, searching and operations are also summarized.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Module 3-Part 2

The document discusses various operations on linked lists, stacks, queues, and their implementations using linked representations. It describes representing multiple stacks and queues using linked lists with nodes. Functions like push, pop, enqueue, dequeue are presented. Header nodes, circular linked lists, doubly linked lists, and their traversal, searching and operations are also summarized.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

LINKED LISTS – Part 2

1
LINKED STACKS AND QUEUES

Whenever several stacks and queues coexists,


linked representation may be used.
Nodes can easily add or delete a node from t
he top of the stack. Nodes can easily add a
node to the rear of the queue and add or del
ete a node at the front

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

Void push(int i, int item)


{
stack_pointer temp ;
temp= (stack_pointer) malloc (sizeof (node))
;
if (temp==NULL) {
printf( “ The memory is full\n”);
exit(1);
}
temp->data = item;
temp->link = top[i];
top[i]= temp;
}
4
pop from 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

#define MAX_QUEUES 10 /* maximum number of que


ues */
struct node {
int data;
struct node *link;
};
typedef struct node *queue_pointer;
queue_pointer front[MAX_QUEUE], rear[MAX_QUEUE
S];
for(i=0;i<NAX_QUEUES;i++)
front[i]=rear[i]=NULL;

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)

int deleteq( int i)


{
queue_pointer temp = front [i];
int item;
if (front[i]==NULL)) {
printf( “The queue is empty\n”);
exit(1);
}
item = temp->data;
front[i] = temp->link;
free(temp);
return item;
8
}
Header Node
 It is an extra node added in the front of list that may
be used to carry some global information about the list
such as total number of nodes or name of the list. Thus
external pointer is pointing to header and header-> link
is the pointer to actual first node.
 While implementing linked queues ,the data part of the h
eader may be holding rare pointer, so that to insert a n
ode you need not traverse the list. In such case pointer
to header is external pointer to the list queque
 head-> info is rear and head-> link is front.
 Header node makes writing operation easier particularly
in circular linked list
 The structure of header node either may be same node str
start
ucture of list node or may be different depending on us
e of header node in that list.
start
9
Additional List Operations

struct list_node
{
char data;
struct list_node *link;
};
typedef struct list_node *list_pointer;

Invert single linked lists


Concatenate two linked lists

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

To handle special cases like empty list , header n


odes are used which makes writing functions for
different operations more easier. Such list are
known as header linked list.
The two kinds of widely used header lists:
1. A grounded header list is a header list where t
he last node contains the null pointer.
2. A circular header list is a header list where t
he last node points back to the header node.

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

The first node in a header list is the node following


the header node, and the location of the first node i
s START→LINK, not START, as with ordinary linked lis
ts.
To traverse a circular header list
1. Begins with PTR = START→LINK (not PTR = START)
15 2. Ends when PTR = START (not PTR = NULL).
Traversing
Algorithm: Traversing a Circular Header List (LIS
T,START,INFO).
1. Set PTR: = LINK[START]. [Initializes the point
er PTR.]
2. Repeat Steps 3 and 4 while PTR ≠ START:
3. Apply PROCESS to INFO[PTR].
4. Set PTR: = LINK[PTR]. [PTR now points to the n
ext node.]
[End of Step 2 loop.]
5. Exit.

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

llink item rlink

Here Empty list has only header 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

Representation of the polynomial

where the ai are nonzero coefficients and the e i are nonneg


ative integer exponents ≥ 0.
We represent each term as a node containing coefficient an
d exponent fields, as well as a pointer to the next term.

Assuming that the coefficients are integers, the type decl


arations are:
Struct polynode {
coef expon link
int coef;
int expon;
struct polynode * link;
} ;
typedef struct polyNode *polyPointer;
31 polyPointer a, b;
32
Circular list representation of polynomials

Polynomials can be represented using circular l


ist
To handle zero polynomial we introduce header no
de into polynomials
The expon and coeft fields of header is irrevale
nt
Circular representation of a Zero polynomial and
a polynomial

33
Representation of polynomials using Circular l
inked list – using Dynamic varibles

We maintain an available list. Initially it is empty.


When a node is freed means we add it to the available lis
t.We call this function as retnode function
If a node is required ,it is taken from the available lis
t.If available list is empty , then we get node by using
malloc function. This function is known as getnode functi
on
Void retnode(PolyPointer node)
{
node->link=avail;
avail=node;
}

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.

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=TRUE;
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, &rear);
a = a->link;
}
} while(!done);
lastC->link=C;
Return C;
}

39
Sparse Matrix representation using linked list

Each column of sparse matrix is represented as


circular linked list with header node.
Similarly each row is represented as circular
linked list with header node.
Two types of nodes are used. Header node and E
ntry node
Each node has a tag field to distinguish heade
r node with entry node

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

#define MAX_SIZE 50 /* size of largest matrix */


typedef enum {head, entry} tagfield;
typedef struct {
int row;
int col;
int value;
} entry node;
struct matrix node {
tagfield tag;
struct matrix node * down;
struct matrix node * right;
union {
struct matrix * next;
entry node entry;
} u;
};
typedef struct matrix_node *matrix pointer;
44 matrix pointer node[MAX_SIZE];
45

You might also like