c linked list sample code

c linked list sample code

c linked list sample code

c linked list sample code is collection of linked list programs designed for beginners who want to learn how to implement linked list using c programming.

//Written by Adarsh Patel

/*    create the linklist in sort order and display on the screen     */
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct link_list
{
int no;
link_list *next;
};
typedef struct link_list node;
void main()
{
clrscr();
int n;
node *head=NULL;
node *insert_sort(node *,int);
void display(node *);

while(1)
{
printf(“\n\nEnter Number (-999 to exit) : “);
scanf(“%d”,&n);

if(n==-999)
break;
head = insert_sort(head,n);
printf(“\n”);
display(head);
}
display(head);
free(head);
getch();
}

node *insert_sort(node *list,int z)
{
node *p1,*p2,*p;
p1 = NULL;
p2 = list;

for(;p2->no < z && p2!= NULL;p2=p2->next)
p1 = p2;

p = (node *)malloc(sizeof(node));
p->no = z;

if(p1==NULL)
list = p;
else
p1 ->next = p;
p->next = p2;
return(list);
}
void display(node *list)
{
while(list->next!=NULL)
{
printf(”       +====+====\n”);
printf(“[%5u] | %4d | (%5u)—>\n”,list,list->no,list->next);
printf(”       +====+====\n”);
list = list->next;
}
printf(”       +====+====\n”);
printf(“[%5u] | %4d | (%5u)\n”,list,list->no,list->next);
printf(”       +====+====\n”);

}

//Written by Adarsh Patel

/*    This program create linklist. and display on screen
always last value is -999
1:    At several places inter related or logical data is there called
data structure.
When we define any structure variable it is required to precede it
with keyword struct.
This can be avoided by typedef    */
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct link_list
{
int no;
link_list *next;
};
typedef struct link_list node;    //now node work as link_list structure

void main()
{
clrscr();
node *head;
void create(node *);
void display(node *);

head = (node *) malloc (sizeof(node));
printf(“\nHead address : %u”,head);

create(head);
display(head);

getch();
}

void create(node *list)
{
printf(“\nInput the number (-999) to exit : “);
scanf(“%d”,&list->no);

if(list->no == -999)
list->next = NULL;
else
{
list->next = (node *) malloc (sizeof(node));
printf(“\n New Address : %u”,list->next);
create(list->next);    //Recursively call of function
//No panding statements are there
}
}
void display(node *list)
{
while(list->next!=NULL)
{
printf(“%2d => “,list->no);
list = list->next;
}
printf(“%3d”,list->no);
}

//Written by Adarsh Patel

/*CREATE THE DOUBLY LINKLIST BY PROMPTING USER POSITION
MAKE IT CIRCULAR
LAST IS A POINTER WHICH POINT OUT LAST NODE OF THE DOUBLY LINK LIST*/

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#include <process.h>
struct link_list
{
int no;
link_list *next,*prev;
};
typedef struct link_list node;

void main()
{
node *head,*temp,*q,*last;
char ch;
int ans,value;
clrscr();
printf(“Size of head %d”,sizeof(node));
node *insert();
void display(node *,node *); // ONE FOR HEAD AND ONE FOR LAST
head = NULL;

while (1)
{
fflush(stdin);
printf(“\n1. Start position”);
printf(“\n2. In between”);
printf(“\n3. Exit.”);
printf(“\nEnter your choice  :   “);
scanf(“%d”,&ans);

switch(ans)
{
case 1 :
{
temp = insert();
if (head!=NULL)
{
temp->next = head;
head->prev = temp;
head=temp;
}
else
{
head=temp;        // HEAD == NULL
last=temp;        // CHANGE IDENTIFY LAST NODE
}
break;
}
case 2 :
{
temp=insert();
q=head;
fflush(stdin);
printf(“Enter node value after the new node is inserted :  “);
scanf(“%d”,&value);

while (q->no!=value && q->next!=head)
q=q->next;       // && CHANGE NULL->HEAD

if (q->no!=value || q->next==head)  // CHANGE
last=temp;
if (q->next == NULL)
{
temp->prev = q;
q->next = temp;
}
else
{
temp->prev = q;
temp->next = q->next;
q->next->prev = temp;
q->next = temp;
}
break;
}//case2

case 3 :
exit(0);
}

last->next = head;   // CIRCULAR CHANGE
head->prev = last;   // CIRCULAR CHANGE
display(head,last);
} // END OF WHILE LOOP
} // END OF MAIN

node *insert()
{
node *tem;
tem=(node *) malloc(sizeof(node));
printf(“Enter value : “);
scanf(“%d”,&tem->no);
tem->next = NULL;
tem->prev = NULL;
return tem;
}
void display(node *q,node *temp)
{

while (temp!=q)
{
printf(”       +=====+====+=======+\n”);
printf(“[%5u]|%5u|%4d|(%5u)|<==>\n”,q,q->prev,q->no,q->next);
printf(”       +=====+====+=======+\n”);
q=q->next;
}     //while (temp!=q);

printf(”       +=====+====+=======+\n”);
printf(“[%5u]|%5u|%4d|(%5u)|\n”,q,q->prev,q->no,q->next);
printf(”       +=====+====+=======+\n”);
}

//Written by Adarsh Patel
//c linked list sample code
/*    This program create linklist. and then insert elemnt at begining
or other place */
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#include <process.h>
struct link_list
{
int no;
link_list *next;
};
typedef struct link_list node;
void main()
{
clrscr();
node *head,*temp,*q;
int ans,value;
char ch;
node *insert();
void display(node *);

//    head = (node *) malloc (sizeof(node));
head = NULL;
printf(“\nHead address : %u”,head);

while(1)
{
temp = insert();

printf(“\n1 : Insert at Starting Position”);
printf(“\n2 : Insert at in between”);
printf(“\n\nEnter Your Choice : “);
scanf(“%d”,&ans);

switch(ans)
{
case 1:
{
if(head!=NULL)
temp->next = head;
head = temp;
break;
}
case 2:
{
q = head;
printf(“\nEnter Node value after you want to add : “);
scanf(“%d”,&value);

while(q->no!=value)
q = q->next;

temp->next = q->next;
q->next = temp;
break;
}
}
display(head);
flushall();
printf(“\nDo you want to Continue…(Y/N) :”);
scanf(“%c”,&ch);

if(ch==’n’ || ch==’N’)
break;
}

getch();
}

node* insert()
{
node *tem;

tem = (node*) malloc(sizeof(node));
printf(“\nEnter the value : “);
scanf(“%d”,&tem->no);
tem -> next = NULL;
return(tem);
}
void display(node *list)
{
while(list->next!=NULL)
{
printf(“%2d –>”,list->no);
list = list->next;
}
printf(“%2d”,list->no);
}

//Written by Adarsh Patel
//c linked list sample code
/*    create the linklist in sort order and display on the scree     */
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#include <string.h>
struct link_list
{
char name[10];
link_list *next;
};
typedef struct link_list node;
void main()
{
clrscr();
char *value;
node *head=NULL;
node *insert_sort(node *,char*);
void display(node *);

while(1)
{
flushall();
printf(“\nInput the Name ( ‘x’ to exit) : “);
scanf(“%s”,value);

if( (strcmp(value,”x”))==0)
break;

head = insert_sort(head,value);
printf(“\n”);
display(head);
}
display(head);
free(head);
getch();
}

node *insert_sort(node *list,char *z)
{
node *p1,*p2,*p;
p1 = NULL;
p2 = list;

for(;strcmp(p2->name,z)<0 && p2!=NULL;p2=p2->next)
p1 = p2;

p = (node *)malloc(sizeof(node));
strcpy(p->name,z);
p->next = p2;

if(p1==NULL)
list = p;
else
p1 ->next = p;
p->next = p2;
return(list);
}
void display(node *list)
{
while(list->next!=NULL)
{
printf(”       +====+====\n”);
printf(“[%5u] | %10s | (%5u)—>\n”,list,list->name,list->next);
printf(”       +====+====\n”);
list = list->next;
}
printf(”       +====+====\n”);
printf(“[%5u] | %10s | (%5u)\n”,list,list->name,list->next);
printf(”       +====+====\n”);

}

//Written by Adarsh Patel
//c linked list sample code
/*    This program create linklist first and then insert or delete
the element at begining or at other places.      */
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#include <process.h>
struct link_list
{
int no;
link_list *next;
};
typedef struct link_list node;
void main()
{
clrscr();
int ans;
node *head=NULL;
void insert(node *);
node *insert_begin(node *);
void display(node *);
void delete1(node *);

while(1)
{
printf(“\n1 : Insert at Begining “);
printf(“\n2 : Insert at Other Places”);
printf(“\n3 : Delete at Begining”);
printf(“\n4 : Delete at Other Places”);
printf(“\n5 : Exit”);
printf(“\n\nEnter Your Choice : “);
scanf(“%d”,&ans);

if(ans==1)
head = insert_begin(head);
else if(ans==2)
insert(head);
else if(ans==3)
head = head->next;
else if(ans==4)
delete1(head);
else
break;
display(head);
}
free(head);
getch();
}

void insert(node *list)
{
int place;
node *new1;

printf(“\nAfter which element new element is inserted : “);
scanf(“%d”,&place);

while(list->no!=place)
list=list->next;

new1 = (node*) malloc(sizeof(node));
printf(“\nEnter the value : “);
scanf(“%d”,&new1->no);
new1->next = list->next;
list->next = new1;

}
void display(node *list)
{
while(list->next!=NULL)
{
printf(“%2d –>”,list->no);
list = list->next;
}
printf(“%2d”,list->no);
}

node *insert_begin(node *list)
{
node *new1;
new1 = (node *) malloc(sizeof(node));

printf(“\nEnter New element no : “);
scanf(“%d”,&new1->no);
new1->next = list;
return(new1);

}
void delete1(node *list)
{
int place;
printf(“\nWhich element do you want to delete : “);
scanf(“%d”,&place);

while(list->next->no!=place && list->next!=NULL)
list = list->next;

if(list->next->next==NULL)
list->next = NULL;
else
list->next = list->next->next;
}

hope you like my c linked list sample code article. Check out few more article by adarsh patel.

c programming tutorial

Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative