Singly Linked List
Singly Linked List
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct studinfo{
int marks;
char name[10];
struct studinfo *next;
}*start;
void createlist();
void traverse();
void search();
void insertf();
void insertl();
void deletef();
void deletel();
void inserta();
void insertb();
void deleteno();
void insertion();
int main()
{
int c;
//clrscr();
start = NULL;
do {
printf("\n Enter choice for LInk List .... ");
printf("\n 1. Create Link list.... ");
printf("\n 2. Insert node as First node.. ");
printf("\n 3. Delete First node ..");
printf("\n 4. Delete Last node ..");
printf("\n 5. Seacrh a node ..");
printf("\n 6. Insert after node no...");
printf("\n 7. insert before node no");
printf("\n 8. Insert node as Last node");
printf("\n 9. Insert node as node number");
printf("\n 10. Delete node(given no)");
printf("\n 11.Traverse List..");
printf("\n 12.Exit Lists..");
scanf("%d",&c);
switch (c)
{
case 1:
createlist();
break;
case 2:
insertf();
break;
case 3:
deletef();
break;
case 4:
deletel();
break;
case 5:
search();
break;
case 6:
inserta();
break;
case 7:
insertb();
break;
case 8:
insertl();
break;
case 9:
insertion();
break;
case 10:
deleteno();
break;
case 11:
traverse();
break;
}
}while (c!=12);
}
void createlist()
{
struct studinfo *newnode, *ptr;
char ch;
do{
newnode=(struct studinfo *) malloc(sizeof(struct studinfo));
printf("Entera new record : marks and name");
scanf("%d %s",&newnode->marks,newnode->name);
newnode->next=NULL;
if (start == NULL)
start =newnode;
else
{
ptr =start;
while (ptr->next != NULL)
ptr= ptr->next;
ptr->next = newnode;
}
printf("do you want to continue (y/n) ");
scanf("%c",&ch);
}
while( ch =='y' || ch =='Y');
}
void search()
{
struct studinfo *ptr;
char str[10];
printf("enter name to be searched ");
scanf("%s",str);
ptr=start;
while(ptr!=NULL)
{
if(strcmp(ptr->name,str)==0)
{
printf("\nfound");
break;
}
else
ptr=ptr->next;
}
if(ptr==NULL)
printf("\nnot found");
getch();
}
void insertf()
{
struct studinfo *newnode;
newnode=(struct studinfo *) malloc(sizeof(struct studinfo));
printf("Enter a new record : marks and name");
scanf("%d%s",&newnode->marks,newnode->name);
newnode->next=NULL;
if(start==NULL)
start = newnode;
else
{
newnode->next = start;
start = newnode;
}
}
void insertl()
{
struct studinfo *newnode, *ptr;
newnode=(struct studinfo *) malloc(sizeof(struct studinfo));
printf("Enter a new record : marks and name");
scanf("%d%s",&newnode->marks,newnode->name);
newnode->next=NULL;
if (start == NULL)
start =newnode;
else
{
ptr =start;
while (ptr->next != NULL)
ptr= ptr->next;
ptr->next = newnode;
}
}
void deletef()
{
struct studinfo *ptr;
ptr=start;
start=start->next;
free(ptr);
}
void deletel()
{
struct studinfo *ptr,*prevptr;
if(start->next== NULL)
{
ptr = start;
start = NULL;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
prevptr=ptr;
ptr=ptr->next;
}
prevptr->next =NULL;
}
free(ptr);
}
/* Function inserta, insert node after a given node in the linked list */
void inserta()
{
int cnt=1, no;
struct studinfo *ptr,*prevptr, *newnode;
printf("\enter number ...");
scanf("%d",&no);
ptr=start;
while (cnt != no)
{
ptr = ptr->next;
cnt++;
}
newnode=(struct studinfo *) malloc(sizeof(struct studinfo));
printf("Enter a new record : marks and name");
scanf("%d%s",&newnode->marks,newnode->name);
newnode->next =NULL;
newnode->next = ptr->next;
ptr->next = newnode;
}
/* Function insertb, insert node before a given node in the linked list */
void insertb()
{
while(cnt!=no)
{
prevptr=ptr;
ptr=ptr->next;
cnt++;
}
if (ptr->next == NULL)
deletel();
prevptr->next=ptr->next;
free(ptr);
}
}
/* function travese the each node of the linked list */
void traverse()
{
struct studinfo *ptr;
ptr= start;
while (ptr !=NULL)
{
printf("\nRecord: marks and name %d %s\n",ptr->marks, ptr->name);
ptr = ptr->next;
}
getch();
}