You are on page 1of 5

//LINKED LIST TOTAL PROGRAM IN C

#include<stdio.h>
#include<conio.h>
struct Node
{
int data;
struct Node* next;
};

typedef struct Node Node;


Node *start,*newptr,*save_st,*save_end,*ptr;
struct Node* Create_New_Node(int);
void Insert_Beg(struct Node*);
void Insert_End(struct Node*);
void Display(struct Node*);
void Search_value(struct Node*,int);
void Delete_Beg(void);

int main()
{
int val,choice;
char ch='y';
start=NULL;
newptr=NULL;
clrscr();
do
{
printf("\n1. Create");
printf("\n2. Insert at Beginning Position");
printf("\n3. Insert at Ending Positin");
printf("\n4. Searching a value...");
printf("\n5. Delete a node from begining Positin");
printf("\n6. Delete a node from Ending Positin");
printf("\n7. Display");
printf("\nEnter Your Choice");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n Enter a value");
scanf("%d",&val);
newptr=Create_New_Node(val);
if(newptr==NULL)
{
printf("\n Memory Is FULL");
exit(0);
}
else
printf("\n Node Create Successfully");
break;
case 2:if(newptr==NULL)
printf("\n Node Not Created");
else
Insert_Beg(newptr);
break;
case 3:if(newptr==NULL)
printf("\n Node Not Created");
else
Insert_End(newptr);
break;
case 4:printf("\n Enter a value for searching..");
scanf("%d",&val);
Search_value(start,val);
break;
case 5:Delete_Beg();
break;
case 7: Display(start);
break;
default:printf("\n Wrong Choice");
}//end of switch statement
printf("\n Do you want to continue(y/Y)?");
fflush(stdin);
scanf("%c",&ch);

}while(ch=='y'|| ch=='Y');//end of while

return 0;
}// end of main

/*************CODE FOR CREATING A NODE*************/

struct Node * Create_New_Node(int n)


{
ptr=(Node *)malloc(sizeof(Node));
ptr->data=n;
ptr->next=NULL;
return ptr;
}
/*************************************************/
/***********CODE FOR INSERT A NODE AT BEGINING POSITIO*****/
void Insert_Beg(Node * np)
{
if(start==NULL)
{
start=np;
save_end=np;
}
else
{
save_st=start;
start=np;
np->next=save_st;
}
printf("\n Your node insert successfully at begining position");
printf("\nAfter Insertion the list is as follows..");
Display(start);

}
/*****************************************************************/
/*******CODE FOR INSERT A NODE AT ENDING POSITION**********/
void Insert_End(Node *np)
{
if(start==NULL)
{
start=np;
save_end=np;
}
else
{
save_end->next=np;
save_end=np;
}
printf("\n Your node insert successfully at ending position");
printf("\nAfter Insertion the list is as follows..");
Display(start);

}
/*****************************************************************/
/*****CODE FOR SEARCHING A VALUE FROM THE LINKED LIST***/
void Search_value(struct Node* st,int v)
{
int temp=0;
while(st!=NULL)
{
if(st->data==v)
{
temp=1;
break;
}
st=st->next;
}
if(temp==1)
printf("\n Your Entered Number %d is Found",v);
else
printf("\n Sorry!!!!Your Entered Number %d is Not Found",v);
}
/*****************************************************************/
/************DELETE THE NODE FROM THE FIRST POSITION*****/
void Delete_Beg()
{
struct Node * np;
int val;
np=start;
val=np->data;
start=start->next;
free(np);
printf("\n Than You...Your First Node %d is deleted",val);
printf("\n After Deletion the Linked List is as Follows...");
Display(start);
}
/****************************************************************/
/***********DISPLAY THE LINKED LIST************/
void Display(Node *np)
{
while(np!=NULL)
{
printf("%d->",np->data);
np=np->next;
}
}
/*********************************************************/

You might also like