Professional Documents
Culture Documents
6 Stacks Excercices 12.11.09
6 Stacks Excercices 12.11.09
( 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);}
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;
}
/*
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);
}
*/