moonlife1
New member
- Katılım
- 10 Kas 2007
- Mesajlar
- 71
- Reaction score
- 0
- Puanları
- 0
Selam,
C++ dilinde DEQUE veri yapısı ile girilen sayıları, INSERT SORTING algoritması ile sıralayacak bir program yazmaya çalışıyorum.
Bu veri yapısını ve algoritmayı yazdım, bunlarda sorum yok. Benim sorunum, MAIN() kısmında bunları birbirine bağlayamamam. Hatayı nerede yaptığımı görebilen var mı?
C++ dilinde DEQUE veri yapısı ile girilen sayıları, INSERT SORTING algoritması ile sıralayacak bir program yazmaya çalışıyorum.
Bu veri yapısını ve algoritmayı yazdım, bunlarda sorum yok. Benim sorunum, MAIN() kısmında bunları birbirine bağlayamamam. Hatayı nerede yaptığımı görebilen var mı?
PHP:
#include <iostream.h>
#include <conio.h>
void deque();
void insertsort(int x[], int n);
void push_l(int n); //prototip
void push_r(int n); //prototip
int pop_l(int &n); //prototip
int pop_r(int &n); //prototip
struct elem{int key; elem *next;}*left=NULL, *right=NULL;
void deque()
{
int ch;
do
{
int num;
cout<<" Menu for deque:"<<endl;
cout<<"1 - Push from left"<<endl;
cout<<"2 - Push from right"<<endl;
cout<<"3 - Pop from left"<<endl;
cout<<"4 - Pop from right"<<endl;
cout<<"5 - End of work"<<endl;
cout<<"Make your choice: "; cin>>ch;
switch(ch)
{
case(1):
case(2):
cout<<"Enter number:"; cin>>num;
if(ch==1)
push_l(num);
else
push_r(num);
break;
case(3):
{
if(pop_l(num))
{
cout<<num<<endl;
getche();
}
else
{cout<<"Empty structure!"<<endl; getch();}
break;
}
case(4):
{if(pop_r(num))
{cout<<num<<endl; getche();}
else
{cout<<"Empty structure!"<<endl; getch();}
}
}
}
while(ch!=5);
}
void push_l(int n) //soldan ekleme
{
elem*p;
p=left;
left=new elem;
left->key=n;
left->next=p;
if(right==NULL)
{right=left;}
}
void push_r(int n) //sagdan ekleme
{
elem*p;
p=right;
right=new elem;
right->key=n;
right->next=NULL;
if(left==NULL)
left=right;
else
p->next=right;
}
int pop_l(int &n) //soldan cikarma
{
elem*p;
if(left)
{
n=left->key;
p=left;
left=left->next;
if(left==NULL)
right=NULL;
delete p;
return 1;
}
else
return 0;
}
int pop_r(int &n) //sagdan cikarma
{
elem*p;
if(right)
{
n=right->key;
if(left==right)
{
delete right;
left=right=NULL;
}
else
{
p=left;
while(p->next!=right)
p++;
n=right->key;
p->next=NULL;
delete right;
right=p;
return 1;
}
}
else
return 0;
}
void insertsort(int x[], int n)
{
int i,k,y;
for(k=1; k<n; k++)
{
y=x[k];
for(i=k-1; i>=0 && y<x[i]; i--)
x[i+1]=x[i];
x[i+1]=y;
};
}
void main()
{
cout<<deque<<endl; //Burada sayilarin girilmesi icin deque fonksiyonunu cagirmaya calistim.
cout<<"Elements are sorted: "<<insertsort<<endl; //Burada girilen sayilarin siralanmasi icin insertsort
//fonksiyonunu cagirmaya calistim.
}