Ushtrim
Ushtrimi me poshte jep deklarimin e stives dhe rradheve te parqaqitura si nje strukture. Per perpunimin e stivave dhe rradheve jane perdorur listat e lidhura. Ne ushtrim jep gjithashtu edhe nje perdorim i listave e trajtuar si nje strukture.(Shihni ndryshimet ne krahasim me ushtrimet e treguara ne leksionin me siper)
#include <stdio.h>
#include <stdlib.h>
struct nyje{
int vlera;
struct nyje * pas;
};
struct liste{
struct nyje * koka;
};
struct stive{
struct nyje * koka;
int nr;
};
struct rradhe{
struct nyje * fillim;
struct nyje * fund;
int nr;
};
void mbushListe(struct liste * l,int n){
l->koka=0;
int el;
printf("Vendos elementet\n");
for(int i=1;i<=n;i++){
struct nyje * eliri;
eliri=(struct nyje *)malloc(sizeof(struct nyje *));
scanf("%d",&el);
eliri->vlera=el;
eliri->pas=l->koka;
l->koka=eliri;
}
}
void afishoListe(struct liste * l){
struct nyje * p=l->koka;
while(p!=0){
printf("%d ",p->vlera);
p=p->pas;
}
}
bool bosh(struct stive * s){
if(s->nr==0)
return true;
return false;
}
void shtoStive(struct stive * s, int el){
struct nyje * eliri;
eliri=(struct nyje *)malloc(sizeof(struct nyje *));
eliri->vlera=el;
eliri->pas=s->koka;
s->koka=eliri;
s->nr=s->nr+1;
}
void hiqStive1(struct stive * s){
if(s->nr==0){
printf("Stive pa elemente\n");
return;
}
//int el=s->koka->vlera;
s->koka=s->koka->pas;
s->nr=s->nr-1;
}
nyje * hiqStive2(struct stive * s){
if(s->nr==0){
printf("Stive pa elemente\n");
return NULL;
}
nyje * p=s->koka;
s->koka=s->koka->pas;
s->nr=s->nr-1;
return p;
}
int hiqStive(struct stive * s){
if(s->nr==0){
printf("Stive pa elemente\n");
return -9999;
}
int el=s->koka->vlera;
s->koka=s->koka->pas;
s->nr=s->nr-1;
return el;
}
void mbushStive(struct stive * s, int n){
int el;
printf("Vendosni elementet\n");
for(int i=1;i<=n;i++){
scanf("%d",&el);
shtoStive(s,el);
}
}
void afishoStive(struct stive * s){
while(bosh(s)==false)
printf("%d ",hiqStive(s));
}
void afishoStiveRuajElement(struct stive * s){
struct stive * tmp=0;
tmp=(struct stive *)malloc(sizeof(struct stive *));
tmp->koka=0;
tmp->nr=0;
while(bosh(s)==false){
int el=hiqStive(s);
//nese duam ti afishojme nga i fundit te i pari
//printf("%d ",el);
shtoStive(tmp,el);
}
while(bosh(tmp)==false){
int el=hiqStive(tmp);
//nese duam ti afishojme nga i pari te i fundit
printf("%d ",el);
shtoStive(s,el);
}
}
bool boshRradhe(struct rradhe * rr){
if(rr->nr==0)
return true;
return false;
}
void shtoRradhe(struct rradhe * rr, int el){
struct nyje * eliri;
eliri=(struct nyje *)malloc(sizeof(struct nyje *));
eliri->vlera=el;
if(boshRradhe(rr)){
rr->fillim=eliri;
rr->fund=eliri;
eliri->pas=0;
}
else{
rr->fund->pas=eliri;
rr->fund=eliri;
}
rr->nr=rr->nr+1;
}
int hiqRradhe(struct rradhe * rr){
if(boshRradhe(rr)){
printf("Rradhe boshe\n");
return -9999;
}
int el=rr->fillim->vlera;
rr->fillim=rr->fillim->pas;
rr->nr=rr->nr-1;
return el;
}
void mbushRradhe(struct rradhe * rr, int n){
int el;
printf("Vendosni elementet\n");
for(int i=1;i<=n;i++){
scanf("%d",&el);
shtoRradhe(rr,el);
}
}
void afishoRradhe(struct rradhe * rr){
while(boshRradhe(rr)==false)
printf("%d ",hiqRradhe(rr));
}
void afishoRradheRuajElementet(struct rradhe * rr){
int n=rr->nr;
for(int i=1;i<=n;i++){
int el=hiqRradhe(rr);
printf("%d ",el);
shtoRradhe(rr,el);
}
}
int main(){
int n;
printf("Jep sasine e numrave\n");
scanf("%d",&n);
//per listat
/*
struct liste * lista;
lista=(struct liste *)malloc(sizeof(struct liste *));
mbushListe(lista,n);
afishoListe(lista);
*/
//per stivat
struct stive * stiva=0;
stiva=(struct stive *)malloc(sizeof(struct stive *));
stiva->koka=0;
stiva->nr=0;
mbushStive(stiva,n);
printf("Stiva ka %d elemente\n",stiva->nr);
afishoStiveRuajElement(stiva);
//per rradhet
/*
struct rradhe * rradha;
rradha=(struct rradhe *)malloc(sizeof(struct rradhe *));
rradha->nr=0;
mbushRradhe(rradha,n);
printf("Rradha ka %d elemente\n",rradha->nr);
afishoRradheRuajElementet(rradha);
printf("\n");
printf("Rradha ka %d elemente\n",rradha->nr);
afishoRradheRuajElementet(rradha);
*/
printf("\n");
return 0;
}