C Program to implement Singular Linked list

Simple C Program to implement Singular Linked list with following operations:

  • Insertion at end
  • Insertion at front
  • Insert after a value
  • Insert before a value
  • Delete at end
  • Delete at front
  • Delete after a value
  • Delete before a value
  • Delete element itself
  • Delete the whole list
  • Display

Code:

#include <stdio.h>

#include <stdlib.h>

#include<conio.h>



struct node {

int data;

struct node *next;

};



struct node *head;





void insertend(){

int x;

struct node *head2=head;

struct node *temp;

temp=(struct node*)malloc(sizeof(struct node));

printf("Enter the element to be inserted: ");

scanf("%d",&x);

temp->data=x;

temp->next=NULL;

if(head==NULL){

head=temp;

}

else{

while(head2->next!=NULL){

head2=head2->next;

}

head2->next=temp;



}

}



void insertf(){

int x;

struct node *head2=head;

struct node *temp;

temp=(struct node*)malloc(sizeof(struct node));

printf("Enter the element to be inserted: ");

scanf("%d",&x);

temp->data=x;

temp->next=NULL;

if(head==NULL){

head=temp;

}

else{

temp->next=head2;

head=temp;



}

}







void insertafter(){

int a,x;

printf("Enter the last data after which the data is to be entered: ");

scanf("%d",&a);

struct node *head2=head;

struct node *temp;

temp=(struct node*)malloc(sizeof(struct node));

printf("Enter the element to be inserted: ");

scanf("%d",&x);

temp->data=x;

temp->next=NULL;

if(head==NULL){

head=temp;



}

else if (head->next==NULL){

head->next=temp;



}

else{

while(head2->data!=a && head2->next!=NULL) {



head2=head2->next;

}

if(head2->data==a){



temp->next=head2->next;

head2->next=temp;

}

else{printf("Element not found!");}

}

}



void insertbefore(){

int a,x;

printf("Enter the last data before which the data is to be entered: ");

scanf("%d",&a);

struct node *head2=head;

struct node *temp;

temp=(struct node*)malloc(sizeof(struct node));

printf("Enter the element to be inserted: ");

scanf("%d",&x);

temp->data=x;

temp->next=NULL;

if(head==NULL){

head=temp;



}

else if (head->next==NULL){

head->next=temp;



}

else{

while(head2->next->data!=a && head2->next->next!=NULL) {



head2=head2->next;

}

if(head2->next->data==a){



temp->next=head2->next;

head2->next=temp;

}

else{

printf("element not found!");

}



}

}



void deleteend(){

if(head==NULL){

printf("Deletion is not possible!");



}

else if(head->next==NULL){

printf("Deletion is not possible!");

}

else{

struct node *head2;

struct node *temp;

head2=head;

while(head2->next->next!=NULL){

head2=head2->next;







}

temp=head2->next;

head2->next=NULL;

free(temp);

}





}



void deletef(){

if(head==NULL){

printf("Deletion is not possible!");



}

else{

head=head->next;





}





}





void deleteafter(){

int a;

if(head==NULL){

printf("Deletion is not possible!");



}

else if(head->next==NULL){

printf("Deletion is not possible!");

}

else{



printf("Enter the number after which the element to be deleted: ");

scanf("%d",&a);

struct node *head2;

struct node *temp;

head2=head;

while(head2->data!=a && head2->next!=NULL){

head2=head2->next;

}

if(head2->data==a){



temp=head2->next;

head2->next=temp->next;

free(temp);

}

else {printf("Element not found!");}

}

}



void deletebefore(){

int a;

if(head==NULL){

printf("Deletion is not possible!");



}

else if(head->next==NULL){

printf("Deletion is not possible!");

}

else{



printf("Enter the number before which the element to be deleted: ");

scanf("%d",&a);

struct node *head2;

struct node *temp;

head2=head;

while(head2->next->next->data!=a && head2->next->next->next!=NULL){

head2=head2->next;

}



if(head2->next->next->data==a){

temp=head2->next;

head2->next=temp->next;

free(temp);



}

else{

printf("element not found!");

}

}

}

void deleteit(){

int a;

if(head==NULL){

printf("Deletion is not possible!");



}

else if(head->next==NULL){

printf("Deletion is not possible!");

}

else{



printf("Enter the number which is to be deleted: ");

scanf("%d",&a);

struct node *head2;

struct node *temp;

head2=head;

while(head2->next->data!=a && head2->next->next!=NULL){

head2=head2->next;

}

if(head2->next->data==a){

temp=head2->next;

head2->next=temp->next;

free(temp);}

else{printf("Element not found!");}

}

}



void deletelist(){

struct node *head2;

head2=head;

struct node *temp;

if(head2==NULL){printf("Deletion not possible!");}

else{

while(head2!=NULL){

temp=head2;

head2=head2->next;

free(temp);

}

}

head=NULL;

}



void display(){

struct node *head2=head;



if(head==NULL){

printf("The list is empty!");



}

else{

while(head2!=NULL)

{



printf("\t%d",head2->data);

head2=head2->next;

}



}



}





void main(){

head=NULL;

int opt;

do{

printf("\noperations:");

printf("\n1. insert in the end ");

printf("\t2. insert in the front ");

printf("\t3. insert after an element ");

printf("\t4. insert before an element ");

printf("\t5. delete from end");

printf("\t6. delete from front");

printf("\t7. delete after an element");

printf("\t8. delete before an element");

printf("\t9. delete element itself:");

printf("\t10. Delete the list");

printf("\t11. Display");

printf("\t12. Exit");

printf("\nEnter your choice: ");

scanf("%d",&opt);

switch(opt){

case 1:

insertend();

printf("\n");

display();

break;

case 2:

insertf();

printf("\n");

display();

break;

case 3:

insertafter();

printf("\n");

display();

break;

case 4:

insertbefore();

printf("\n");

display();

break;

case 5:

deleteend();

printf("\n");

display();

break;

case 6:

deletef();

printf("\n");

display();

break;

case 7:

deleteafter();

printf("\n");

display();

break;

case 8:

deletebefore();

printf("\n");

display();

break;

case 9:

deleteit();

printf("\n");

display();

break;

case 10:

deletelist();

display();

break;

case 11:

display();

break;



default:

break;



}

}while(opt!=12);

getch();

}

Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

Servlet Program to Print Today’s Date and Time using refresh header

Java Program to Implement sorting algorithm using TCP on Server application