Dsa Mod3 - Part4
Dsa Mod3 - Part4
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.
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.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void begin_insert ();
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..");
}
}
}
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--;
}
}
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)
{
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:
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
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;
}
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;
}