Thursday 15 May 2014

Singly Linked List with Program

Singly Linked List with Program

Node Stucture:

in singly linked list data are stored in node and node have the below structure

Where 
                          data part contain data value
                          link part contain link to the next node
                          address part contain address of node


creation of Linked List:

the creation of linked list have following structure

the head node always points to the first node and the last node always contain NULL value as displayed in the given picture

Insertion of New Node:

when we try to insert a node there are three possibility

1. insert at Beginning


 2. Insert at Last(end):

3.insert at Given Position 



Deletion of Node:

when we are delete a node than there are three possibility

1.Delete From Beginning:



2.Delete  From Last(END):

3.Delete from at Given Position:



-: Program of Singly Linked list :-
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define SIZE 15

struct singly
{
int roll_no;
char name[SIZE];
int age;
struct singly *next;
}*head,*lastnode;
void create()
{
int no=1;
struct singly *newnode,*node;
node=head;
while(1)
{
printf("enter roll no(-0 for exit):-");
fflush(stdin);
scanf("%d",&no);

if(no==-0)
break;
else
{
newnode=(struct singly *)malloc(sizeof(struct singly));
newnode->roll_no=no;
printf("\nenter student name:-");
fflush(stdin);
gets(newnode->name);
printf("enter age:-");
scanf("%d",&newnode->age);
newnode->next=NULL;
node->next=newnode;
lastnode=node;
if(head==NULL)
{
head=node;
}
node=node->next;
}
}
}
void display(struct singly *temp)
{
printf("\n\n==========================================\n");
printf("  roll no   name\t\t age");
printf("\n===========================================\n");

while(temp!=NULL)
{
printf("  %3d        %-15s\t%3d\n",temp->roll_no,temp->name,temp->age);
temp=temp->next;
}
}



void insert(struct singly *node)
{
int no,pos,count=0;
struct singly *newnode,*prevnode,*nextnode;
clrscr();
printf("1 for insert at first\n");
printf("2 for insert at end\n");
printf("3 for insert at pth position\n");
printf("\n\nenter your choise:-");
fflush(stdin);
scanf("%d",&pos);
printf("\n\nenter roll value:-");
fflush(stdin);
scanf("%d",&no);
newnode=(struct singly *)malloc(sizeof(struct singly));
newnode->next=NULL;
newnode->roll_no=no;
printf("\nenter student name:-");
fflush(stdin);
gets(newnode->name);
printf("enter age:-");
scanf("%d",&newnode->age);
switch(pos)
{
case 1:
newnode->next=head;
head=newnode;
break;
case 2:
while(node!=NULL)
{
lastnode=node;
node=node->next;
}
lastnode->next=newnode;
break;
case 3:
nextnode=node;
printf("enter position:-");
fflush(stdin);
scanf("%d",&pos);
if(pos==0||(pos>1 && nextnode==NULL))
{
printf("\n\nwrong choise.....");
getch();
}
else
{
while(count<pos && nextnode!=NULL)
{
prevnode=nextnode;
nextnode=nextnode->next;
count++;
}
newnode->next=nextnode;
prevnode->next=newnode;
}

break;
default:
printf("\n\n\nwrong choise....");
getch();
}


}
void delet(struct singly *node)
{
struct singly *delnode,*lastnode;
int no,pos,count=0;
clrscr();
printf("1 for delete first\n");
printf("2 for delete last\n");
printf("3 for delete from pth position\n");
printf("\n\nenter your choise:-");
fflush(stdin);
scanf("%d",&pos);
switch(pos)
{
case 1:
delnode=head;
head=head->next;
free(delnode);
break;
case 2:
delnode=head;
while(delnode->next!=NULL)
{
lastnode=delnode;
delnode=delnode->next;
}
lastnode->next=NULL;
free(delnode);
break;
case 3:
delnode=head;
printf("enter position:-");
fflush(stdin);
scanf("%d",&pos);
if(pos==0||(pos>1 && delnode==NULL))
{
printf("\n\nwrong choise.....");
getch();
}
else
{
while(count<pos && delnode!=NULL)
{
lastnode=delnode;
delnode=delnode->next;
count++;
}
lastnode->next=delnode->next;
free(delnode);
}
break;
default:
printf("\n\n\nwrong choise....");
getch();
}



}

void sort_node(struct singly *node){
int no,choice,flag=0;
char name[SIZE]={'\0'};
struct singly *temp;
temp=head;
clrscr();
printf("1 for sort on roll no\n");
printf("2 for sort on age\n");
printf("enter your choise:-");
fflush(stdin);
scanf("%d",&choice);

switch(choice)
{
case 1:
flag=1;
break;
case 2:
flag=2;
break;
default:
printf("\n\n\nwrong choice......");
getch();
}
if(flag!=0)
for(node=head;node!=NULL;node=node->next)
for(temp=node;temp!=NULL;temp=temp->next)
{
if(flag==1)
{
if(temp->roll_no<node->roll_no)
{
no=temp->roll_no;
temp->roll_no=node->roll_no;
node->roll_no=no;
strset(name,'\0');
strcpy(name,temp->name);
strset(temp->name,'\0');
strcpy(temp->name,node->name);
strset(node->name,'\0');
strcpy(node->name,name);
no=temp->age;
temp->age=node->age;
node->age=no;

}
}
else if(flag==2)
{
if(temp->age<node->age)
{
no=temp->roll_no;
temp->roll_no=node->roll_no;
node->roll_no=no;
strset(name,'\0');
strcpy(name,temp->name);
strset(temp->name,'\0');
strcpy(temp->name,node->name);
strset(node->name,'\0');
strcpy(node->name,name);
no=temp->age;
temp->age=node->age;
node->age=no;

}
}
}
else
printf("bye bey");
}
int find_length(struct singly *node){
int no=0;
while(node!=NULL)
{
no++;
node=node->next;
}
return no;
}
void main()
{
int choise;
clrscr();
head=NULL;
create(head);
while(1)
{
clrscr();
printf("1 for insert\n");
printf("2 for delete\n");
printf("3 for display\n");
printf("4 for sort a linked list\n");
printf("5 for find length of linked list\n");
printf("6 for exit");
printf("\n\nenter your choise:-");
fflush(stdin);
scanf("%d",&choise);

switch(choise)
{
case 1:
insert(head);
break;
case 2:
delet(head);
break;
case 3:
display(head);
break;
case 4:
sort_node(head);
display(head);
break;
case 5:
printf("\n\n\ttotal no of nodes are:- %d",find_length(head));
break;
case 6:
exit(0);
default:
printf("\n\n\nwrong choise....");
}
getch();
}
}

No comments:

Post a Comment