0% found this document useful (0 votes)
7 views9 pages

DS Mod 3 Notes

The document provides an overview of linked lists, including their structure, creation, insertion, deletion, and printing operations in C. It also discusses applications of linked lists such as polynomial addition, sparse matrices, and circular linked lists. Additionally, it includes functions for concatenating lists, inserting nodes, inverting lists, and calculating the length of circular linked lists.

Uploaded by

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

DS Mod 3 Notes

The document provides an overview of linked lists, including their structure, creation, insertion, deletion, and printing operations in C. It also discusses applications of linked lists such as polynomial addition, sparse matrices, and circular linked lists. Additionally, it includes functions for concatenating lists, inserting nodes, inverting lists, and calculating the length of circular linked lists.

Uploaded by

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

Module 3

Linked List
Linked list or one-way list (Singly linked list) is a linear collection of data elements called nodes
where each node is divided into two parts;
The first part contains the information of the element
The second part called the link field or next pointer field, contains the address of the next node in the
list.
Consider the following Examples:

C Representation
typedef struct listNode *listPointer;
typedef struct
{
int data;
listPointer link;
}listNode;
Create two node list
#create a linked list with two nodes

Raksha Puthran, Assistant Professor, SDIT


listPointer create2()
{
listPointer first, second;
MALLOC(first, sizeof(*first));
MALLOC(second, sizeof(*second));
second link= NULL;
second data= 20;
first data= 10;
first link=second;
return first;
}
Insertion
#insert a new node with data=50 into the chain first after node x
void insert (listPointer *first, listPointer x)
{
listPointer temp;
MALLOC(temp, sizeof(*temp));
tempdata=50;
if(*first)
{
templink=xlink;
xlink=temp;
}
else
{
templink=NULL;
*first=temp;
}
}
Deletion
# delete x from the list, trail is the preceding node and *first is the front of the list
void delete(listPointer *first, listPointer trail, listPointer x)

Raksha Puthran, Assistant Professor, SDIT


{
if(trail)
traillink=xlink;
else
*first = (*first)link;
free(x);
}
Print
#printing a list
void printList(listPointer first)
{
print(“The list contains:”);
for( ; first ; first=firstlink)
printf(“%d”, firstdata);
printf(“\n”);
}
Application of linked list
Polynomials
C representation:

Node Representation:

Consider the following Example:

Raksha Puthran, Assistant Professor, SDIT


Adding two polynomial
Consider the following cases:

Raksha Puthran, Assistant Professor, SDIT


Consider the following function to add two polynomials:
#returns a polynomial which is the sum of a and b
polyPointer padd(polyPointer a, polyPointer b)
{
polyPointer c, rear, temp;
int sum;
MALLOC(rear, sizeof(*rear));
c= rear;
while (a && b)
switch(COMPARE(a expon, b expon))
{
case -1: attach(b coef, b expon, &rear);
b=blink;
break;
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;

Raksha Puthran, Assistant Professor, SDIT


}
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);
return c;
}
/* attach function creates a new node with coef= coefficient and expon=exponent, attach it to the
node pointed to by ptr. Ptr is updated to point to this node*/
void attach(float coefficient, int exponent, polyPointer *ptr)
{
polyPointer temp;
MALLOC(temp, sizeof(*temp));
tempcoef=coefficient;
temp expon= exponent;
(*ptr)  link= temp;
*ptr = temp;
}
Circular linked list

Raksha Puthran, Assistant Professor, SDIT


Sparse matrix
A sparse matrix is a matrix in which many or most of the elements have a value of zero.

Node structure for sparse matrices

Raksha Puthran, Assistant Professor, SDIT


Concatenating singly linked lists
/*produces a new list that contains the list ptr1 followed by the list ptr2. The list pointed to by ptr1 is
changed permanently */
listPointer concatenate(listPointer ptr1, listPointer ptr2)
{
listPointer temp;
if(!ptr1)
return ptr2;
if(!ptr2)
return ptr1;
for(temp=ptr1; templink;temp=templink);
templink=ptr2;
}
Inserting a node at the front of a circular linked list
/* insert node at the front of the circular list whose last node is last*/
void insertFront(listPointer *last, listPointer node)
{
if(!(*last))
{
*last=node;
nodelink=node;
}
else
{
nodelink=(*last)link;
(*last)link=node;
}
}
Inverting a singly linked list
/* invert the list pointed to by lead*/
listPointer invert(listPointer lead)
{

Raksha Puthran, Assistant Professor, SDIT


listPointer middle, trail;
middle=NULL;
while(lead)
{
trail=middle;
middle=lead;
lead=leadlink;
middlelink=trail;
}
return middle;
}
Length of a circular linked list
/*find the length of the circular list last*/
int length(listPointer last)
{
listPointer temp;
int count=0;
if(last)
{
temp=last;
do
{
count++;
temp = templink;
} while(temp!= last);
}
return count;
}

Raksha Puthran, Assistant Professor, SDIT

You might also like