#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
struct node *next;
};
void insrt(node *s, int data)
{
while(s->next != NULL)
{
s = s->next;
}
s->next = (node*) malloc(sizeof(node));
s->next ->data = data;
s->next ->next = NULL;
}
void display(node *s)
{
while(s->next != NULL)
{
cout<< s->next->data << endl;
s = s->next;
}
}
void srch(node *s, int data)
{
int c = 0;
while(s->next != NULL)
{
if(s->next -> data == data)
{
c++;
}
s = s->next;
}
cout << data << " Found " << c << " Times " << endl;
}
void deletef(node *s, int data)
{
int c = 0;
while(s->next != NULL)
{
if(s->next -> data == data)
{
s->next = s->next->next;
c = 1;
break;
}
s = s->next;
}
if(c == 0)
{
cout << data << " is not found" << endl;
}
else
{
cout << "Delete is Successfully.." << endl;
}
}
int main()
{
node *h = (node*) malloc(sizeof(node));
h->next = NULL;
insrt(h, 1);
insrt(h, 2);
insrt(h, 3);
insrt(h, 4);
insrt(h, 5);
display(h);
srch(h, 5);
deletef(h, 4);
display(h);
return 0;
}
#include<cstdlib>
using namespace std;
struct node
{
int data;
struct node *next;
};
void insrt(node *s, int data)
{
while(s->next != NULL)
{
s = s->next;
}
s->next = (node*) malloc(sizeof(node));
s->next ->data = data;
s->next ->next = NULL;
}
void display(node *s)
{
while(s->next != NULL)
{
cout<< s->next->data << endl;
s = s->next;
}
}
void srch(node *s, int data)
{
int c = 0;
while(s->next != NULL)
{
if(s->next -> data == data)
{
c++;
}
s = s->next;
}
cout << data << " Found " << c << " Times " << endl;
}
void deletef(node *s, int data)
{
int c = 0;
while(s->next != NULL)
{
if(s->next -> data == data)
{
s->next = s->next->next;
c = 1;
break;
}
s = s->next;
}
if(c == 0)
{
cout << data << " is not found" << endl;
}
else
{
cout << "Delete is Successfully.." << endl;
}
}
int main()
{
node *h = (node*) malloc(sizeof(node));
h->next = NULL;
insrt(h, 1);
insrt(h, 2);
insrt(h, 3);
insrt(h, 4);
insrt(h, 5);
display(h);
srch(h, 5);
deletef(h, 4);
display(h);
return 0;
}
Comments
Post a Comment