FreePrograms


C language is a middle level language. It is a structured language. C language is a case sensitive language. All syntax written in c language is in lower case. C is the basis for C++.
  CONTACT US   ABOUT US   PRIVACY  DISCLAIMER

FREE C PROGRAMS 

What is C C vs C++ vs Java Program Structure Data Types in C Basic Rules of C & C++
Functions in C If, Else Conditions Loops in C Switch Case Arrays
Pointers Structures in C Strings in C Command Line Arguments Type Casting
Linked Lists Recursion Binary Trees Inheritance Multiple Inheritance
Templates File I/O Object Oriented Programming Data Structures in C C interview Questions

..

Simple Linked List

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct linklist
{
int info;
struct linklist *next;
};
struct linklist *start=NULL;
int x=0,c=0;
void create_node();
void display();
void insertion_info();
void split();
void deletion_before();
void deletion_after();
void deletion_info();
void insertion_before();
void insertion_after();
void insertion_after()
{
struct linklist *newnode,*p,*t;
int data,b,i;
if(start!=NULL)
{
printf("\nthe total no. of nodes are= %d",c);
printf("\nenter the no. of node after which the new node required to be inserted");
scanf("%d",&b);
if(b>c)
{
printf("wrong choice entered");
goto z;
}
}
printf("\nenter the information of the newnode");
scanf("%d",&data);
newnode=(struct linklist *)malloc(sizeof(struct linklist));
c++;
newnode->info=data;
p=start;
if(start==NULL)
{
newnode->next=NULL;
start=newnode;
}
else
{
for(i=1;i<=b;i++)
{
t=p;
p=p->next;
}
t->next=newnode;
newnode->next=p;
}
z:}
void create_node()
{
int m,i;
struct linklist *newnode;
printf("\nenter the no. of nodes to be inserted");
scanf("%d",&x);
if(x<1)
printf("\nentered a wrong no.");
else
{
for(i=1;i<=x;i++)
{
printf("\nenter the information of the %d node",i);
fflush(stdin);
scanf("%d",&m);
newnode=(struct linklist *)malloc(sizeof(struct linklist));
newnode->info=m;
newnode->next=start;
start=newnode;
}
c=c+x;
}
}
void display()
{
struct linklist *p;
p=start;
if(p==NULL)
printf("link list is empty");
else
{
while(p!=NULL)
{
printf("\t%d",p->info);
p=p->next;
}
}
}
void insertion_before()
{
struct linklist *p,*t,*newnode;
int n,s,i;
if(start!=NULL )
{
printf("\nthe total no. of nodes are= %d",c);
printf("\nenter the no. before which the node has to be inserted");
scanf("%d",&n);
if(n>c)
{
printf("wrong choice");
goto x;
}
}
printf("\nenter the information of the inserted node");
scanf("%d",&s);
p=start;
newnode=(struct linklist*)malloc(sizeof(struct linklist));
c++;
newnode->info=s;
if(start==NULL || n==1)
{
newnode->next=start;
start=newnode;
}
else
{
for(i=1;i<n;i++)
{
t=p;
p=p->next;
}
t->next=newnode;
newnode->next=p;
}
x:}
void insertion_info()
{
struct linklist *newnode,*p,*t;
int i,j,a,s,q,m;
printf("\nenter the information to be inserted");
scanf("%d",&s);
newnode=(struct linklist*)malloc(sizeof(struct linklist));
c++;
newnode->info=s;
t=start;
p=t->next;
if(start==NULL || t->info>s)
{
newnode->next=start;
start=newnode;
}
else
{
for(i=1;i<c;i++)
{
if(i!=1)
{
t=t->next;
p=t->next;
}
for(j=i+1;j<=c;j++)
{
q=t->info;
m=p->info;
if(q>m)
{
a=q;
q=m;
m=a;
}
p=p->next;
}
}
p=start;
while(p->info<s)
{
t=p;
p=p->next;
if(p==NULL)
{
t->next=newnode;
newnode->next=p;
break;
}
}
if(p!=NULL)
{
t->next=newnode;
newnode->next=p;
}
}
}
void split()
{
struct linklist *v,*t;
int n,i;
v=start;
printf("\nenter the node no. after which the linked list is to be splitted");
scanf("%d",&n);
if(start==NULL)
printf("\nthe linked list is empty");
else
{
printf("\nthe splitted linked lists are->");
for(i=1;i<=n;i++)
{
t=v;
v=v->next;
}
t->next=NULL;
display();
printf("\n\t");
while(v!=NULL)
{
printf("\t%d",v->info);
v=v->next;
}
}
}
void deletion_before()
{
struct linklist *p,*t;
int j,i;
p=start;
if(start!=NULL)
{
printf("\nenter the no. of node before which the node has to be deleted");
scanf("%d",&j);
}
if(start==NULL)
printf("\nlinked list is empty");
else if(j==1)
printf("\nwrong choice");
else if(j==2)
{
start=p->next;
free(p);
}
else
{
for(i=1;i<j-1;i++)
{
t=p;
p=p->next;
}
t->next=p->next;
free(p);
}
}
void deletion_after()
{
struct linklist *p,*t;
int y,i;
p=start;
if(start!=NULL)
{
printf("\nenter the no. of node before which the node has to be deleted");
scanf("%d",&y);
}
if(start==NULL)
printf("\nlinked list is empty");
else
{
for(i=1;i<=y;i++)
{
t=p;
p=p->next;
}
t->next=p->next;
free(p);
}
}
void deletion_info()
{
int s;
struct linklist *p,*t;
p=start;
if(p==NULL)
printf("\nlinked list is empty");
else
{
printf("\nenter the information to be deleted");
scanf("%d",&s);
while(p->info !=s)
{
if(p==NULL)
{
printf("\ninformation is not present in the linked list");
break;
}
t=p;
p=p->next;
}
if(p==start)
{
start=p->next;
free(p);
}
t->next=p->next;
free(p);
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\nenter 1. for creation of node");
printf("\nenter 2. for display of linked list");
printf("\nenter 3. for insertion before a node");
printf("\nenter 4. for insertion after a node");
printf("\nenter 5. for insertion according to information of the node");
printf("\nenter 6. for splitting the linked list");
printf("\nenter 7. for deletion before a node");
printf("\nenter 8. for deletion after a node");
printf("\nenter 9. for deletion according to information");
printf("\nenter 10. for exit");
printf("\nenter your choice");
fflush(stdin);
scanf("%d",&ch);
switch(ch)
{
case 1:
create_node();
break;
case 2:
display();
break;
case 3:
insertion_before();
break;
case 4:
insertion_after();
break;
case 5:
insertion_info();
break;
case 6:
split();
break;
case 7:
deletion_before();
break;
case 8:
deletion_after();
break;
case 9:
deletion_info();
break;
case 10:
exit(0);
default:
printf("\nentered a wrong choice");
}
}
getch();
}