Header Ads

C program to implement linked list using struct

Structure in C programming language is a composite data type that can hold variables of more than one data type and keyword struct is use to declare structure in C.

Syntax to define struct in C programming language is as:

struct [structure_tag]
{
    //datatype variable 1
    //int x;
    //char s;
    ...
}[structure_variables];
I will assume that you know how to implement struct normally in C programming language because here I am going to cover how to implement struct using linked list

C Program for struct using linked list:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

//...............................................Function Creation...........................................//

void createlinkedlist();
void display();
void insert_at_first();
void delete_from_first();
void delete_from_end();
void insert_at_selected_position();
void delete_entire_list();
void reverse_nodes();

//.........................................structure node...............................................//

struct node
{
int roll;
char name[30];
struct node *next;
};
struct node *head;

//......................................................MAIN..............................................//

int main()
{

int ch;
do
{

printf("\n\n\n\n\n\n\t\t\t1.Create Linked list");
printf("\n\t\t\t2.Display List");
if(head!=NULL)
{

printf("\n\t\t\t3.Delete From First\t");
printf("\n\t\t\t4.Delete From end\t");
printf("\n\t\t\t5.Insert at first\t");
printf("\n\t\t\t6.Insert at selected position");
printf("\n\t\t\t7.Delete entire List");
printf("\n\t\t\t8.Reverse List");
}

printf("\n\t\t\t9.Exit\t");


scanf("%d",&ch);
switch(ch)
{
case 1:
createlinkedlist();
break;
case 2:
display();
break;
case 9:
exit (0);
break;
case 5:
insert_at_first();
break;
case 3:
delete_from_first();
break;
case 4:
delete_from_end();
break;
case 6:
insert_at_selected_position();
break;
case 7:
delete_entire_list();
break;
case 8:
reverse_list();
break;
default:
printf("Wrong choice Try again......");
}
}while(ch!=3);

}

//............................................Create linked list.................................................//

void createlinkedlist()
{
system("cls");
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n\n\t\t\tRoll : ");
scanf("%d",&newnode->roll);
printf("\t\t\tName : ");
scanf("%s",&newnode->name);
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}

// ......................................................DISPLAY............................................//

void display()
{
system("cls");
struct node *temp;
printf("\n\n\t\t\tRoll\t\tName\n");
if(head!=NULL)
{
for(temp=head;temp!=NULL;temp=temp->next)
printf("\t\t\t%d\t\t%s\n",temp->roll,temp->name);


}
else
{
printf("\n\t\t\tList is empty Boss... %c %c ",2,2);
}
}

//............................................Insert At FIrst.................................................//

void insert_at_first()
{
system("cls");
struct node *temp,*newnode;
newnode=(struct student *)malloc(sizeof(struct node));
printf("\n\n\t\t\tRoll : ");
scanf("%d",&newnode->roll);
printf("\t\t\tName : ");
scanf("%s",&newnode->name);
temp=head;
head=newnode;
newnode->next=temp;

return 0;
}

//..............................................Delete from first.....................................................//

void delete_from_first()
{
system("cls");
struct node *temp;
if(head==NULL)
{
printf("\n\t\t\tList is empty Boss... %c %c ",2,2);
}
else
{
temp=head;
free(head);
head=temp->next;
printf("\n\n\t\t\tFirst list deleted from node...");
}

return 0;

}

//..............................................Delete from end................................................//

void delete_from_end()
{
system("cls");
struct node *temp,*newtemp;
temp=head;

while(temp->next!=NULL)
{
newtemp=temp;
temp=temp->next;
if(temp->next==NULL)
newtemp->next=NULL;
}
free(temp);

printf("\n\n\t\t\tLast node is deleted....");
return 0;

}

//..................................................Insert at selected position....................................//

void insert_at_selected_position()
{
struct node *temp;
struct node *new_temp,*temp_copy;
temp=head;
int position,i;
printf("\n\n\t\t\tEnter the roll before you want to insert\t");
scanf("%d",&position);
for(i=1;i<position;i++)
{
temp=temp->next;
}
temp_copy=temp->next;
new_temp=(struct node *)malloc(sizeof(struct node));
temp->next=new_temp;
printf("\n\n\t\t\tRoll : ");
scanf("%d",&new_temp->roll);
printf("\t\t\tName : ");
scanf("%s",&new_temp->name);
new_temp->next=temp_copy;
return 0;
}

//..................................................free entire list........................................//

void delete_entire_list()
{
system("cls");
struct node *temp,*had;
temp=head;
if(temp->next==NULL)
{
free(temp);
}
else
{
while(temp!=NULL)
{
had=temp;
temp=temp->next;
free(had);
}
}
head=NULL;


return 0;
}

//.......................................Reverse List....................................//

void reverse_list()
{
system("cls");
if(head->next==NULL)
{
printf("\n\n\tsorry BOSS %c reverse not possible...",1);
}
else
{
struct node *p1,*p2,*p3;
p1=head;
p2=p1->next;
p3=p1->next->next;
p1->next=NULL;


while(p3!=NULL)
{

p2->next=p1;
p1=p2;
p2=p3;
p3=p3->next;

}
p2->next=p1;
head=p2;
}

}

No comments