0% found this document useful (0 votes)
17 views10 pages

Dsa Mod3 - Part4

Uploaded by

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

Dsa Mod3 - Part4

Uploaded by

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

Data Structures and Applications (CS204) Module 3

3.8 Additional List Operations:


Search a node with a particular data
void search()
{
struct node *ptr; Consider the below as the node structure:
int item,i=1,flag=0; struct node
ptr = head; {
if(ptr == NULL) int data;
{ struct node *next;
printf("\nEmpty List\n"); };
} Also, consider struct node *head as the start
else of a singly linked list.
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i);
flag=1;
break;
}
i++;
ptr = ptr -> next;
}
if(flag==0)
{
printf("Item not found\n");
}
}
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 45


Data Structures and Applications (CS204) Module 3

Concatenation of singly linked list


Consider a node in a singly linked list as under, struct node *head1 as the start of list1 and struct node
*head2 as the start of list2. The concat() function concatenates head1 and head2 lists and returns the
concatenated list.
struct node
{
int data;
struct node *next;
};
struct node *head1;
struct node *head2;
struct node* concat(struct node *head1,struct node *head2)
{
struct node *ptr;
if(head1 == NULL && head2==NULL)
{
printf("\n Concatenation is not possible because list is empty");
return NULL;
}
if(head1 == NULL)
{
return head2;
}
if(head2 == NULL)
{
return head1;
}
ptr=head1;
while (ptr->next!=NULL)
{
ptr = ptr -> next;
}
ptr->next=head2;
return head1;
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 46


Data Structures and Applications (CS204) Module 3

3.9 Header linked operations: Traversing, Searching, Insertion, and Deletion

A header linked list is a type of linked list that has a header node at the beginning of the list. In a header
linked list, HEAD points to the header node instead of the first node of the list.

The header node does not represent an item in the linked list. This data part of this node is generally used
to hold any global information about the entire linked list like information about number of nodes, max
node and min node values. The next part of the header node points to the first node in the list.

A header linked list can be divided into two types:

1) Grounded header linked list that stores NULL in the last node’s next field.

2) Circular header linked list that stores the address of the header node in the next part of the last
node of the list.

// Header Linked List Operations

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void begin_insert ();

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 47


Data Structures and Applications (CS204) Module 3

void begin_delete();
void display();
void search();
void main ()
{
int choice =0;
head = (struct node *) malloc(sizeof(struct node));
head->data=0;
head->next=NULL;
while(choice != 6)
{
printf("\n1.Insert in begining\n2.Delete from Beginning");
printf("\n3Search for an element\n4.Show\n5.Total nodes\n 6.Exit ");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
begin_insert();
break;
case 2:
begin_delete();
break;
case 3:
search();
break;
case 4:
display();
break;
case 5:
printf("\n Total number of nodes %d", head->data);
break;
case 6:
free(head)
exit(0);
break;
default:
printf("Please enter valid choice..");

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 48


Data Structures and Applications (CS204) Module 3

}
}
}
void begin_insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head->next;
head->next=ptr;
head->data++;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head->next== NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head->next;
head ->next= ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
head->data--;
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 49


Data Structures and Applications (CS204) Module 3

}
void search()
{
struct node *ptr;
int item,i=1,flag=0;
ptr = head->next;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i);
flag=1;
break;
}
i++;
ptr = ptr -> next;
}
if(flag==0)
{
printf("Item not found\n");
}
}

}
void display()
{
int count=0;
struct node *ptr;
ptr = head->next;
if(ptr == NULL)

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 50


Data Structures and Applications (CS204) Module 3

{
printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}

}
}
3.10 Polynomial using header linked list
Just like array representation of polynomials, we can also represent polynomials using linked lists.
Let us consider a node in a polynomial using linked list as under:

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 51


Data Structures and Applications (CS204) Module 3

A zero polynomial is a polynomial in which all the coefficients are zero. It is typically represented as:

P(x)=0.
Let us handle this zero polynomial with a header node introduced into each polynomial that is, each
polynomial, zero or nonzero, contains one additional node called header node (i.e), We will implement a
header linked list to represent the polynomial. Let us also consider the exp field of the header node to -1.
header 0 -1 NULL Zero Polynomial

Polynomial Node Creation:

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 52


Data Structures and Applications (CS204) Module 3

struct polynode
{
int coef;
int exp;
struct polynode* next;
};
Let us consider an insert() function that adds a new term (node) with coef as coefficient and exp as exponent
to the end of a polynomial represented as a header linked list called poly.
void insert(struct polynode* poly, int coef, int exp);
The addpolynomial() function adds two polynomials poly1 and poly2 represented as singly linked lists with
header nodes and the lists are not empty.
struct polynode* addpolynomial(struct polynode* poly1, struct polynode* poly2)
{
struct polynode *head = (struct polynode *) malloc(sizeof(struct polynode));
head->exp=-1;
head->coeff=0;
head->next=NULL;
struct polynode *result = head;
poly1=poly1->next;
poly2=poly2->next;
while (poly1 != NULL && poly2 != NULL)
{
if (poly1->exp == poly2->exp)
{
insert(result, poly1->coef + poly2->coef, poly1->exp);
poly1 = poly1->next;
poly2 = poly2->next;
}
else if (poly1->exp > poly2->exp)
{
insert(result, poly1->coef, poly1->exp);
poly1 = poly1->next;
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 53


Data Structures and Applications (CS204) Module 3

else
{
insert(result, poly2->coef, poly2->exp);
poly2 = poly2->next;
}
}
while (poly1!= NULL)
{
insert(result, poly1->coef, poly1->exp);
poly1 = poly1->next;
}
while (poly2!= NULL) {
insert(result, poly2->coef, poly2->exp);
poly2 = poly2->next;
}
return result;
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 54

You might also like