#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
node *h = NULL, *l = NULL;
void insrt(int v)
{
node *t = (node*) malloc(sizeof(node));
t->data = v;
t->next = NULL;
if(h == NULL)
{
h = t;
l = t;
}
else
{
l->next = t;
l = t;
}
}
void createlink()
{
int v;
cout << "Input A Number. (Enter -10 to Exit)" << endl;
while(1)
{
cin >> v;
if(v == -10)
break;
insrt(v);
}
}
void insrtF(int v)
{
node *t = (node*) malloc(sizeof(node));
t->data = v;
if(h == NULL)
{
t->next = NULL;
h = t;
l = t;
}
else
{
t->next = h;
h = t;
}
}
void insrtafter(int k, int v)
{
node *s = h;
int f = 0;
while(s != NULL)
{
if(s->data == k)
{
node *n = (node*) malloc(sizeof(node));
n -> data = v;
n -> next = s->next;
s->next = n;
cout << v << " Successfully Inserted After." << endl;
f = 1;
break;
}
else
s = s->next;
}
if(f == 0)
{
cout << "Key Not Found." << endl;
}
}
void searchitem(int v)
{
node *s = h;
int c = 0;
while(s != NULL)
{
if(s->data == v)
{
c++;
}
s = s->next;
}
cout << v << " found " << c << " Times." << endl;
}
void deleteitem(int v)
{
node *n = h;
node *p = NULL;
int f = 0;
while(n != NULL)
{
if(n->data == v)
{
if(p == NULL)
{
h = n->next;
}
else
p->next = n->next;
f = 1;
break;
}
p = n;
n = n->next;
}
if(f == 0)
{
cout << v << " is not found.." << endl;
}
}
void display()
{
node *t = h;
while(t != NULL)
{
cout << t->data << endl;
t = t->next;
}
}
int main()
{
createlink();
display();
deleteitem(5);
deleteitem(6);
display();
searchitem(3);
return 0;
}
using namespace std;
struct node
{
int data;
struct node *next;
};
node *h = NULL, *l = NULL;
void insrt(int v)
{
node *t = (node*) malloc(sizeof(node));
t->data = v;
t->next = NULL;
if(h == NULL)
{
h = t;
l = t;
}
else
{
l->next = t;
l = t;
}
}
void createlink()
{
int v;
cout << "Input A Number. (Enter -10 to Exit)" << endl;
while(1)
{
cin >> v;
if(v == -10)
break;
insrt(v);
}
}
void insrtF(int v)
{
node *t = (node*) malloc(sizeof(node));
t->data = v;
if(h == NULL)
{
t->next = NULL;
h = t;
l = t;
}
else
{
t->next = h;
h = t;
}
}
void insrtafter(int k, int v)
{
node *s = h;
int f = 0;
while(s != NULL)
{
if(s->data == k)
{
node *n = (node*) malloc(sizeof(node));
n -> data = v;
n -> next = s->next;
s->next = n;
cout << v << " Successfully Inserted After." << endl;
f = 1;
break;
}
else
s = s->next;
}
if(f == 0)
{
cout << "Key Not Found." << endl;
}
}
void searchitem(int v)
{
node *s = h;
int c = 0;
while(s != NULL)
{
if(s->data == v)
{
c++;
}
s = s->next;
}
cout << v << " found " << c << " Times." << endl;
}
void deleteitem(int v)
{
node *n = h;
node *p = NULL;
int f = 0;
while(n != NULL)
{
if(n->data == v)
{
if(p == NULL)
{
h = n->next;
}
else
p->next = n->next;
f = 1;
break;
}
p = n;
n = n->next;
}
if(f == 0)
{
cout << v << " is not found.." << endl;
}
}
void display()
{
node *t = h;
while(t != NULL)
{
cout << t->data << endl;
t = t->next;
}
}
int main()
{
createlink();
display();
deleteitem(5);
deleteitem(6);
display();
searchitem(3);
return 0;
}
Comments
Post a Comment