Sometimes it is desirable to keep an extra node at the front of a linked list. Such a node does not represent an item in the list. The information part of such node is used to store information of a different type than the values stored in the list nodes. For example a header node may contain the number of nodes in a list. A list which contains this type of node is called header linked list.(including the header node ). In a header linked list where the next pointer of the last node is pointing to NULL is called a grounded header linked list.
# include < stdio.h >
# include < stdlib.h >
struct link
{
int info;
struct link *next;
};
int i=0;
struct link *node,*new1;
void create(struct link *n)
{
char ch='y';
node=n;
while(ch!='n')
{
node->next = (struct link* ) malloc(sizeof(struct link));
node=node->next;
printf("\n Enter the value %d:- ", i+1);
scanf("%d", &node->info);
node->next = NULL;
fflush(stdin);
printf("\n Enter choice--'n' for break: ");
ch = getche();
i++;
}
printf("\n Total nodes = %d\n", i);
n->info = i; /* Assign total number of nodes to the header node */
}
void display(struct link *n)
{
int count = n->info;
printf("The address and the value of the header node :-");
printf("\n 0x%x--%d", n,n->info);
node = n->next;
printf("\n The nodes are as follows:-\n");
while (count)
{
printf("\n 0x%x--%d", node, node->info);
node = node->next;
count --;
}
printf("\n");
}
void main()
{
struct link *start;
clrscr();
start=(struct link *)malloc(sizeof(struct link));
create(start);
display (start);
getch();
}
Here the header node does not contain any values in this program but it can hold values. Any operations like insertion or deletion of nodes will be performed after the header node.
Program on linked list with header node which contains information of the list
# include < stdio.h >
# include < stdlib.h >
struct link
{
char name[100];
char street[100];
char city[100];
char ph[20];
struct link *next;
};
struct link *node,*new1;
void create(struct link *n)
{
char ch='y';
node = n;
printf("\n Now store some information about your list:-\n ");
gets(node->name);
strcpy(node->street," ");
strcpy(node->city," ");
strcpy(node->ph," ");
while(ch != 'n')
{
node->next = (struct link* ) malloc(sizeof(struct link));
node = node->next;
fflush(stdin);
puts("Enter the Name:- ");
gets(node->name);
fflush(stdin);
puts("Enter the Street name:- ");
gets(node->street);
puts("Enter the City:- ");
gets(node->city);
puts("Phone Number:- ");
gets(node->ph);
fflush(stdin);
node->next = NULL;
printf("\n Enter choice--'n' for break: ");
ch = getche();
clrscr();
}
}
void display(struct link *n)
{
node=n;
printf("\nHere is the header node value:-\n");
printf("%s\n", node->name);
printf("\nNode values:-");
node=node->next;
while (node)
{
printf("\nPress ant key........\n");
getch();
clrscr();
printf("%s\n",node->name);
printf("%s\n", node->street);
printf("%s\n", node->city);
printf("%s\n", node->ph);
node = node->next;
}
}
void main()
{
struct link *start;
clrscr();
start=(struct link *)malloc(sizeof(struct link));
create(start);
display (start);
getch();
}
No comments:
Post a Comment