Вы находитесь на странице: 1из 67

1. Wrte a program to fnd the factora of any no. usng recurson.

#ncude<ostream.h>
nt factora(nt);
vod man(vod)
{
nt number;
cout << "Pease enter a postve nteger: ";
cn >> number;
f (number < 0)
cout << "That s not a postve nteger.\n";
ese
cout << number << " factora s: " << factora(number) <<
end;
}

nt factora(nt number)
{
nt temp;
f(number <= 1) return 1;
temp = number * factora(number - 1);
return temp;
}
Output:-
2. Wrte a program to generate Fbonacc seres that the
user enters the mt of seres
#ncude<ostream.h>
nt man()
{
nt ,n,frst=0,second=1,fbo;
cout<<"Enter the number of terms: ";
cn>>n;
cout<<"\nFbonacc seres:\n";
cout<<frst<<"\t"<<second;
for(=0;<n-2;++)
{
fbo=frst+second;
frst=second;
second=fbo;
cout<<"\t"<<fbo;
}
return 0;
}
Resut:
3. Write a program to print the reverse of the integer no. entered by user
#ncude<ostream.h>
man()
{
nt number;
nt remander;
cout<<"\n Enter the number = ";
cn>>number;
cout<<"\n The number wth reverse dgts s = ";
do
{
remander=number%10;
cout<<remander;
number=number/10;
}
whe(number>0);
return 0;
}
Resut:
5. Wrte a program to check the entered no. s a PRIME no.
or not?
#ncude<ostream.h>
vod man()
{
nt n,m;
cout<<"Pease enter a number: ";
cn>>n;
for (nt =2;<=(n-1);++)
{
f(n%==0)
{
cout<<"Not a prme";
break;
}
ese
{
cout<<"Prme";
break;
}
}
}

Resut:
6. write the program to generate the table of no. entered by the user
#ncude <ostream.h>
nt man ()
{
nt num;
cout<<"enter the no";
cn >> num;
nt |=1;
cout<<"tabe";
for (;|<=10;|++)
{nt z;
z=num*|;
cout<<"\n"<< " " << z;
cout << end;
}
return 0;
}
Resut:
7. wrte a program to demonstrate how to pass array to
the functon?
#ncude <ostream.h>

// functon decaraton:
doube getAverage(nt arr||, nt sze);
nt man ()
{
// an nt array wth 5 eements.
nt baance|5| = {1000, 2, 3, 17, 50};
doube avg;
// pass ponter to the array as an argument.
avg = getAverage( baance, 5 ) ;

// output the returned vaue
cout << "Average vaue s: " << avg << end;

return 0;
}
doube getAverage(nt arr||, nt sze)
{
nt , sum = 0;
doube avg;
for ( = 0; < sze; ++)
{
sum += arr||;
}
avg = doube(sum) / sze;
return avg;
}
Resut:
8. Wrte a program to nsert an eement n an array
#ncude<ostream.h>
vod man()
{
nt a|100|,,pos,num,tem,sze;
cout<<"How many eement: ";
cn>>sze;
cout<<"Enter the eements of array: "<<end;
for(=0;<sze;++)
cn>>a||;
cout<<end;
cout<<"Now Array s: "<<end;
for(=0;<sze;++)
cout<<"Eement at poston "<<+1<<" s
"<<a||<<end;
cout<<end<<"Press any key to add another
eement"<<end<<end;
cout<<"Enter another eement: ";
cn>>num;
cout<<"Enter poston number: ";
cn>>pos;
cout<<end;
--pos;
for(=sze;>=pos;--)
a|+1|=a||;
a|pos|=num;
sze++;
cout<<"New Array: "<<end;
for(=0;<sze;++)
cout<<"Eement at poston "<<+1<<" s
"<<a||<<end;
}
Resut:
9. Wrte a program to deete an eement n an array.
#ncude<ostream.h>
vod man()
{
nt a|100|,,pos,num,tem,sze;
cout<<"How many eement: ";
cn>>sze;
cout<<"Enter the eements of array: "<<end;
for(=0;<sze;++)
cn>>a||;
cout<<end;
cout<<"Now Array s: "<<end;
for(=0;<sze;++)
cout<<"Eement at poston "<<+1<<" s "<<a||<<end;
cout<<"Enter poston number to deete: ";
cn>>pos;
--pos;
tem=a|pos|;
for(=pos;<sze;++)
a||=a|+1|;
sze--;
cout<<"Deeted eement: "<<tem<<end;
cout<<end<<"New array: "<<end;
for(=0;<sze;++)
cout<<"Eement at poston "<<+1<<" s "<<a||<<end;
}
Resut:
10. Write the program to multiplication of to matrices.
#ncude<ostream.h>
vod man()
{
nt a|10||10|,b|10||10|,c|10||10|,m,n,o,p,,|;
cout<<"Enter number of rows of A: ";
cn>>m;
cout<<"Enter number of cooumns of A: ";
cn>>n;
cout<<end<<"Enter eements of matrx A: "<<end;
for(=0;<m;++)
{
for(|=0;|<n;|++)
{
cout<<"Enter eement a"<<+1<<|+1<<": ";
cn>>a|||||;
}
}
cout<<end<<"Enter number of rows of B: ";
cn>>o;
cout<<"Enter number of cooumns of B: ";
cn>>p;
cout<<end<<"Enter eements of matrx B: "<<end;
for(=0;<o;++)
{
for(|=0;|<p;|++)
{
cout<<"Enter eement b"<<+1<<|+1<<": ";
cn>>b|||||;
}
}
cout<<end<<"Dspayng Matrx A: "<<end<<end;
for(=0;<m;++)
{
for(|=0;|<n;|++)
{
cout<<a|||||<<" ";
}
cout<<end<<end;
}
cout<<end<<"Dspayng Matrx B: "<<end<<end;
for(=0;<o;++)
{
for(|=0;|<p;|++)
{
cout<<b|||||<<" ";
}
cout<<end<<end;
}
f(n==o)
{
for(=0;<m;++)
{
for(|=0;|<p;|++)
{
c|||||=0;
for(nt k=0;k<n;k++)
{
c|||||=c|||||+a|||k|*b|k||||;
}
}
}
cout<<end<<"Matrx A * Matrx B = Matrx C: "<<end<<end;
for(=0;<m;++)
{
for(|=0;|<p;|++)
{
cout<<c|||||<<" ";
}
cout<<end<<end;
}
}
ese
cout<<"Mutpcaton not possbe :(";
}
Resut:
11. Write a program to find transpose of a matrix.
#ncude<ostream.h>
vod man()
{
nt a|10||10|,m,n,,|;
cout<<"Enter number of rows: ";
cn>>m;
cout<<"Enter number of cooumns: ";
cn>>n;
f(m!=n)
{
cout<<"Matrx not square so Transpose not possbe :(";
}
ese
{
cout<<end<<"Enter eements of matrx: "<<end;
for(=0;<m;++)
{
for(|=0;|<n;|++)
{
cout<<"Enter eement a"<<+1<<|+1<<": ";
cn>>a|||||;
}
}
cout<<end<<"Dspayng Matrx: "<<end<<end;
for(=0;<m;++)
{
for(|=0;|<n;|++)
{
cout<<a|||||<<" ";
}
cout<<end<<end;
}
cout<<end<<"Dspayng Matrx Transpose: "<<end<<end;
for(=0;<m;++)
{
for(|=0;|<n;|++)
{
cout<<a|||||<<" ";
}
cout<<end<<end;
}
}
}
Resut:
12. Wrte a program to demonstrate the operaton
perform on stack.
#ncude<ostream.h>
nt max=7;
nt t=0;

cass stack
{
nt s|7|;
pubc:
vod push(nt);
vod pop();
vod top();
vod empty();
vod show();
};

vod stack::push(nt y) //Push Operaton
{
f(t<max)
{
t=t+1;
s|t|=y;
}
ese
cout<<end<<"stack overfows..."<<end;
}

vod stack::pop() //Pop Operaton
{
nt tem;
f(t>=0)
{
t=t-1;
tem=s|t+1|;
cout<<end<<"popped tem >>"<<tem<<end;
}
ese
cout<<end<<"stack underfows"<<end;
}

vod stack::top() //To fnd the top of the stack
{
f(t>=0)
cout<<end<<"topmost eement >> "<<s|t|<<end;
ese
cout<<end<<"stack underfows..."<<end;
}

vod stack::empty() //To check f the stack s empty
{
f(t<0)
cout<<end<<"stack s empty..."<<end;
ese
cout<<end<<"stack s not empty..."<<end;
}

vod man()
{
nt a,x;
stack s1;
do
{
cout<<"enter an opton..."<<end<<"1-push"<<end<<"2-
pop"<<end<<"3-top"<<end<<"4-empty"<<end;
cout<<"5-end"<<end;
cn>>a;
cout<<end;
swtch(a)
{
case 1:
{
cout<<end<<"enter a vaue >> "<<end;
cn>>x;
s1.push(x);
}
break;
case 2:
s1.pop();
break;
case 3:
s1.top();
break;
case 4:
s1.empty();
break;
}
}
whe(a!=5);
}
Resut:
13. Write a program to insert an item into a Simple queue using
array.

#ncude<ostream.h>
vod man()
{
nt queue|100|,oc,front=1,n=100,rear,tem;
cout<<"\n------ Oueue Inserton usng Array ------";
cout<<"\n\nEnter Vaue of Rear : ";
cn>>rear;
f(front==1 && rear==n)
{
cout<<"\nOueue s Fu";
return 0;
}
cout<<"\nEnter Eements n Oueue :\n";
for(oc=front;oc<=rear;oc++)
{
cn>>queue|oc|;
}
ns:
cout<<"\nEnter Item you want to Insert : ";
cn>>tem;
rear=rear+1; // Increment the Rear
queue|rear|=tem; // Insert Eement at Rear
cout<<"\nOueue After Inserton :\n";
for(oc=front;oc<=rear;oc++)
{
cout<<queue|oc|<<end;
}
cout<<"\nRear : "<<rear;
cout<<"\n\nItem "<<tem<<" s Inserted at Rear\n\n";
cout<<"\n\n\nWant to Insert more eements ?????\n\n";
cout<<" -----> Press 1 to Contnue\n";
cout<<" -----> Press any Key to Ext\n";
char choce;
cn>>choce;
f(choce=='1' && front==1 && rear<n)
{
cout<<end<<end;
goto ns;
}
ese f(choce=='1' && front==1 && rear==n)
{
cout<<"\nOueue s Fu";
return 0;
}
ese
{
return 0;
}
}
Resut:
1. Write a program to delete an item from a Simple queue using
array.
#ncude<ostream.h>
cass queue
{
nt a|10|,front,rear,n;
pubc:
vod head();
vod add();
vod de();
};
vod queue::head()
{
rear=0;
front=0;
cout<<"mpementaton of queue usng array"<<end;
cout<<"**********************************"<<end;
cout<<"1.add"<<end;
cout<<"2.de"<<end;
cout<<"3.ext"<<end;
cout<<"enter the vaue of n:";
cn>>n;
}
vod queue::add()
{
f(rear>=n)
{
cout<<"queue s fu"<<end;
}
ese
{
cout<<"enter the num:";
cn>>a|rear|;
rear++;
}
}
vod queue::de()
{
f(rear==front)
{
cout<<"queue s empty"<<end;
}
ese
{
cout<<"deeted eements s:"<<a|front|<<end;
front++;
}
}
vod man()
{
nt a;
queue q;
q.head();
do
{
cout<<"enter your choce:";
cn>>a;
swtch(a)
{
case 1:
q.add();
break;
case 2:
q.de();
break;
case 3:
cout<<"ext"<<end;
break;
}
}
whe(a!=3);
}
Resut:
1!. Write a program to insert an item into a Simple queue using lin"
list.
#ncude <ostream>
cass Node {
nt data;
Node* next;
pubc:
Node() {};
vod SetData(nt aData) { data = aData; };
vod SetNext(Node* aNext) { next = aNext; };
nt Data() { return data; };
Node* Next() { return next; };
};
cass Lst {
Node *head;
pubc:
Lst() { head = NULL; };
vod Dspay();
vod Append(nt data);

};
vod Lst::Dspay() {
Node *tmp = head;
f ( tmp == NULL ) {
cout << "EMPTY" << end;
return;
}
f ( tmp->Next() == NULL ) {
cout << tmp->Data();
cout << " --> ";
cout << "NULL" << end;
}
ese {
do {
cout << tmp->Data();
cout << " --> ";
tmp = tmp->Next();
} whe ( tmp != NULL );
cout << "NULL" << end;
}
}
vod Lst::Append(nt data) {
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
Node *tmp = head;
f ( tmp != NULL ) {
whe ( tmp->Next() != NULL ) {
tmp = tmp->Next();
}
tmp->SetNext(newNode);
}
ese {
head = newNode;
}
}
nt man() {
Lst st;
nt n|100|, , sze;
char choce;
for(=1; <=100; ++){
cout<<"Insert|I|\nDspay|D|\n";
cn>>choce;
f (choce=='I'||choce==''){
cout<<"Enter the number of nodes: ";
cn>>sze;
cout<<"Enter a number to be nserted:\n";
for(=1; <=sze; ++){
cn>>n||;
st.Append(n||);
}
}

f (choce=='D'||choce=='d'){
cout<<"Eements n the st are: ";
st.Dspay();
}
}
}
Resut:
16. Write a program to delete an item from a Simple queue using
list.
#ncude <ostream.h>
cass Node {
nt data;
Node* next;
pubc:
Node() {};
vod SetData(nt aData) { data = aData; };
vod SetNext(Node* aNext) { next = aNext; };
nt Data() { return data; };
Node* Next() { return next; };
};
cass Lst {
Node *head;
pubc:
Lst() { head = NULL; };
vod Dspay();
vod Append(nt data);
vod Remove(nt data);
};
vod Lst::Dspay() {
Node *tmp = head;
f ( tmp == NULL ) {
cout << "EMPTY" << end;
return;
}
f ( tmp->Next() == NULL ) {
cout << tmp->Data();
cout << " --> ";
cout << "NULL" << end;
}
ese {
do {
cout << tmp->Data();
cout << " --> ";
tmp = tmp->Next();
} whe ( tmp != NULL );
cout << "NULL" << end;
}
}
vod Lst::Append(nt data) {
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
Node *tmp = head;
f ( tmp != NULL ) {
whe ( tmp->Next() != NULL ) {
tmp = tmp->Next();
}
tmp->SetNext(newNode);
}
ese {
head = newNode;
}
}
vod Lst::Remove(nt data) {
Node *tmp = head;
f ( tmp == NULL )
return;
f ( tmp->Next() == NULL ) {
deete tmp;
head = NULL;
}
ese {
Node *prev;
do {
f ( tmp->Data() == data ) break;
prev = tmp;
tmp = tmp->Next();
} whe ( tmp != NULL );
prev->SetNext(tmp->Next());
deete tmp;
}
}
nt man() {
Lst st;
nt n|100|, , sze;
char choce;
for(=1; <=100; ++){
cout<<"Insert|I|\nDspay|D|\nRemove|R|\n";
cn>>choce;
f (choce=='I'||choce==''){
cout<<"Enter the number of nodes: ";
cn>>sze;
cout<<"Enter a number to be nserted:\n";
for(=1; <=sze; ++){
cn>>n||;
st.Append(n||);
}
}
f (choce=='D'||choce=='d'){
cout<<"Eements n the st are: ";
st.Dspay();
}
f (choce=='R'||choce=='r'){
cout<<"Enter a number to be removed: ";
cn>>n||;
st.Remove(n||);
}
}
}
Resut:
1#. Write a program to implement circular queue by using an array.
#ncude <ostream>
#ncude<cstdb>
#defne MAX 10
usng namespace std;
vod add(nt);
vod de();
vod dspay();
nt cq|MAX|;
nt front,rear;
nt man()
{
nt ch=1,,num;
front=-1;
rear=-1;
whe(1)
{
cout<<"\n\n\tMAIN MENU";
cout<<"\n##############################";
cout<<"\n1. INSERTION\n2. DELETION\n3. Dspay\n4. EXIT";
cout<<"\n\nEnter Your Choce: ";
cn>>ch;
swtch(ch)
{
case 1:
cout<<"\n\nEnter The Eement: ";
cn>>num;
add(num);
break;
case 2:
de();
break;
case 3:
dspay();
break;
case 4:
ext(0);
break;
defaut:
cout<<"\n\nWrong Choce!!! Try Agan.";
}
}
return 0;
}
vod add(nt tem)
{
f(front==(rear+1)%MAX)
{
cout<<"\n\nCrcuar Oueue s Fu!!!";
}
ese
{
f(front==-1)
{
front=rear=0;
}
ese
{
rear=(rear+1)%MAX;
}
cq|rear|=tem;
dspay();
}
}
vod de()
{
nt a;
f(front==-1)
{
cout<<"\n\nCrcuar Oueue s Empty!!!";
}
ese
{
a=cq|front|;
f(front==rear)
{
front=rear=-1;
}
ese
{
front=(front+1)%MAX;
}
cout<<"\n\nEement "<<a<<" Deeted from Oueue";
dspay();
}
}
vod dspay()
{
nt ;
f(front==-1)
{
cout<<"\n\nCrcuar Oueue s Empty!!!";
}
ese
{
cout<<"\nFront-->"<<front<<"\nRear--> "<<rear;
cout<<"\n\nThe Crcuar Oueue eements are:\n";
for(=0;<=rear-front;++)
{
cout<<cq|front+|<<"\n";
}
}
}
Resut:

18. Wrte a program to nserton at the begnnng of a
snge nked st
#ncude<ostream>
#ncude<cstdo>
#ncude<cstdb>
usng namespace std;
/*
* Node Decaraton
*/
struct node
{
nt nfo;
struct node *next;
}*start;

/*
* Cass Decaraton
*/
cass snge_st
{
pubc:
node* create_node(nt);
vod nsert_begn();
vod dspay();
snge_st()
{
start = NULL;
}
};

/*
* Man :contans menu
*/
man()
{
nt choce, nodes, eement, poston, ;
snge_st s;
start = NULL;
whe (1)
{
cout<<end<<"---------------------------------"<<end;
cout<<end<<"Operatons on sngy nked st"<<end;
cout<<end<<"---------------------------------"<<end;
cout<<"1.Insert Node at begnnng"<<end;
cout<<"2.Dspay Lnked Lst"<<end;
cout<<"3.Ext "<<end;
cout<<"Enter your choce : ";
cn>>choce;
swtch(choce)
{
case 1:
cout<<"Insertng Node at Begnnng: "<<end;
s.nsert_begn();
cout<<end;
break;

case 2:
cout<<"Dspay eements of nk st"<<end;
s.dspay();
cout<<end;
break;

case 3:
cout<<"Extng..."<<end;
ext(1);
break;
defaut:
cout<<"Wrong choce"<<end;
}
}
}

/*
* Creatng Node
*/
node *snge_st::create_node(nt vaue)
{
struct node *temp, *s;
temp = new(struct node);
f (temp == NULL)
{
cout<<"Memory not aocated "<<end;
return 0;
}
ese
{
temp->nfo = vaue;
temp->next = NULL;
return temp;
}
}

/*
* Insertng eement n begnnng
*/
vod snge_st::nsert_begn()
{
nt vaue;
cout<<"Enter the vaue to be nserted: ";
cn>>vaue;
struct node *temp, *p;
temp = create_node(vaue);
f (start == NULL)
{
start = temp;
start->next = NULL;
}
ese
{
p = start;
start = temp;
start->next = p;
}
cout<<"Eement Inserted at begnnng"<<end;
}


/*
* Dspay Eements of a nk st
*/
vod snge_st::dspay()
{
struct node *temp;
f (start == NULL)
{
cout<<"The Lst s Empty"<<end;
return;
}
temp = start;
cout<<"Eements of st are: "<<end;
whe (temp != NULL)
{
cout<<temp->nfo<<"->";
temp = temp->next;
}
cout<<"NULL"<<end;
}
Resut:
1$. Write a program insertion at the end of a singly lin"ed list.
#ncude<ostream>
#ncude<cstdo>
#ncude<cstdb>
usng namespace std;
/*
* Node Decaraton
*/
struct node
{
nt nfo;
struct node *next;
}*start;

/*
* Cass Decaraton
*/
cass snge_st
{
pubc:
node* create_node(nt);
vod nsert();
vod nsert_ast();
vod dspay();
snge_st()
{
start = NULL;
}
};

/*
* Man :contans menu
*/
man()
{
nt choce, nodes, eement, poston, ;
snge_st s;
start = NULL;
whe (1)
{
cout<<end<<"---------------------------------"<<end;
cout<<end<<"Operatons on sngy nked st"<<end;
cout<<end<<"---------------------------------"<<end;
cout<<"1.Insert Node "<<end;
cout<<"2.Insert node at ast"<<end;
cout<<"3.Dspay Lnked Lst"<<end;
cout<<"4.Ext "<<end;
cout<<"Enter your choce : ";
cn>>choce;
swtch(choce)
{
case 1:
cout<<"Insertng Node: "<<end;
s.nsert();
cout<<end;
break;
case 2:
cout<<"Insertng Node at Last: "<<end;
s.nsert_ast();
cout<<end;
break;

case 3:
cout<<"Dspay eements of nk st"<<end;
s.dspay();
cout<<end;
break;

case 4:
cout<<"Extng..."<<end;
ext(1);
break;
defaut:
cout<<"Wrong choce"<<end;
}
}
}

/*
* Creatng Node
*/
node *snge_st::create_node(nt vaue)
{
struct node *temp, *s;
temp = new(struct node);
f (temp == NULL)
{
cout<<"Memory not aocated "<<end;
return 0;
}
ese
{
temp->nfo = vaue;
temp->next = NULL;
return temp;
}
}

/*
* Insertng eement n begnnng
*/
vod snge_st::nsert()
{
nt vaue;
cout<<"Enter the vaue to be nserted: ";
cn>>vaue;
struct node *temp, *p;
temp = create_node(vaue);
f (start == NULL)
{
start = temp;
start->next = NULL;
}
ese
{
p = start;
start = temp;
start->next = p;
}
cout<<"Eement Inserted at begnnng"<<end;
}

/*
* Insertng Node at ast
*/
vod snge_st::nsert_ast()
{
nt vaue;
cout<<"Enter the vaue to be nserted: ";
cn>>vaue;
struct node *temp, *s;
temp = create_node(vaue);
s = start;
whe (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Eement Inserted at ast"<<end;
}

/*
* Dspay Eements of a nk st
*/
vod snge_st::dspay()
{
struct node *temp;
f (start == NULL)
{
cout<<"The Lst s Empty"<<end;
return;
}
temp = start;
cout<<"Eements of st are: "<<end;
whe (temp != NULL)
{
cout<<temp->nfo<<"->";
temp = temp->next;
}
cout<<"NULL"<<end;
}
Resut:

%0. Write a program insertion at the specified position in a singly
lin"ed list.

#ncude<ostream>
#ncude<cstdo>
#ncude<cstdb>
usng namespace std;
/*
* Node Decaraton
*/
struct node
{
nt nfo;
struct node *next;
}*start;

/*
* Cass Decaraton
*/
cass snge_st
{
pubc:
node* create_node(nt);
vod nsert();
vod nsert_pos();
vod dspay();
snge_st()
{
start = NULL;
}
};

/*
* Man :contans menu
*/
man()
{
nt choce, nodes, eement, poston, ;
snge_st s;
start = NULL;
whe (1)
{
cout<<end<<"---------------------------------"<<end;
cout<<end<<"Operatons on sngy nked st"<<end;
cout<<end<<"---------------------------------"<<end;
cout<<"1.Insert Node "<<end;
cout<<"2.Insert node at poston"<<end;
cout<<"3.Dspay Lnked Lst"<<end;
cout<<"4.Ext "<<end;
cout<<"Enter your choce : ";
cn>>choce;
swtch(choce)
{
case 1:
cout<<"Insertng "<<end;
s.nsert();
cout<<end;
break;
case 2:
cout<<"Insertng Node at a gven poston:"<<end;
s.nsert_pos();
cout<<end;
break;
case 3:
cout<<"Dspay eements of nk st"<<end;
s.dspay();
cout<<end;
break;
case 4:
cout<<"Extng..."<<end;
ext(1);
break;
defaut:
cout<<"Wrong choce"<<end;
}
}
}

/*
* Creatng Node
*/
node *snge_st::create_node(nt vaue)
{
struct node *temp, *s;
temp = new(struct node);
f (temp == NULL)
{
cout<<"Memory not aocated "<<end;
return 0;
}
ese
{
temp->nfo = vaue;
temp->next = NULL;
return temp;
}
}

/*
* Insertng eement n begnnng
*/
vod snge_st::nsert()
{
nt vaue;
cout<<"Enter the vaue to be nserted: ";
cn>>vaue;
struct node *temp, *p;
temp = create_node(vaue);
f (start == NULL)
{
start = temp;
start->next = NULL;
}
ese
{
p = start;
start = temp;
start->next = p;
}
cout<<"Eement Inserted at begnnng"<<end;
}

/*
* Inserton of node at a gven poston
*/
vod snge_st::nsert_pos()
{
nt vaue, pos, counter = 0;
cout<<"Enter the vaue to be nserted: ";
cn>>vaue;
struct node *temp, *s, *ptr;
temp = create_node(vaue);
cout<<"Enter the poston at whch node to be nserted: ";
cn>>pos;
nt ;
s = start;
whe (s != NULL)
{
s = s->next;
counter++;
}
f (pos == 1)
{
f (start == NULL)
{
start = temp;
start->next = NULL;
}
ese
{
ptr = start;
start = temp;
start->next = ptr;
}
}
ese f (pos > 1 && pos <= counter)
{
s = start;
for ( = 1; < pos; ++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
ese
{
cout<<"Poston out of range"<<end;
}
}

/*
* Dspay Eements of a nk st
*/
vod snge_st::dspay()
{
struct node *temp;
f (start == NULL)
{
cout<<"The Lst s Empty"<<end;
return;
}
temp = start;
cout<<"Eements of st are: "<<end;
whe (temp != NULL)
{
cout<<temp->nfo<<"->";
temp = temp->next;
}
cout<<"NULL"<<end;
}
Resut:
%1. Write the program to traverse the element of singly lin"ed list.
#ncude <ostream.h>
char choce;
nt wsh,no,check;
struct st
{
nt data;
st *next;
}*head=NULL;
typedef struct st Node;
cass Lst
{
pubc:
vod create();
vod traverse();
nt count;
Lst()
{
count=0;
}
};
nt man()
{
Lst ;
.create();
cout << "Traversng the created st" << end;
.traverse();
return 0;
}
vod Lst::create()
{
nt range;
cout<<"enter range";
cn>>range;
for(nt =0;<=range;++)
{
Node *node,*temp;
node=new Node;

cout << "Enter the data tem" << end;
cn>>node->data;
node->next = NULL;
f(head!=NULL)
{
temp->next =node;
temp=node;
}
ese
{
head=node;
temp=node;
}
}
cout << "The nked st created s " << end;
traverse();
}
vod Lst::traverse()
{
Node *temp;
temp=head;
f(head==NULL)
{
cout << "The nked st s empty" << end;
return;
}
ese
{
whe(temp->next!=NULL)
{
cout << temp->data << "-";
temp=temp->next;
count ;
};
cout << temp->data << "-";
cout << end;
count ;

}
count=0;
}
Resut:
22. Wrte a program to nserton an tem at the
begnnng of crcuar nked st.
#ncude<ostream>
#ncude<cstdo>
#ncude<cstdb>
usng namespace std;
/*
* Node Decaraton
*/
struct node
{
nt nfo;
struct node *next;
}*ast;

/*
* Cass Decaraton
*/
cass crcuar_st
{
pubc:
vod create_node(nt vaue);
vod add_begn(nt vaue);
vod dspay_st();
crcuar_st()
{
ast = NULL;
}
};

/*
* Man :contans menu
*/
nt man()
{
nt choce, eement, poston;
crcuar_st c;
whe (1)
{
cout<<end<<"---------------------------"<<end;
cout<<end<<"Crcuar sngy nked st"<<end;
cout<<end<<"---------------------------"<<end;
cout<<"1.Create Node"<<end;
cout<<"2.Add at begnnng"<<end;
cout<<"3.Dspay"<<end;
cout<<"4.Out"<<end;
cout<<"Enter your choce : ";
cn>>choce;
swtch(choce)
{
case 1:
cout<<"Enter the eement: ";
cn>>eement;
c.create_node(eement);
cout<<end;
break;
case 2:
cout<<"Enter the eement: ";
cn>>eement;
c.add_begn(eement);
cout<<end;
break;

case 3:
c.dspay_st();
break;

case 4:
ext(1);
break;
defaut:
cout<<"Wrong choce"<<end;
}
}
return 0;
}

/*
* Create Crcuar Lnk Lst
*/
vod crcuar_st::create_node(nt vaue)
{
struct node *temp;
temp = new(struct node);
temp->nfo = vaue;
f (ast == NULL)
{
ast = temp;
temp->next = ast;
}
ese
{
temp->next = ast->next;
ast->next = temp;
ast = temp;
}
}

/*
* Inserton of eement at begnnng
*/
vod crcuar_st::add_begn(nt vaue)
{
f (ast == NULL)
{
cout<<"Frst Create the st."<<end;
return;
}
struct node *temp;
temp = new(struct node);
temp->nfo = vaue;
temp->next = ast->next;
ast->next = temp;
}


/*
* Dspay Crcuar Lnk Lst
*/
vod crcuar_st::dspay_st()
{
struct node *s;
f (ast == NULL)
{
cout<<"Lst s empty, nothng to dspay"<<end;
return;
}
s = ast->next;
cout<<"Crcuar Lnk Lst: "<<end;
whe (s != ast)
{
cout<<s->nfo<<"->";
s = s->next;
}
cout<<s->nfo<<end;
}
Resut:
%3. Write a program insertion at the end at circular lin"ed list.
#include <iostream.h>
using namespace std;
char s;
int a;
int no;
struct node //stucture of a node
{
int data; //data of node
struct node *next; //pointer to the next element
};
typedef struct node Node;
Node *head;
class llist
{
public //!ist of all the function for the list.
"oid create#$;

"oid tra"erse#$;
"oid insert%nd#$;
int count;
llist#$ //!ist constructor
{
count&';
}
};
int main#$ //main program begins
{
llist l;
int n;
cout << ()f you *ant to create a circular list enter y( <<
endl;
cin >> s;
if#s&&+y+,,s&&+-+$
{
l.create#$;
}
do
{
cout << endl;
cout << (enter . to insert at end of the list( << endl;
cout << (enter / to exit( << endl;
cin >> n;
s*itch#n$
{

case . l.insert%nd#$;
brea0;

}
}*hile#n1&/$;

return ';
}
//)mplementation of all the functions from the class llist
"oid llistcreate#$
{
Node *temp;
do
{
Node *node;
node&ne* Node;
cout << (enter the data of node( << endl;
cin >> node2>data;
node2>next&head;
if#head1&N3!!$
{
temp2>next&node;
temp&node;
node2>next&head;
}
else
{
temp&node;
head&node;
node2>next&head;
}
cout << (do you *ant to enter more ( << endl;
cin >> s;
}*hile#s&&+y+,,s&&+-+$;
tra"erse#$;
}
"oid llisttra"erse#$
{
Node *temp;
temp&head;
if#head&&N3!!$
{
cout << (empty( << endl;
}
else
{
*hile#temp2>next1&head$
{
cout << temp2>data << (2(;
temp&temp2>next;
count ;
}
cout << temp2>data;
count ;
}
}

"oid llistinsert%nd#$
{
Node *node4*temp;
node&ne* Node;
cout << (enter data of the node( << endl;
cin >> node2>data;
if#head&&N3!!$
{
head&node;
node2>next&head;
}
else
{
temp&head;
*hile#temp2>next1&head$
{
temp&temp2>next;
}
temp2>next&node;
node2>next&head;
}
tra"erse#$;
}
5esult
%. Write a program to deletion from beginning of circular lin"ed
list.
#include <iostream.h>
using namespace std;
char s;
int a;
int no;
struct node //stucture of a node
{
int data; //data of node
struct node *next; //pointer to the next element
};
typedef struct node Node;
Node *head;
class llist
{
public //!ist of all the function for the list.
"oid create#$;
"oid insert#$;
"oid del#$;
"oid tra"erse#$;
"oid delete6egin#$;
int count;
llist#$ //!ist constructor
{
count&';
}
};
int main#$ //main program begins
{
llist l;
int n;
cout << ()f you *ant to create a circular list enter y( <<
endl;
cin >> s;
if#s&&+y+,,s&&+-+$
{
l.create#$;
}
do
{
cout << endl;
cout << (enter . to delete .st( << endl;
cout << (enter / to exit( << endl;
cin >> n;
s*itch#n$
{

case . l.delete6egin#$;
brea0;

}
}*hile#n1&/$;
return ';
}
//)mplementation of all the functions from the class llist
"oid llistcreate#$
{
Node *temp;
do
{
Node *node;
node&ne* Node;
cout << (enter the data of node( << endl;
cin >> node2>data;
node2>next&head;
if#head1&N3!!$
{
temp2>next&node;
temp&node;
node2>next&head;
}
else
{
temp&node;
head&node;
node2>next&head;
}
cout << (do you *ant to enter more ( << endl;
cin >> s;
}*hile#s&&+y+,,s&&+-+$;
tra"erse#$;
}
"oid llisttra"erse#$
{
Node *temp;
temp&head;
if#head&&N3!!$
{
cout << (empty( << endl;
}
else
{
*hile#temp2>next1&head$
{
cout << temp2>data << (2(;
temp&temp2>next;
count ;
}
cout << temp2>data;
count ;
}
}

"oid llistdelete6egin#$
{
Node *temp;
if#head&&N3!!$
{
cout << (no element is found( << endl;
return;
}
else
{
temp&head;
*hile#temp2>next1&head$
{
temp&temp2>next;
}
if#head2>next&&head$
{
temp&head;
head&N3!!;
delete temp;
}
else
{
temp2>next&head2>next;
temp&head;
head&head2>next;
delete temp;
}
}
tra"erse#$;
}
5esult

Вам также может понравиться