SIMPLE AND BASIC LINKED LIST OPERATIONS
SIMPLE AND BASIC LINKED LIST OPERATIONS
In This Post I'm Going To Demonstrate you Simple Operations On Linked List i.e.
(1) Addition Of Nodes
(2) Deletion Of Nodes
(3) Traversing The List and
(4) Search A Data from List
THE CODE IS AS FOLLOWING :-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct ll
{
struct ll *next;
int digit;
}mynode;
mynode *head,*start,*new1;
void addnode(mynode **hd,int data);
void delnode(mynode **hd,int data);
void traverse(mynode *hd);
void search(mynode *hd,int data);
void main()
{
int choice,data;
head=(mynode*)malloc(sizeof(mynode));
head->next=NULL;
clrscr();
do
{
printf("\nEnter Choice : ");
printf("\n1.ADD NODE");
printf("\n2.DELETE NODE");
printf("\n3.TRAVERSE OR READ LIST");
printf("\n4.SEARCH NODE");
printf("\n5.EXIT");
printf("\nEnter Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter A Number ");
scanf("%d",&data);
addnode(&head,data);
break;
case 2:
printf("\nEnter Data To Delete : ");
scanf("%d",&data);
delnode(&head,data);
break;
case 3:
traverse(head);
break;
case 4:
printf("\nEnter Data To Search : ");
scanf("%d",&data);
search(head,data);
break;
case 5:
printf("\nTHANKS FOR USING ME !! HAVE A NICE DAY , BYE BYE %c!! ",1);
getch();
exit(0);
break;
default:
printf("\nINVALID CHOICE RE-ENTER\n");
break;
}//switch
}while(choice!=5);
}
void addnode(mynode **hd,int data)
{
mynode *new1,*head;
head=*hd;
new1=(mynode*)malloc(sizeof(mynode));
new1->next=NULL;
new1->digit=data;
if(head->next==NULL)
{
head->next=new1;
printf("\nFIRST NODE ADDED");
return;
}
while(head->next!=NULL)
head=head->next;
head->next=new1;
printf("\n%d IS ADDED ! ",data);
}
void delnode(mynode **hd,int data)
{
mynode *temp,*prev;
temp=*hd;
clrscr();
if(temp->next==NULL)
{
printf("\nNOTHING TO DELETE");
return;
}
if(temp->next->next==NULL)
{
if(temp->next->digit!=data)
{
printf("\nDATA NOT FOUND IN SINGLE NODE");
return;
}
temp=temp->next;
printf("\nWARNING : SINGLE DATA DELETED ");
prev=*hd;
prev->next=NULL;
free(temp);
printf("\n%d IS DELETED !!",data);
return;
}
prev=*hd;
while(temp->next!=NULL)
{
temp=temp->next;
if(temp->digit==data)
{
prev->next=temp->next;
free(temp);
printf("\n%d IS DELETED",data);
return;
}//if
else
prev=prev->next;
}//while
printf("\nDATA NOT FOUND");
}
void traverse(mynode *hd)
{
mynode *temp=hd;
clrscr();
printf("\nData Available Are: \n");
if(temp->next==NULL)
{
printf("\nNOTHING");
return;
}
while(hd->next!=NULL)
{
hd=hd->next;
printf("%d,",hd->digit);
}
}
void search(mynode *hd,int data)
{
mynode *temp;
temp=hd;
clrscr();
while(temp->next!=NULL)
{
temp=temp->next;
if(temp->digit==data)
{
printf("\nData Found");
return;
}
}
printf("\nNOT FOUND");
}
#include<conio.h>
#include<stdlib.h>
typedef struct ll
{
struct ll *next;
int digit;
}mynode;
mynode *head,*start,*new1;
void addnode(mynode **hd,int data);
void delnode(mynode **hd,int data);
void traverse(mynode *hd);
void search(mynode *hd,int data);
void main()
{
int choice,data;
head=(mynode*)malloc(sizeof(mynode));
head->next=NULL;
clrscr();
do
{
printf("\nEnter Choice : ");
printf("\n1.ADD NODE");
printf("\n2.DELETE NODE");
printf("\n3.TRAVERSE OR READ LIST");
printf("\n4.SEARCH NODE");
printf("\n5.EXIT");
printf("\nEnter Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter A Number ");
scanf("%d",&data);
addnode(&head,data);
break;
case 2:
printf("\nEnter Data To Delete : ");
scanf("%d",&data);
delnode(&head,data);
break;
case 3:
traverse(head);
break;
case 4:
printf("\nEnter Data To Search : ");
scanf("%d",&data);
search(head,data);
break;
case 5:
printf("\nTHANKS FOR USING ME !! HAVE A NICE DAY , BYE BYE %c!! ",1);
getch();
exit(0);
break;
default:
printf("\nINVALID CHOICE RE-ENTER\n");
break;
}//switch
}while(choice!=5);
}
void addnode(mynode **hd,int data)
{
mynode *new1,*head;
head=*hd;
new1=(mynode*)malloc(sizeof(mynode));
new1->next=NULL;
new1->digit=data;
if(head->next==NULL)
{
head->next=new1;
printf("\nFIRST NODE ADDED");
return;
}
while(head->next!=NULL)
head=head->next;
head->next=new1;
printf("\n%d IS ADDED ! ",data);
}
void delnode(mynode **hd,int data)
{
mynode *temp,*prev;
temp=*hd;
clrscr();
if(temp->next==NULL)
{
printf("\nNOTHING TO DELETE");
return;
}
if(temp->next->next==NULL)
{
if(temp->next->digit!=data)
{
printf("\nDATA NOT FOUND IN SINGLE NODE");
return;
}
temp=temp->next;
printf("\nWARNING : SINGLE DATA DELETED ");
prev=*hd;
prev->next=NULL;
free(temp);
printf("\n%d IS DELETED !!",data);
return;
}
prev=*hd;
while(temp->next!=NULL)
{
temp=temp->next;
if(temp->digit==data)
{
prev->next=temp->next;
free(temp);
printf("\n%d IS DELETED",data);
return;
}//if
else
prev=prev->next;
}//while
printf("\nDATA NOT FOUND");
}
void traverse(mynode *hd)
{
mynode *temp=hd;
clrscr();
printf("\nData Available Are: \n");
if(temp->next==NULL)
{
printf("\nNOTHING");
return;
}
while(hd->next!=NULL)
{
hd=hd->next;
printf("%d,",hd->digit);
}
}
void search(mynode *hd,int data)
{
mynode *temp;
temp=hd;
clrscr();
while(temp->next!=NULL)
{
temp=temp->next;
if(temp->digit==data)
{
printf("\nData Found");
return;
}
}
printf("\nNOT FOUND");
}
!!! HOPE U LIKE IT !!!
!!! THANKS FOR READING !!!
!!!!!HAPPY PROGRAMMING !!!!!
U Can catch me at http://www.facebook.com/salokr
Comments
Post a Comment