Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

//Data Structure and Algorithms - Stacks Exercises

( Funksioni PUSH )
#include <iostream>
using namespace std;
struct Node{
int Element;
Node *Next;
};
typedef Node *Position;
typedef Node *Stack;

Stack CreateEmptyStack()
{
Stack s=NULL;
return s;}

bool IsEmpty(Stack s)
{return (s==NULL);}

Stack Push(int x, Stack s)


{
Node *tmp=new Node; tmp->Element=x;
if(tmp==NULL) cout<<"\n S'ka memorje te mjaftueshme!";
else{
if(s==NULL){
tmp->Next=NULL;
s=tmp;}
else{ tmp->Next=s;
s=tmp;
}
}
return s;
}

//Funksioni POP - Si te largohet elementi i pare


Stack Pop(Stack s)
{
if(s==NULL)
cout<<"\n Stack-u eshte i zbrazet!";
else{ Node *tmp=s;
s=s->Next;
delete tmp;
}
return s;
}
//Funksioni TOP
int Top(Stack s)
{ if(s!=NULL)
return (s->Element);
else cout<<"\n Shtack-u eshte bosh!";
}
//Funksioni "Print Stack"

void PrintStack(Stack s)
{ if(s==NULL)
cout<<"\n Empty Stack!";// Kjo do te perseritet deri sa kemi
elemente ne stack
else {
Stack snd=NULL; //snd - Stack ndihmes
while(!IsEmpty(s)) //S!=NULL
{ cout<<Top(s)<<" ";
snd=Push(Top(s), snd);
s=Pop(s);
}
//Kthimi i elementeve prap ne stack-un s
while(!IsEmpty(snd))
{ s=Push(Top(snd),s);
snd=Pop(snd);
}
}
}

void PrintReverseS(Stack s)
{ if(s==NULL)
cout<<"\n Empty Stack!";// Kjo do te perseritet deri sa kemi
elemente ne stack
else {Stack snd=NULL; //snd - Stack ndihmes
while(!IsEmpty(s)) //S!=NULL
{ snd=Push(Top(s), snd);
s=Pop(s);
}
//Kthimi i elementeve prap ne stack-un s
while(!IsEmpty(snd))
{ cout<<Top(snd)<<" ";
s=Push(Top(snd),s);
snd=Pop(snd);
}
}
}

Stack DestroyStack(Stack s)
{ if(s==NULL)
cout<<"\n Empty Stack!";// Kjo do te perseritet deri sa kemi
elemente ne stack
else {Stack snd=NULL; //snd - Stack ndihmes
while(!IsEmpty(s)) //S!=NULL
{
s=Pop(s);
}

}
return s;}
//Programi kryesor - Vetem vlerat çift i vendosim ne stack
int main()
{
Stack s=CreateEmptyStack();
int sent=0;
int n;
while(sent!=-1)
{ cout<<"\n Jepe numrin:";
cin>>n;
if(n%2==0)//Numrin çift
s=Push(n,s);
cout<<"\n A doni te lexoni ende vlera? Per jo shtyp
-1!";
cin>>sent;
}

cout<<"\n Numrat cift sipas rradhitjes qe i lexuam


jane:";
PrintReverseS(s);
s=DestroyStack(s);
cin.get();
}

/*

//Te shkruhet program per numerimin e elmenteve çift ne


stack duke perdorur POP,PUSH dhe TOP pa pas nevoj per
implementimin
int main()
{
void NumberOddElement(Stack s)
int num=0;
{ if(s==NULL)
cout<<"\n Empty Stack!";
else {Stack snd=NUL;
while(!IsEmpty(s))
{ if(top(s)%2==0) num++;
Push(Top(s), snd);
Pop(s);
}
//Te definohet funksion i cili nga stack-u i dhene i largon
elmentet çift
int main()
{
Stack NewStack(Stack s)

Stack snd=NUL;
while(!IsEmpty(s))
{ if(Top(s)%2==0)
Pop(s);
else{ Push(top(s), snd)
Pop(s);
}
while(!IsEmpty(snd))
{
Push(Top(snd), s);
Pop (snd);
}
}
return s;
}
//Per fjalin e dhene nga tastatura te shtypet ne forme
inverse ose te invertohet fjalia "Hello World"
int main()
{

char fjali[30];
cout<<"\n Jepe fjalin:";
cin.getline(fjali,30);
for(int i=0; fjali[i]!='\0';i++)
Push(fjali[i],s);
cout<<"\n Fjalia e invertuar:";
PrintStack(s);
char invert[30]; int i=0;
while(!IsEmpty(s))
{ invert[i]=Top(s);
Pop(s);
i++;
}
cout<<"\n Fjalia inverte:"
<<invert<<endl;
cin.get();
}
//Te shkruhet program qe i kthen ne shkronja te medhaja dhe
te shtypet fjalia siç eshte e dhene prej ne fund
// dhe ne fillim
int main()
{
Stack s=CreateEmptyStack();
char fjali[30];
cout<<"\n Jepe fjalin:";
cin.getLine(fjali,s)
for(int i=0;fjali[i]!='\0';i++)
if(islower(fjali[i])
Push(islower(fjali[i])
Push(toupper(fjali[i]),s)
else Push(fjali[i],s)
PrintRev(s);
}
*/

You might also like