Skip to content

Instantly share code, notes, and snippets.

@milon
Created January 19, 2018 01:25
Show Gist options
  • Save milon/c8eecef59c14bc0f9da1171cc70cd54a to your computer and use it in GitHub Desktop.
Save milon/c8eecef59c14bc0f9da1171cc70cd54a to your computer and use it in GitHub Desktop.
Data Structure: Linked List
//Linked list operations
//Author: Milon
#include<stdio.h>
#include<string.h>
#include<malloc.h>
//#include<conio.h>
void insert_first();
void insert_last();
void insert_before();
void insert_after();
void search_list();
void delete_node();
void show_list();
struct student{
char name[25];
char roll[8];
char dept[4];
char semester[10];
struct student *next;
} *start=NULL;
void main(){
int d=0;
char choice;
do{
//clrscr();
printf("This program is for Linked List operations.\n\n");
printf("# Insert at (F)irst position.\n");
printf("# Insert at (L)ast position.\n");
printf("# Insert (B)efore node.\n");
printf("# Insert (A)fter node.\n");
printf("# (S)earch in a list.\n");
printf("# (R)emove a node from list.\n");
printf("# (D)isplay all nodes of list.\n");
printf("# (E)xit\n\n\n");
printf("Enter your choice: ");
choice=getchar();
switch(choice){
case 'F':
case 'f':
insert_first();
break;
case 'L':
case 'l':
insert_last();
break;
case 'B':
case 'b':
insert_before();
break;
case 'A':
case 'a':
insert_after();
break;
case 'S':
case 's':
search_list();
break;
case 'R':
case 'r':
delete_node();
break;
case 'D':
case 'd':
show_list();
break;
case 'E':
case 'e':
d=1;
break;
default:
printf("\nInvalid input.");
break;
}
}while(d==0);
}
void insert_first(){
struct student *data;
data=(struct student*)malloc(sizeof(struct student));
printf("\nEnter your name: ");
gets(data->name);
printf("\nEnter your roll: ");
gets(data->roll);
printf("\nEnter your department: ");
gets(data->dept);
printf("\nEnter your semester: ");
gets(data->semester);
data->next=NULL;
if(start==NULL)
start=data;
else{
data->next=start;
start=data;
}
printf("\nItem successfully inserted.");
//getch();
}
void insert_last(){
struct student *data,*ptr;
data=(struct student*)malloc(sizeof(struct student));
printf("\nEnter your name: ");
gets(data->name);
printf("\nEnter your roll: ");
gets(data->roll);
printf("\nEnter your department: ");
gets(data->dept);
printf("\nEnter your semester: ");
gets(data->semester);
data->next=NULL;
if(start==NULL)
start=data;
else{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=data;
}
printf("\nItem successfully inserted.");
//getch();
}
void insert_before(){
char roll[8];
struct student *data,*prev,*ptr;
printf("\nEnter the student roll before which you want to insert: ");
gets(roll);
ptr=start;
while(strcmp(roll,ptr->roll)!=0&&ptr!=NULL)
ptr=ptr->next;
if(ptr==NULL){
printf("\Item not found.");
//getch();
return;
}
data=(struct student*)malloc(sizeof(struct student));
printf("\nEnter your name: ");
gets(data->name);
printf("\nEnter your roll: ");
gets(data->roll);
printf("\nEnter your department: ");
gets(data->dept);
printf("\nEnter your semester: ");
gets(data->semester);
data->next=NULL;
ptr=start;
if(strcmp(roll,ptr->roll)==0){
ptr->next=start;
start=ptr;
}
else{
ptr=ptr->next;
prev=start;
while(strcmp(roll,ptr->roll)!=0){
ptr=ptr->next;
prev=prev->next;
}
}
data->next=ptr;
prev->next=data;
printf("\nItem successfully Inserted.");
//getch();
}
void insert_after(){
char roll[8];
struct student *data,*ptr;
printf("\nEnter the student roll before which you want to insert: ");
gets(roll);
ptr=start;
while(strcmp(roll,ptr->roll)!=0&&ptr!=NULL)
ptr=ptr->next;
if(ptr==NULL){
printf("\Item not found.");
//getch();
return;
}
data=(struct student*)malloc(sizeof(struct student));
printf("\nEnter your name: ");
gets(data->name);
printf("\nEnter your roll: ");
gets(data->roll);
printf("\nEnter your department: ");
gets(data->dept);
printf("\nEnter your semester: ");
gets(data->semester);
data->next=NULL;
ptr=start;
while(strcmp(roll,ptr->roll)!=0)
ptr=ptr->next;
data->next=ptr->next;
ptr->next=data;
printf("\nItem successfully inserted.");
//getch();
}
void search_list(){
char roll[8];
struct student *ptr;
printf("\nEnter the student roll you want to search: ");
gets(roll);
ptr=start;
while(strcmp(roll,ptr->roll)!=0&&ptr->next!=NULL)
ptr=ptr->next;
if(ptr->next==NULL)
printf("\nNo match found.");
else{
printf("\nItem found.\n");
printf("%28s %10s %5s %12s",ptr->name,ptr->roll,ptr->dept,ptr->semester);
}
//getch();
}
void delete_node(){
char roll[8];
struct student *ptr,*prev;
printf("\nEnter the student roll you want to search: ");
gets(roll);
ptr=start;
if(strcmp(ptr->roll,roll)==0)
start=ptr->next;
else{
ptr=ptr->next;
prev=start;
while(strcmp(roll,ptr->roll)!=0&&ptr->next!=NULL){
ptr=ptr->next;
prev=prev->next;
}
if(ptr->next==NULL)
printf("No match found.");
else
prev->next=ptr->next;
}
printf("\nNode is successfully deleted.");
//getch();
}
void show_list(){
int c=0;
struct student *ptr;
if(start==NULL)
printf("List is empty.");
else{
ptr=start;
while(ptr!=NULL){
printf("\n%3d.%25s%10s%5s%12s",++c,ptr->name,ptr->roll,ptr->dept,ptr->semester);
ptr=ptr->next;
}
}
//getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment