Beruflich Dokumente
Kultur Dokumente
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
class arr_list
{
private:
struct node
{
int data;
int next;
}
a[10];
public:
int head;
arr_list();
int create();
void display(int);
void insert();
void del();
void search();
};
arr_list::arr_list()
{
for(int i=0;i<10;i++)
{
a[i].data=-1;
}
}
int arr_list::create()
{
int head,i;
cout<<"\nenter the index for first node";
cin>>i;
head=i;
while(i!=-1)
{
cout<<"\nenter the data and index of the first element";
cin>>a[i].data;
cout<<"";
cin>>a[i].next;
i=a[i].next;
}
return head;
}
void arr_list::display(int i)
{
while(i!=-1)
{
if(a[i].data==-1)
cout<<"";
else
{
cout<<a[i].data<<"->";
}
i=a[i].next;
}
cout<<"NULL";
}
void arr_list::insert()
{
int i,new_data,temp;
cout<<"\nenter the new data which is to be inserted";
cin>>new_data;
cout<<"\nenter the data after whice you want to insert";
cin>>temp;
for(i=0;i<10;i++)
{
if(a[i].data==temp)
{
if(a[i+1].data++-1)
{
a[i+1].next=a[i].next;
a[i].next=i+1;
a[i+1].data=new_data;
}
}
}
}
void arr_list::del()
{
int i,temp,current,new_next;
cout<<"\nenter the node to be delete";
cin>>temp;
for(i=0;i<10;i++)
{
if(a[i].data==temp)
{
if(a[i].next==-1)
{
a[i].data=-1;
}
current=i;
new_next=a[i].next;
}
}
for(i=0;i<10;i++)
{
if(a[i].next==current)
{
a[i].next=new_next;
a[current].data=-1;
}
}
}
void arr_list::search()
{
int i,temp,flag=0;
cout<<"\nenter the node to be searched";
cin>>temp;
for(i=0;i<10;i++)
{
if(a[i].data==temp)
{
flag=1;
break;
}
}
if(flag==1)
cout<<"\nthe"<<temp<<"node is present is the list";
else
cout<<"\nthe node is not present";
}
void main()
{
char ans;
int choice;
arr_list obj;
clrscr();
cout<<"\t\tprogram for implementing list using array";
cout<<"\nmain menu";
cout<<"\n1.creation";
cout<<"\n2.display";
cout<<"\n3.insertion of element in the list";
cout<<"\n4.deletion of element form the list";
cout<<"\n5.searching of element from the list";
cout<<"\n6.exit";
do
{
cout<<"\nenter your choice";
cin>>choice;
switch(choice)
{
case 1:
obj.head=obj.create();
break;
case 2:
obj.display(obj.head);
break;
case 3:
obj.insert();
obj.display(obj.head);
break;
case 4:
obj.del();
obj.display(obj.head);
break;
case 5:
obj.search();
break;
case 6:
exit(0);
}
cout<<"\ndo you wish to go to main menu?";
ans=getch();
}
while(ans=='Y.'||ans=='y');
cout<<"Thank You";
getch();
}
OUTPUT
program for implementing list using array
main menu
1.creation
2.display
3.insertion of element in the list
4.deletion of element form the list
5.searching of element from the list
6.exit
enter your choice1
enter the index for first node3
char ans;
flag=TRUE;
do
{
cout<<"\nenter the data";
cin>>val;
New=new node;
if(New==NULL)
cout<<"unable to allocate memory\n";
New->data=val;
New->next=NULL;
if(flag==TRUE)
{
head=New;
temp=head;
flag=FALSE;
}
else
{
temp->next=New;
temp=New;
}
cout<<"\ndo you want to enter more elements?(Y,n)";
ans=getch();
}while(ans=='y'||ans=='Y');
cout<<"\nthe singly linked list is created\n";
getch();
}
void sll::display()
{
node*temp;
temp=head;
if(temp==NULL)
{
cout<<"\nthe list is empty\n";
getch();
clrscr();
return;
}
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
getch();
}
prev->next=temp->next;
delete temp;
cout<<"\nthe element is deleted\n";
}
getch();
}
void sll::insert_last()
{
node*New,*temp;
cout<<"\nenter the element which you want to insert";
cin>>New->data;
if(head==NULL)
head=New;
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=New;
New->next=NULL;
}
}
void sll::insert_after()
{
int key;
node*temp,*New;
New=new node;
cout<<"\nenter the element whice you want to insert";
cin>>New->data;
if(head==NULL)
{
head=New;
}
else
{
cout<<"\nenter the element after whice you want to insert the node";
cin>>key;
temp=head;
do
{
if(temp->data==key)
{
New->next=temp->next;
temp->next=New;
break;
}
else
temp=temp->next;
}
while(temp!=NULL);
}
}
void sll::insert_head()
{
node*New,*temp;
New=new node;
cout<<"\nenter the element which you want to insert";
cin>>New->data;
if(head==NULL)
head=New;
else
{
temp=head;
New->next=temp;
head=New;
}
}
void main()
{
sll s;
int choice,val,ch1;
clrscr();
cout<<"\nprogram to perform various operations on linked list";
cout<<"\n1.create";
cout<<"\n2.display";
cout<<"\n3.search";
cout<<"\n4.insert an element in a list";
cout<<"\n5.delete an element from list";
cout<<"\n6.quit";
do
{
cout<<"\nenter your choice(1-6)";
cin>>choice;
switch(choice)
{
case 1:
s.create();
break;
case 2:
s.display();
break;
case 3:
OUTPUT
program to perform various operations on linked list
1.create
2.display
3.search
4.insert an element in a list
5.delete an element from list
6.quit
enter your choice(1-6)1
enter the data2
do you want to enter more elements?(Y,n)
enter the data3
do you want to enter more elements?(Y,n)
enter the data4
do you want to enter more elements?(Y,n)
the singly linked list is created
enter your choice(1-6)2
234
enter your choice(1-6)3
enter the element you want to search2
the element is present in the list
enter your choice(1-6)3
enter the element you want to search1
enter your choice(1-6)
4
the list is:
234
menu
1.insert at beginning
2.insert after
3.insert at end
enter your choice1
enter the element which you want to insert1
1234
enter your choice(1-6)4
the list is:
1234
menu
1.insert at beginning
2.insert after
3.insert at end
enter your choice2
enter the element whice you want to insert5
enter the element after whice you want to insert the node3
12354
enter your choice(1-6)4
the list is:
12354
menu
1.insert at beginning
2.insert after
3.insert at end
enter your choice3
enter the element which you want to insert6
123546
enter your choice(1-6)5
enter the data of the node you want to delete:2
the element is deleted
13546
enter your choice(1-6)6
int i;
clrscr();
cout<<"\nthe reversed list is...\n\n";
for(i=n-1;i>=0;i--)
cout<<"\n"<<list[i];
cout<<"\npress any key to continue..\n";
getch();
}
int LIST::search(int n)
{
int i,key;
clrscr();
cout<<"\nentre thenumber you want to search?";
cin>>key;
for(i=0;i<n;i++)
{
if(list[i]==key)
{
cout<<"\nthe given numbre is at position:"<<i<<"\n";
getch();
return i;
}
}
cout<<"\nthe given number is not in the list\n";
getch();
return -1;
}
void LIST::delet(int n)
{
int i;
i=search(n);
list[i]=-1;
cout<<"\nthe elements is now deleted";
cout<<"\n we put -1 to indicate empty location";
getch();
}
void main()
{
LIST obj;
int choice,len,position;
char ans;
do
{
clrscr();
cout<<"\n\tprogram to perform operations on ordered list";
cout<<"\n1.create";
cout<<"\n2.display";
cout<<"\n3.search for a number";
cout<<"\n4.reverse";
cout<<"\n5.delete";
cout<<"\n6.quit";
cout<<"\nenter your choice(1-6)";
cin>>choice;
switch(choice)
{
case 1:
len=obj.create();
break;
case 2:
obj.display(len);
break;
case 3:
position=obj.search(len);
break;
case 4:
obj.reverse(len);
break;
case 5:
obj.delet(len);
break;
case 6:
cout<<"\ndo you want to exit(y/n)?";
ans=getch();
if(ans=='y'||ans=='y')
exit(0);
else
break;
default:
clrscr();
cout<<"\n invalid choice,tryagain";
getch();
}
}
while(choice!=6);
}
OUTPUT
program to perform operations on ordered list
1.create
2.display
3.search for a number
4.reverse
5.delete
6.quit
enter your choice(1-6)1
how many elements you want in the list:4
enter the element number1:1
enter the element number2:2
enter the element number3:3
enter the element number4:4
the List is successfully
program to perform operations on ordered list
1.create
2.display
3.search for a number
4.reverse
5.delete
6.quit
enter your choice(1-6)2
the List is...
1
2
3
4
press any key to continue...
program to perform operations on ordered list
1.create
2.display
3.search for a number
4.reverse
5.delete
6.quit
enter your choice(1-6)3
cout<<stk[i]<<" ";
}
};
int main()
{
int ch;
stack st;
clrscr();
cout<<"\n1.push\n2.pop\n3.display\n4.exit";
while(1)
{
cout<<"\nenter a choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<"entre the element";
cin>>ch;
st.push(ch);
break;
case 2:
st.pop();
break;
case 3:
st.display();
break;
case 4:
exit(0);
}
}
return 0;
}
OUTPUT
1.push
2.pop
3.display
4.exit
enter a choice1
entre the element3
inserted3
enter a choice1
entre the element2
inserted2
enter a choice1
entre the element4
inserted4
enter a choice1
entre the element5
inserted5
enter a choice3
5423
enter a choice2
deleted5
enter a choice3
423
enter a choice 4
entry->next=NULL;
top->next=entry;
top=entry;
}
cout<<"\n\t *** "<<num<<" is pushed onto the Stack."<<endl;
}
void Linked_list_Stack::pop( )
{
if(bottom==NULL)
cout<<"\n\t *** Error : Stack is empty. \n"<<endl;
else
{
for(last_entry=bottom;last_entry->next!=NULL;
last_entry=last_entry->next)
second_last_entry=last_entry;
if(top==bottom)
bottom=NULL;
int poped_element=top->data;
delete top;
top=second_last_entry;
top->next=NULL;
cout<<"\n\t *** "<<poped_element<<" is poped from the Stack."<<endl;
}
}
void Linked_list_Stack::print_list( )
{
print=bottom;
if(print!=NULL)
cout<<"\n\t Values pushed onto Stack are : \n"<<endl;
else
cout<<"\n\t *** Nothing to show. "<<endl;
while(print!=NULL)
{
cout<<"\t "<<print->data<<endl;
print=print->next;
}
}
void Linked_list_Stack::show_working( )
{
int choice;
clrscr( );
cout<<"\n\n********** Implementation of Linked List as a Stack
**********"<<endl;
cout<<"\n1.Push elements to stack"<<endl;
cout<<"2.Pop elements to stack"<<endl;
cout<<"3.Print the elements of stack"<<endl;
cout<<"4.Exit"<<endl;
do
{
cout<<"\nEnter your Choice : ";
cin>>choice;
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: print_list( );
break;
case 4: exit(0);
break;
default:
cout<<"enter the valid choice";
}
}while(1);
}
int main( )
{
Linked_list_Stack obj;
obj.show_working( );
return 0;
}
OUTPUT:
********** Implementation of Linked List as a Stack**********
1.Push elements to stack
2.Pop elements to stack
3.Print the elements of stack
4.Exit
Enter your Choice : 1
Enter value to push onto Stack : 2
*** 2 is pushed onto the Stack.
Enter your Choice : 1
Enter value to push onto Stack : 3
*** 3 is pushed onto the Stack.
Enter your Choice : 1
Enter value to push onto Stack : 4
*** 4 is pushed onto the Stack.
Enter your Choice : 3
Values pushed onto Stack are :
2
3
4
Enter your Choice : 2
*** 4 is poped from the Stack.
Enter your Choice : 3
Values pushed onto Stack are :
2
3
Enter your Choice : 4
return 0;
}
char stk_class::pop()
{
char item;
item=st.s[st.top];
st.top--;
return(item);
}
program.cpp
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include "d:\stack.h"
#define size 10
void main(void)
{
char item;
char ans,bracket[10];
stk_class obj;
int i;
clrscr();
cout<<"\n\t\t program for stack application using separate header file";
cout<<"\n enter the expression and put $at end ";
cin>>bracket;
i=0;
if(bracket[i]==')')
cout<<"\n the expression is invalid";
else
{
do
{
while(bracket[i]=='(')
{
obj.push(bracket[i]);
i++;
}
while(bracket[i]==')')
{
item=obj.pop();
i++;
}
}
while(bracket[i]!='$');
if(!obj.stempty())
cout<<"\n the expression is invalid";
else
cout<<"\n the expression has well formed parenthesis";
}
getch();
}
OUTPUT:
else
return 0;
}
void stk_class::pop()
{
node *temp;
temp=top;
top=top->next;
delete temp;
}
program.cpp
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include"d:\stack1.h"
void main(void)
{
char ans,bracket[10];
char data,item;
stk_class obj;
int choice;
int i;
clrscr();
cout<<"\n\t\t enter the expression and put $ at the end ";
cin>>bracket;
i=0;
if(bracket[i]==')')
cout<<"\n the expression is invalid";
else
{
do
{
if(bracket[i]=='(')
{
obj.push(bracket[i]);
}
else if(bracket[i]==')')
{
if(obj.sempty())
{
cout<<"\n the expression is invalid";
getch();
exit(0);
}
obj.pop();
}
i++;
}
while(bracket[i]!='$');
if(obj.sempty())
cout<<"\n the expression has well formed parenthesis";
else
cout<<"\n the expression is invalid";
}
getch();
}
OUTPUT:
enter the expression and put $ at the end ((()))()$
the expression has well formed parenthesis
while(temp->next!=NULL)
temp=temp->next;
temp1=new node;
temp->next=temp1;
temp1->next=NULL;
temp1->data=x;
}
}
void display()
{
node *temp;
temp=head;
if(rear<0)
{
cout<<" queue under flow";
return;
}
while(temp!=NULL)
{
cout<<temp->data<< " ";
temp=temp->next;
}
}
void dequeue()
{
node *temp;
temp=head;
if(rear<0)
{
cout<<"queue under flow";
return;
}
if(front==rear)
{
front=rear=-1;
head=NULL;
return;
}
front++;
head=head->next;
}
};
main()
{
queue s1;
int ch;
clrscr();
cout<<"\n\n\tQUEUE USING LINKED LIST";
cout <<"\n1.enqueue\n2.dequeue\n3.DISPLAY\n4.EXIT";
while(1)
{
cout<<"\n enter your choice:";
cin >> ch;
switch(ch)
{
case 1:
cout <<"\n enter a element";
cin >> ch;
s1.enqueue(ch);
break;
case 2: s1.dequeue();
break;
case 3: s1.display();
break;
case 4: exit(0);
}
}
return 0;
}
OUTPUT:
QUEUE USING LINKED LIST
1.enqueue
2.dequeue
3.DISPLAY
4.EXIT
enter your choice:1
enter a element2
enter your choice:1
enter a element3
enter your choice:1
enter a element4
enter your choice:3
234
enter your choice:2
enter your choice:3
34
enter your choice:4
return;
}
for(int i=front+1;i<=rear;i++)
cout<<queue1[i]<<" ";
}
};
main()
{
int ch;
clrscr();
queue qu;
cout<<"\n1.insert \n2.delete \n3.display \n4.exit";
while(1)
{
cout<<"\nEnter ur choice";
cin>>ch;
switch(ch)
{
case 1:cout<<"\nenter the element";
cin>>ch;
qu.insert(ch);
break;
case 2:qu.delet();
break;
case 3:qu.display();
break;
case 4:exit(0);
}
}
return(0);
}
OUTPUT:
1.insert
2.delete
3.display
4.exit
Enter ur choice1
enter the element5
inserted 5
Enter ur choice1
enter the element6
inserted 6
Enter ur choice1
enter the element9
inserted 9
Enter ur choice3
569
Enter ur choice2
deleted 5
Enter ur choice3
69
Enter ur choice4
program.cpp
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include "d:\stack2.h"
#define size 80
void main()
{
char exp[size];
int len;
double result;
double post(char exp[]);
clrscr();
cout<<"enter the postfix expression\n";
cin>>exp;
len=strlen(exp);
exp[len]='$';
result=post(exp);
cout<<"the value of the expression is"<<result;
getch();
exit(0);
}
double post(char exp[])
{
stk_class obj;
char ch,*type;
double result ,val,op1,op2;
int i;
i=0;
ch=exp[i];
while(ch!='$')
{
if(ch>='0'&&ch<='9')
type="operand";
else if(ch=='+'||ch=='-'||ch=='^'||ch=='*'||ch=='/')
type="operator";
if(strcmp(type,"operand")==0)
{
val=ch-48;
obj.push(val);
}
else
if(strcmp(type,"operator")==0)
{
op2=obj.pop();
op1=obj.pop();
switch(ch)
{
case '+':result=op1+op2;
break;
case '-':result=op1-op2;
break;
case '*':result=op1*op2;
break;
case '/': result=op1/op2;
break;
case '^':result=pow(op1,op2);
break;
}
obj.push(result);
}
i++;
ch=exp[i];
}
result=obj.pop();
return(result);
}
OUTPUT:
enter the postfix expression
19*3+5/
the value of the expression is 2
#include<stdlib.h>
#include<conio.h>
#include"d:\stack3.h"
#define size 80
void main()
{
char exp[size];
int len;
double result;
double post(char exp[]);
clrscr();
cout<<"enter the postfix expression\n";
cin>>exp;
len=strlen(exp);
exp[len]='$';
result=post(exp);
cout<<"the value of the expression is"<<result;
getch();
exit(0);
}
double post(char exp[])
{
char ch,*type;
double result ,val,op1,op2;
int i;
stk_class obj;
i=0;
ch=exp[i];
while(ch!='$')
{
if(ch>='0'&&ch<='9')
type="operand";
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^')
type="operator";
if(strcmp(type,"operand")==0) /* if the character is operand*/
{
val=ch-48;
obj.push(val);
}
else
if(strcmp(type,"operator")==0)/*if it is operator*/
{
op2=obj.pop();
op1=obj.pop();
switch(ch)
{
case '+':result=op1+op2;
break;
case '-':result=op1-op2;
break;
case '*':result=op1*op2;
break;
case '/': result=op1/op2;
break;
case '^':result=pow(op1,op2);
break;
}
obj.push(result);
}
i++;
ch=exp[i];
}
result=obj.pop();/*pop the result*/
return(result);
}
OUTPUT:
enter the postfix expression
19*3+5/
the value of the expression is 2
else
{
int middle=array[last];
int count_1=first-1;
int count_2=last;
while(count_1<count_2)
{
do
{
count_1++;
}
while(array[count_1]<middle);
do
{
count_2--;
}
while(count_2>=0 && array[count_2]>middle);
if(count_1<count_2)
swap(array[count_1],array[count_2]);
}
swap(array[count_1],array[last]);
quick_sort(array,first,count_1-1);
quick_sort(array,count_1+1,last);
}
}
OUTPUT:
Sorted Array :
Element[0] = 0
Element[1] = 1
Element[2] = 2
Element[3] = 3
Element[4] = 4
Element[5] = 4
Element[6] = 6
Element[7] = 7
Element[8] = 8
Element[9] = 9
**********************************************************