0% found this document useful (0 votes)
8 views77 pages

Module 2

Uploaded by

veena.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views77 pages

Module 2

Uploaded by

veena.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 77

LINKED LISTS

(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

• Singly Linked List


• Doubly Linked List
• Circular Linked List
SINGLY LINKED LIST:

simplest type of linked list in which every node contains


some data and a pointer to the next node of the same
data type.
Representing chains in C
• Self referential structure: a self-referential structure is a
structure that contains a pointer to a variable of the
same type. This allows the structure to refer to itself
Example : struct xyz
{
int num;
struct xyz *ptr;
};
Representing chains in C
1. Use self referential structure for defining a nodes structure(the
field’s it contains)
2. Create a new nodes by using malloc() function
3. Use free() function to remove the nodes that are no longer needed
• Structure definition to define a node to hold integer data is
typedef struct listNode *listPointer;
typedef struct
{
int data;
listPointer link;
}listNode;
FUNCTION DELETE A NODE AT BEGINNING OF
LIST
void delete_begin()
{
struct node *temp;
if (first== NULL)
{
printf(“list empty”);
return;
}
temp= first;
printf(“ deleted node =%d\n”,firstdata);
first= firstlink;
free( temp);
}
FUNCTION DELETE A NODE AT
END OF LIST
void delete_END()
{
struct node *temp;
if (first== NULL)
{
printf(“list empty”);
return;
}
temp=first;
while (templink != NULL)
{
prev=temp;
temp=templink
}
prevlink=NULL;
free(temp);
}
FUNCTION TO DELETE A NODE
AT NTH LOCATION
void random_delete(int loc)
{
struct node *temp;
if (first== NULL)
{
printf(“list empty”);
return;
}
If(loc==1)
{
temp=first;
first=firstlink;
free(temp);
return;
}
temp=first;
for(i=1;i<loc;i++)
{
prev=temp
temp=templink;
}
Prevlink=templink;
free(temp);
}
MULTIPLE STACKS
• we have been concerned only with the representations of a single
stack
• multiple stacks are examined only sequential mappings of stacks into
an array, memory[MEMORY-SIZE]
• To represent only two stacks,
1.memory [0] for the bottom element of the first stack and
memory[MEMORY_SlZE - 1] for the bottom element of the second
stack.
2.The first stack grows toward memory[MEMORY_SIZE - 1] and the
second grows toward memory [0].
• Representing more than two stacks within the same array poses
problems since we no longer have an obvious point for the bottom
element of each stack.
• Assume that
1. i refers to the stack number of one of the n stacks
2. To establish this stack, we must create indices for both the bottom
and top positions of this stack.
.
3. The bottom element, boundary[i], 0 < i< MAX-STACKS, always points
to the position immediately to the left of the bottom element
4.top[i], 0 < i< MAX-STACKS points to the top element
The relevant declarations are:
#define MEMORY-SIZE 100
#define MAX-STACKS 10
int memory[MEMORY—SIZE];
int top[MAX—STACKS] ;
int boundary[MAX—STACKS] ;
int n; /* number of stacks entered by the user *
To divide the array into roughly
equal segments
top[0] = boundary[0]=-1
for (j= 1; j<n;j++)
top[j] = boundary[j]=(MEMORY-SIZE /n)*j
boundary[n] = MEMORY_SIZE-1;
function to Add an item to the
ith stack
void push (int i, int item)
{
if (top[i] == boundary[i+1])
stack_full(i) ;
memory[++top[i]]= item;
}
Delete an item from the ith
stack
int pop(int i)
{
if (top[i] == boundary[i]) return stackEmpty(i);
return memory[top[i]--];
}
LINKED STACKS AND QUEUES
• #define MAX—STACKS 10 /*maximum number of stacks
struct stack
{
int item
struct stack * link;
};
struct stack top[MAX—STACKS];
• We assume that the initial condition for the stacks is:
top [i] = NULL ,0<i< MAX-STACKS
boundary conditions are:
top [i] = NULL iff the ith stack is empty
Function to Add to a linked
stack
void push(int i,int item)
{
struct stack *temp=(struct stack) malloc(struct stack);
tempdata=item;
templink=top[i];
top[i]=temp;
}
Function to delete from a linked
stack
int pop(int i)
{
struct stack *temp=top[i];
if( top[i]==NULL)
return StackEmpty();
Item=tempdata;
top[i]=templink;
free(temp);
return item;
}
Linked Queues
#define MAX-QUEUES 10
typedef struct queue *queuepointer;
typdef struct
{
int key;
}element;
typedef struct {
element data;
queuepointer link;
} queue;
queuepointer front[MAX-QUEUES], rear[MAX-QUEUES];

Initial condition for the queue


front[i]=null 0<i<max_queue
Boundary condition for the queue
front[i]=NULL iff ith queue is empty
Function to add to the rear of
linked list
void addq(int i,element item)
{
queuepointer temp;
temp=(queuepointer*)malloc (sizeof(*temp));
tempdata=item;
templink=NULL;
if (front[i]==NULL)
{
front[i]=temp;
rear[i]=temp;
}
else
{
rear[i]link=temp;
rear[i]=temp;
}
Function to delete from the
front of linked
element deleteq( int i)
list
{
queuepointer temp=front[i];
element item;
if (front[i]==NULL)
return QueueEmpty();
Item=tempdata
front[i]=templink; or front[i]=front[i]link;
free(temp);
return item;
}
POLYNOMIALS
Representing Polynomials As Singly Linked Lists
typedef struct polynode *polypointer;
typedef struct polynode
{
int coef;
int expon;
polypointer link;
};
polypointer a,b,d;
• We draw poly-nodes as:

coef expon link


Store the polynomials
a= 3x^14+2x^8+1
b=8x^14-3x^4+10x^2
polypointer padd(polypointer a, polypointer b)
•{
polypointer c, rear, temp;
int sum;
rear=(polynode *)malloc(sizeof(polynode));
c=rear;
while (a && b)
{
switch (COMPARE(a->expon,b->expon))
{
case 0:
sum = a->coef + b->coef;
if (sum)
attach(sum,a->expon,&rear);
a = a—>link;
b = b—>link;
break;
case 1:
attach(a->coef,a->expon,&rear) ;
a= a->link;
break;
case -1:
attach(b->coef,b->expon,&rear) ;
a= b->link;
break;
}
}
for (; a; a = a->link) attach(a->coef,a->expon,&rear)
for (; b; b = b->link) attach(b->coef,b->expon,&rear)
rearlink =NULL;

temp = c;
c=clink;
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=alink;
b=blink;
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;
}
newnodedata=item;
newnodelink=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=firstlink;
printf(“deleted data %d\n”,tempdata);
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 ( curlink!=NULL)
{
cur=curlink;
}
curlink= 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“,curdata);
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)
{
newnodelink=first;
first=newnode;
return;
}
newnodedata=item;
temp=first;
for(int i=1;i< loc-1;i++)
{
temp=temp->link;
}
newnodelink=
templink;
templink=
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;

while (cur->rlink != NULL)


{
prev = cur;
cur = cur->rlink;
}
printf("Details deleted\n");
free(cur);

prev->rlink = NULL;
return first;
}

You might also like