Queue ile ilgili hazır kodlardan uyarlama!!!!!!!

Başlatan suskun, 18 Nisan 2009 - 20:57:15

« önceki - sonraki »

0 Üyeler ve 1 Ziyaretçi konuyu incelemekte.

suskun

Aşşağıda verdiğim örneklerden yararlanarak Dynamic Queue nin hafızasından silen bir fonksiyon nasıl yapılabilir.Yardım eden arkadaşlar biraz sade bir dil ile anlatımını yaparsa daha güzel.Öğrenmek istiyorum ama bazı terımleri anlamakta zorluk cekiyorum acemiyim:)



create a queue with pisitive numbers.print the result on the screen.

#include<iostream.h>
struct elem
{int key;
elem *next;
}*first=NULL,*last=NULL;
void push(int n)
{elem *p=last;
last=new elem;
last->key=n;
last->next=NULL;
if(p)p->next=last;
if(first==NULL)first=last;
}
int pop(int &n)
{if(first)
{elem*p=first;
n=first->key;
first=first->next;
delete p;
if(first==NULL)
last=first;
return 1;}
else return 0;
}
void main()
{int a;
do {cout<<"\nNumber:";
cin>>a;
if(a>0) push(a);
}while(a>0);
cout<<"\nThe queue is:";
while(pop(a))
cout<<a<<endl;
}


create a queue with positive numbers and delete number with index k from queue.

#include<iostream.h>
struct elem
{int key;
elem *next;
}*first=NULL,*last=NULL;
void push(int n)
{elem *p=last;
last=new elem;
last->key=n;
last->next=NULL;
if(p)p->next=last;
if(first==NULL)first=last;
}
int pop(int &n)
{if(first)
{elem*p=first;
n=first->key;
first=first->next;
delete p;
if(first==NULL)
last=first;
return 1;}
else return 0;
}
void main()
{int a,br=0,k,i;
do {cout<<"\nNumbers:";
cin>>a;
if(a>0){push(a);br++;}
}while(a>0);
do
{cout<<"\nk=";cin>>k;}
while(k<=0||k>br);
for(i=1;i<=br;i++)
{pop(a);if(i!=k)push(a);}
cout<<"\nQueue is:";
while(pop(a))
cout<<a<<"\t";
}


create 2 queues with odd and even values and print the result

#include<iostream.h>
struct elem
{int key;
elem *next;
}*f1=NULL,*l1=NULL,*f2=NULL,*l2=NULL;
void push(int n,elem*&first,elem*&last)
{elem *p=last;
last=new elem;
last->key=n;
last->next=NULL;
if(p)p->next=last;
if(first==NULL)first=last;
}
int pop(int &n,elem*&first,elem*&last)
{if(first)
{elem*p=first;
n=first->key;
first=first->next;
delete p;
if(first==NULL)
last=first;
return 1;}
else return 0;
}
void main()
{int a;
do{cout<<"\na=";
cin>>a;
if(a>0)
if(a%2)push(a,f1,l1);
else push(a,f2,l2);
}while(a>0);
cout<<"\nodd values:";
while(pop(a,f1,l1))
cout<<a<<"\t";
cout<<"\nEVen values:";
while(pop(a,f2,l2))
cout<<a<<"\t";
}