Showing posts with label Linked List. Show all posts
Showing posts with label Linked List. Show all posts

Friday, June 22, 2012

Searching values in Link List using C Program


In this program we will create five nodes and then the linked list will be traversed one after another to display the value stored within the node.
Our next program will create a linked list ( size depends on the user ) and each node will contain the Name , Surname , Address and Telephone number .Then the user has to enter a Name which will be searched in the linked list. If found the details record against the name will be displayed, otherwise a not found message will be displayed.

# include < stdio.h >
# include < malloc.h >
struct link
{
char name[100];
char surname[100];
char add[100];
long ph;
struct link *next;
};
int x=0;
struct link *node;
void search(char ch[],struct link *n)
{
node=n - > next;
for(; node != NULL; node = node->next)
{
if(strcmp(node-> name,ch)==0)
{
x++;
printf("Surname is %s \n", node->surname);
printf("Address is %s \n",node-> add);
printf("Phone number is %ld \n",node->ph);
}
}
if(x == 0)
printf("Item not found in the list \n");
}
void get(struct link *n)
{
char ch='y';
node=n;
while(ch !='n')
{
node->next = (struct link* ) malloc (sizeof (struct link));
node = node->next;
node->next =NULL;
printf("\n Enter the name: -");
scanf("%s",node->name);
printf("\n Enter the surname: -");
scanf("%s",node->surname);
printf("\n Enter the address: -");
scanf("%s",node->add);
printf("\n Enter the Telephone number: -");
scanf("%ld",&node->ph);
printf("\n Input choice n for break: ");
ch = getche();
}
}
void main()
{
struct link *start=NULL;
char name[15];
clrscr();
get(start);
printf("\nEnter the name to search :");
scanf("%s",name);
search(name,start);
getch();
}

Thursday, June 14, 2012

Creating a linked list with common node values of two different linked lists


In this C program on linked list, two different linked lists will be created with different number of nodes. A third linked list will be created from those two linked lists taking the nodes with common values.

Step by step analysis of the linked list program

 Two linked lists are created separately with varying number of nodes. User enters the number of nodes of each linked list while creating them. The linked lists are displayed after creating. Function ‘void make(struct list *)’ is used to create linked list and another function ‘void show(struct list *)’ is defined in this program to display linked list. Function ‘struct list *com(struct list *,struct list *,struct list *)’ is defined in this program to do the final job, searching the common value nodes from each of the linked lists and creating the third linked list.

Codes of this linked list program

  #include< stdio.h>
#include< stdlib.h>
struct list
{
int info;
struct list *next;
};
int i=0;
void make(struct list *);
void show(struct list *);
struct list *com(struct list *,struct list *,struct list *);
void main()
{
struct list *temp,*head,*in=NULL;
clrscr();
temp=(struct list *)malloc(sizeof(struct list));
make(temp);
printf("\ndisplaying FIRST linked list\n");
show(temp);
head=(struct list *)malloc(sizeof(struct list));
make(head);
printf("\ndisplaying SECOND linked list\n");
show(head);
in=com(temp,head,in);
printf("\nDisplaying the final linked list\n");
show(in);
getch();
}
void make(struct list *temp)
{
int n,i;
printf("\n\n enter the no of nodes you want to create\n");
scanf("%d",&n);
for(i=1;i< n;i++)
{
printf("entering value in %d node\n",i);
scanf("%d",&temp->info);
temp->next=(struct list *)malloc(sizeof(struct list));
temp=temp->next;
}
printf("Entering value in %d node\n",i);
scanf("%d",&temp->info);
temp->next=NULL;
}
void show(struct list *temp)
{
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
}
struct list *com(struct list *temp,struct list *h,struct list *in)
{
struct list *head,*head1;
while(temp!=NULL)
{
head=h;
while(head!=NULL)
{
if(temp->info==head->info)
{
in->next=(struct list *)malloc(sizeof(struct list));
in=in->next;
in->info=temp->info;
in->next=NULL;
if(i==0)
head1=in;
i++;
}
head=head->next;
}
temp=temp->next;
}
return head1;
}

c(siz: � s r 8� �� );
char name[15];
int i=0;
clrscr();
get(start);
display(start);
while(i==0)
{
printf("\nEnter the name whose record is to be changed :");
scanf("%s",name);
i=search(name,start);
}
display(start);
getch();
}

Program to display a linked list from front side and reverse side
#include
#include
struct tag
{
int i;
struct tag *next;
};
struct tag *start=NULL,*node;
int x=1,flag=0,flag1;
void create()
{
char ch;
node=start;
puts("Want to create nodes(y/n):-");
scanf("%c",&ch);
while(ch!='n')
{
node->next=(struct tag *)malloc(sizeof(struct tag));
node=node->next;
node->next=NULL;
printf("\nEnter value::");
scanf("%d",&node->i);
flag++;
puts("Want to create nodes(y/n):-");
ch=getche();
}
flag1=flag;
}
void dis()
{
node=start->next;
while(node)
{
printf("%d\n",node->i);
node=node->next;
}
}
void dis1()
{
while(x<=flag1)
{
flag=flag1-x;
node=start->next;
while(flag)
{
node=node->next;
flag--;
}
printf("%d\n",node->i);
x++;
}
}
void main()
{
clrscr();
create();
printf("\nValues from front side are as follows.\n");
dis();
printf("\nLinked List displayed from reverse\n");
dis1();
getch();
}

C program on modification of values in a linked list


Suppose a linked list is created with four fields in the information part and the user wants to modify the contents of a node of the linked list.


Creating the linked list and searching the linked list have been discussed earlier. This program on linked list also involves these two operations. After creating the list searching is required for modification of values. On successful seaching the values of the specific node is modified.


The program would be as follows :-

# include < stdio.h>
# include < stdlib.h>
struct link
{
char name[100];
char surname[100];
char add[100];
long ph;
struct link *next;
};
int x=0;
struct link *node;
int search(char ch[],struct link *n)
{
char c;
for(node=n; node->next != NULL; node = node->next)
{
if(strcmp(node-> name,ch)==0)
{
x++;
fflush(stdin);
printf("\nWant to change the surname ?(y/n)");
scanf("%c",&c);
fflush(stdin);
if(c!='n')
{
printf("\nEnter the Surname :-");
gets(node->surname);
}
printf("\nWant to change the address ?(y/n)");
scanf("%c",&c);
fflush(stdin);
if(c!='n')
{
printf("\nEnter the Address :-");
gets(node->add);
}
printf("\nWant to change the phone number ?(y/n)");
scanf("%c",&c);
fflush(stdin);
if(c!='n')
{
printf("\nEnter the Phone number :-");
scanf("%ld",&node->ph);
}
}
}
if(x == 0)
return 0;
else
return 1;
}
void display(struct link *n)
{
for(node=n; node->next != NULL; node = node->next)
{
printf("\n%s\n",node->name);
printf("%s\n",node->surname);
printf("%s\n",node->add);
printf("%ld\n",node->ph);
printf("\n*****************\n");
}
}
void get(struct link *n)
{
char ch='y';
node=n;
while(ch !='n')
{
fflush(stdin);
printf("\n Enter the name: -");
scanf("%s",node->name);
printf("\n Enter the surname: -");
scanf("%s",node->surname);
printf("\n Enter the address: -");
scanf("%s",node->add);
printf("\n Enter the Telephone number: -");
scanf("%ld",&node->ph);
node->next = (struct link* ) malloc (sizeof (struct link));
node = node->next;
node->next =NULL;
printf("\n Input choice n for break: ");
ch=getche();
}
}
void main()
{
struct link *start = (struct link *)malloc(sizeof(struct link));
char name[15];
int i=0;
clrscr();
get(start);
display(start);
while(i==0)
{
printf("\nEnter the name whose record is to be changed :");
scanf("%s",name);
i=search(name,start);
}
display(start);
getch();
}

Program to display a linked list from front side and reverse side
#include
#include
struct tag
{
int i;
struct tag *next;
};
struct tag *start=NULL,*node;
int x=1,flag=0,flag1;
void create()
{
char ch;
node=start;
puts("Want to create nodes(y/n):-");
scanf("%c",&ch);
while(ch!='n')
{
node->next=(struct tag *)malloc(sizeof(struct tag));
node=node->next;
node->next=NULL;
printf("\nEnter value::");
scanf("%d",&node->i);
flag++;
puts("Want to create nodes(y/n):-");
ch=getche();
}
flag1=flag;
}
void dis()
{
node=start->next;
while(node)
{
printf("%d\n",node->i);
node=node->next;
}
}
void dis1()
{
while(x<=flag1)
{
flag=flag1-x;
node=start->next;
while(flag)
{
node=node->next;
flag--;
}
printf("%d\n",node->i);
x++;
}
}
void main()
{
clrscr();
create();
printf("\nValues from front side are as follows.\n");
dis();
printf("\nLinked List displayed from reverse\n");
dis1();
getch();
}

Tuesday, June 12, 2012

C Program on linked list using address of list pointer


I have posted number of programs on new node insertion and deletion of node from linked list. Those programs were either using header node in linked list or normal linked list. In linked list with header node the insertion at first location or deletion from the first location can be carried out easily but in linked list without header node, inserting new node at first location or deleting node from first location invokes some precautions. In both the cases the function is defined as returning address of the first node of the linked list after the operation is completed.

Today I will show you how to execute the operations of deletion and insertion on linked list even at the first location without returning values from the function. I have passed the address of the pointer holding the address of first node of the linked list to each function. So after deletion or insertion of node at starting location of the linked list, the address of the first node is assigned to the pointer to pointer and no need to return the new address of the first node of the linked list to void main.


# include < stdio.h>
# include < stdlib.h>
struct list
{
int age;
struct list *next;
};
struct list *new1,*node,*p;
void create(struct list **s)
{
char ch;
node=*s;
printf("\nWant to create a node(y/n):");
ch=getche();
while (ch != 'n')
{
node->next = (struct list *) malloc(sizeof(struct list));
node = node->next;
printf("\n Enter the numeric value:- ");
scanf("%d",&node->age);
printf("\n Enter choice--'n' for break: ");
ch = getche();
}
node->next = NULL;
}
void displayList (struct list **s)
{
node=*s;
while (node!=NULL)
{
printf("\n%d", node->age);
node = node->next;
}
printf("\n");
}
void delA(struct list **s)
{
int c=1,counter;
printf("\nEnter the location of the node to be deleted:");
scanf("%d",&counter);
if(c==counter)
{
node=(*s)->next;
free(*s);
s=&node;
}
else
{
node=*s;
p=node->next;
while(p->next!=NULL)
{
if(c+1==counter)
break;
c++;
node=node->next;
p=p->next;
}
node->next=p->next;
free(p);
}
}
void insA(struct list **s)
{
int c=1,counter;
printf("\nEnter the location where to insert the new node:");
scanf("%d",&counter);
new1=(struct list *) malloc(sizeof(struct list));
printf("\n Enter the numeric value:- ");
scanf("%d",&new1->age);

if(c==counter)
{
new1->next=*s;
s=&new1;
}
else
{
node=*s;
p=node->next;
while(p!=NULL)
{
if(c+1==counter)
break;
c++;
node=node->next;
p=p->next;
}
node->next=new1;
new1->next=p;
}
}

void main()
{
struct list *start;
clrscr();
start=(struct list *) malloc(sizeof(struct list));
printf("\n Enter the numeric value:- ");
scanf("%d",&start->age);

create(&start);
printf("\n Created list is as follows\n");
displayList(&start);
printf("\nDeleting any node\n");
delA(&start);
printf("\n now the list is\n");
displayList(&start);
printf("\nInserting new node\n");
insA(&start);
printf("\n now the list is\n");
displayList(&start);
getch();
}

Monday, June 11, 2012

Sorting program on linked list


In this program a linked list will be created first and after that the linked list will be sorted in ascending in ascending order according to values in nodes.

How to proceed in this linked list sorting program

Create a linked list using a do while loop and assign values to the information part variable of the nodes. Create the first node of the linked list in the main () function and the other nodes are to be created inside a function ‘void create (struct link *)’.

Use another function ‘void sort (struct tag *)’ where selection sort technique can be used for sorting the elements of the linked list according to their values.

The third function ‘void display (struct tag *)’ in this program will display the value of the nodes in the linked list.

After creating the linear linked list, display the list and again display after sorting the elements.

Here are the programming codes of the program

# include < stdio.h>
# include < stdlib.h>
struct link
{
int info;
struct link *next;
};
int i;
int number;
struct link *p,*node;
void create(struct link *n)
{
char ch;
i=1;
node=n;
printf("\n Enter value for node: %d: ",i);
scanf("%d", &node->info);
fflush(stdin);
node->next = NULL;
do
{
i=i+1;
node->next = (struct link* ) malloc(sizeof(struct link));
node = node->next;
printf("\n Enter value for node: %d: ",i);
scanf("%d", &node->info);
node->next = NULL;
printf("\n Input choice n for break: ");
ch = getche();
} while(ch != 'n');

printf("\n Total nodes = %d", i);
}
void display(struct link *n)
{
node = n;
while (node)
{
printf(" %d", node->info);
node = node->next;
}
}
void sort(struct link *n)
{
int t;
p=n;
while(p)
{
node=p->next;
while(node)
{
if(p->info>node->info)
{
t=p->info;
p->info=node->info;
node->info=t;
}
node=node->next;
}
p=p->next;
}
}
void main()
{
struct link *start;
clrscr();
start=(struct link *)malloc(sizeof(struct link));
create(start);
printf("\n Created list is as follows:\n");
display(start);
sort(start);
printf("\n After sorting,list is as follows:\n");
display(start);
getch();
}

Saturday, June 9, 2012

Use of header node in Linked List


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();
}

Monday, June 4, 2012

Appending one linked list to another


Here is another program on linked list. The following program will create two separate linked list and then the second list will be appended at the end of the first linked list .

#include < stdio.h >
#include < stdlib.h >
#include< string.h >
struct link
{
char data[30];
struct link *next;
};
struct link *node;
void display(struct link *n)
{
node=n;
while (node)
{
printf(" %s\n", node->data);
node = node->next;
}
}
void create(struct link *n)
{
int i = 1;
char ch='y';
node=n;
printf("\n Enter the String- %d:- ", (i+1));
scanf("%s", &node->data);
while(ch != 'n')
{
node->next = (struct link * ) malloc(sizeof(struct link));
node = node->next;
node->next = NULL;
fflush(stdin);
printf("\n Enter the String- %d:- ", (i+1));
scanf("%s", &node->data);
fflush(stdin);
printf("\n Enter choice--'n' for break: ");
ch = getche();
i ++;
}
printf("\n Total nodes = %d", i);
}
void join(struct link *one, struct link *two, struct link *three)
{
while(one)
{
strcpy(three->data,one->data);
one = one->next;
three->next = (struct link* ) malloc(sizeof(struct link));
three = three->next;
}
while(two)
{
strcpy(three->data,two->data);
two = two->next;
three->next = (struct link*)malloc(sizeof(struct link));
three = three->next;
}
three->next = NULL;
}
void main()
{
struct link *one, *two, *three;
one = (struct link *)malloc(sizeof(struct link));
two = (struct link *)malloc(sizeof(struct link));
three = (struct link *)malloc(sizeof(struct link));
clrscr();
printf("Creating First linked list.\n");
create(one);
printf("\n Original list ONE is as follows:\n");
display(one);
printf("\nCreating Second linked list.\n");
create(two);
printf("\n Original list TWO is as follows:\n");
display(two);
join(one,two,three);
printf("\n Original list ONE + TWO is as follows:\n");
display(three);
getch();
}

Technical analysis of this appending linked list program

 Three list pointers are used in this linked list program. All the pointers are assigned address of three separate locations ( values are not assigned) and this is done in the main () function. User defined function ‘void create(struct link*)’ is used to create the two separate linked lists. Another function defined in this program ‘void display(struct link*)’ is used to display the linked lists separately. Appending the linked lists are done by function ‘void join(struct link*, struct link*, struct link*)’

Subscribe via email

Enter your email address:

Delivered by FeedBurner