You are on page 1of 9

Listat e lidhura

-----------------------------------------------------------------------------------
---------

Ushtrimi 1

#include <stdio.h>
#include <stdlib.h>

struct nyje{
int vl;
/*
char[20] emri;
char * mbiemri;
int mosha;
int notat[21];
*/
struct nyje * pas;
//struct nyje * para;
};
//funksionet qe do ndertojme
struct nyje * krijoNyje(int vlera){
struct nyje * eliri;
eliri=(struct nyje *)malloc(sizeof(struct nyje));
eliri->vl=vlera;
eliri->pas=NULL;
//eliri->para=NULL;
}

struct nyje * hiqFillim(struct nyje * fillimi){


if(fillimi==NULL){
printf("Asnje element nuk mund te hiqet nga lista. Lista eshte bosh.\n");
return NULL;
}
fillimi=fillimi->pas;
return fillimi;
}

struct nyje * hiqFund(struct nyje * fillimi){


if(fillimi==NULL){
printf("Asnje element nuk mund te hiqet nga lista. Lista eshte bosh.\n");
return NULL;
}
if(fillimi->pas==NULL){
return NULL;
}

struct nyje * tmp=fillimi;


while(tmp->pas->pas!=NULL)
tmp=tmp->pas;

tmp->pas=NULL;
return NULL;
}
struct nyje * hiqPozicion(struct nyje * fillimi, int poz){
if(fillimi==NULL){
printf("Asnje element nuk mund te hiqet nga lista. Lista eshte bosh.\n");
return NULL;
}
if(poz==1){
fillimi=fillimi->pas;
return fillimi;
}
struct nyje * tmp=fillimi;
int i=1;
while (i<=poz-2&&tmp->pas!=NULL){
tmp=tmp->pas;
i++;
}
if(tmp->pas==NULL){
printf("Pozicioni nuk eshte i sakte\n");
return fillimi;
}

tmp->pas=tmp->pas->pas;
return fillimi;
}

struct nyje * hiqVleraNjeHere(struct nyje * fillimi,int vlera){


if(fillimi==NULL){
printf("Asnje element nuk mund te hiqet nga lista. Lista eshte bosh.\n");
return NULL;
}
if(fillimi->vl==vlera){
fillimi=fillimi->pas;
return fillimi;
}

struct nyje * tmp=fillimi;


while(tmp->pas!=NULL && tmp->pas->vl!=vlera)
tmp=tmp->pas;

if(tmp->pas==NULL){
printf("Vlera nuk gjendet ne liste\n");
return fillimi;
}

tmp->pas=tmp->pas->pas;
return fillimi;
}

struct nyje * shtoPozicion(struct nyje * fillimi, int poz, int vlera){


struct nyje * eliri=krijoNyje(vlera);

if(poz==1){
eliri->pas=fillimi;
fillimi=eliri;
return fillimi;
}

struct nyje * tmp=fillimi;


int i=1;
while (i<=poz-2&&tmp->pas!=NULL){
tmp=tmp->pas;
i++;
}

if(tmp->pas==NULL&&i!=poz-1){
printf("Pozicioni nuk eshte i sakte\n");
return fillimi;
}
eliri->pas=tmp->pas;
tmp->pas=eliri;
}

bool gjendetNeListe(struct nyje * fillimi,int vlere){


struct nyje * tmp=fillimi;
while(tmp!=NULL){
if(tmp->vl==vlere)
return true;
tmp=tmp->pas;
}
return false;
}

int sasiaListe(struct nyje * fillimi,int vlere){


int nr=0;
struct nyje * tmp=fillimi;
while(tmp!=NULL){
if(tmp->vl==vlere)
nr++;
tmp=tmp->pas;
}
return nr;
}

struct nyje * futFund(struct nyje * fillimi,int vlera){


//krijojme elementin ne memorje. Elementi adresohet me eliri
struct nyje * eliri;
eliri=krijoNyje(vlera);
//kontrollojme nese lista eshte bosh
//nese eshte bosh kthejme elementin e krijuar
//i cili tashme eshte lista e re
if(fillimi==NULL){
return eliri;
}

struct nyje * tmp=fillimi;


while(tmp->pas!=NULL){
tmp=tmp->pas;
}
tmp->pas=eliri;
return fillimi;
}

struct nyje * futFillim(struct nyje * fillimi,int vlera){


//krijojme elementin ne memorje. Elementi adresohet me eliri
struct nyje * eliri;
eliri=krijoNyje(vlera);
eliri->pas=fillimi;
fillimi=eliri;
return fillimi;
}

struct nyje * krijoListe(struct nyje * fillimi,int nr,int renditje){


if(renditje==0){
for(int i=1;i<=nr;i++){
fillimi=futFillim(fillimi,i);
}
}
else{
for(int i=1;i<=nr;i++){
fillimi=futFund(fillimi,i);
}
}
return fillimi;
}

void afishoListe(struct nyje * fillimi){


struct nyje * tmp=fillimi;
while (tmp!=NULL){
printf("%d ",tmp->vl);
tmp=tmp->pas;
}
printf("\n");
}
int main(){
struct nyje * koka=NULL;
koka=krijoListe(koka,10,0);

struct nyje * kokaF=NULL;


kokaF=krijoListe(kokaF,15,1);

struct nyje * tmp=koka;

int s=0,nr=0,nr_neg=0;
while (tmp!=NULL){
s=s+tmp->vl;
nr++;
if(tmp->vl<0)
nr_neg++;
tmp=tmp->pas;
}

printf("\n");
afishoListe(koka);
afishoListe(kokaF);
koka=hiqVleraNjeHere(koka,3);
afishoListe(koka);
/*
for(struct nyje * tmp=koka;tmp!=NULL;tmp=tmp->pas){
printf("%d ",tmp->vl);
}
*/
printf("Shuma %d\n",s);
printf("Numri elementeve %d\n",nr);
printf("Numri elementeve negative %d\n",nr_neg);
return 0;
}

-----------------------------------------------------------------------------------
-------

Ushtrimi 2

#include <stdio.h>
#include <stdlib.h>

struct nyje{
int vl;
struct nyje * pas;
} ;

struct nyje * mbushListe(int n){


int vlera;
struct nyje * k;
k=0;
for(int i=1;i<=n;i++){
struct nyje * eliri;
eliri = (struct nyje *) malloc( sizeof(struct nyje) );
scanf("%d",&vlera);
eliri->vl=vlera;
eliri->pas=k;
k=eliri;
}
return k;
}

struct nyje * futFillim(struct nyje * koka,int vl){


struct nyje * eliri;
eliri = (struct nyje *) malloc( sizeof(struct nyje) );
eliri->vl=vl;
eliri->pas=koka;
koka=eliri;
return koka;
}

struct nyje * futFund(struct nyje * koka,int vl){


struct nyje * eliri;
eliri = (struct nyje *) malloc( sizeof(struct nyje) );
eliri->vl=vl;
eliri->pas=NULL;
if(koka==NULL){
koka=eliri;
return koka;
}
struct nyje * q=koka;
while(q->pas!=NULL)
q=q->pas;

q->pas=eliri;
return koka;
}

void afishoListe(struct nyje * koka){


struct nyje * p=koka;
while(p!=0){
printf("%d ",p->vl);
p=p->pas;
}
printf("\n");
}

int shumaListe(struct nyje * koka){


int s=0;
struct nyje * p=koka;
while(p!=0){
s+=p->vl;
p=p->pas;
}
return s;
}

int shumaListeFor(struct nyje * koka){


int s=0;
for(struct nyje * p=koka;p!=0;p=p->pas){
s+=p->vl;
}
return s;
}
int nrNeg(struct nyje * koka){
int nr=0;
struct nyje * p=koka;
while(p!=0){
if(p->vl<0)
nr++;
p=p->pas;
}
return nr;
}

struct nyje * futElementPozicion(struct nyje * koka, int vlere, int poz){

if(poz==1){
struct nyje * eliri;
eliri = (struct nyje *) malloc( sizeof(struct nyje) );
eliri->vl=vlere;
eliri->pas=koka;
koka=eliri;
}
else{
int i=1;
struct nyje * p=koka;
while(p->pas!=0 && i<poz-1){
//printf("%d ",p->vl);
p=p->pas;
i++;
}
struct nyje * eliri;
eliri = (struct nyje *) malloc( sizeof(struct nyje) );
eliri->vl=vlere;
eliri->pas=p->pas;
p->pas=eliri;
}
return koka;
}
int main(){
int n;
struct nyje * koka;

printf("sa elemente do kete lista?\n");


scanf("%d",&n);
koka=mbushListe(n);
koka=futFillim(koka,5);
koka=futFund(koka,13);
koka=futElementPozicion(koka,99,13);
afishoListe(koka);
printf("Shuma e listes eshte %d\n",shumaListe(koka));
printf("Numri i elementeve negative eshte %d\n",nrNeg(koka));
futElementPozicion(koka,12,2);

return 0;
}

-----------------------------------------------------------------------------------
-------

Lista dydrejtimore

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct nyje{
int vl;
struct nyje * pas;
struct nyje * para;
};

struct nyje * nyjeRe(int vlera){


struct nyje * eliri;
eliri = (struct nyje *) malloc( sizeof(struct nyje) );
eliri->vl=vlera;
eliri->pas=NULL;
eliri->para=NULL;
return eliri;
}

struct nyje * shtoElFillim(struct nyje * koka, int vlera){


struct nyje * eliri=nyjeRe(vlera);
eliri->pas=koka;
koka->para=eliri;
koka=eliri;
}

bool eshteBosh(struct nyje * koka){


if(koka==NULL)
return true;
return false;
}

struct nyje * shtoElFund(struct nyje * koka, int vlera){


struct nyje * eliri=nyjeRe(vlera);
if(eshteBosh(koka))
return eliri;
struct nyje* p=koka;
while(p->pas!=NULL)
p=p->pas;
p->pas=eliri;
eliri->para=p;
return koka;
}

struct nyje * fshiElFillim(struct nyje * koka){


if(eshteBosh(koka)||koka->pas!=NULL)
return NULL;
koka=koka->pas;
koka->para=NULL;
return koka;
}

int shuma(struct nyje * koka){


int s=0;
struct nyje * p=koka;
while(p!=NULL){
s=s+p->vl;
p=p->pas;
}
return s;
}

void afisho(struct nyje * koka){


struct nyje * p=koka;
while(p!=NULL){
printf("%d ",p->vl);
p=p->pas;
}
printf("\n");
}

struct nyje * hiqVl(struct nyje * koka, int vlera){


if(eshteBosh(koka))
return koka;
struct nyje* p=koka;
while(p!=NULL&&p->vl!=vlera)
p=p->pas;
if(p==NULL)
return koka;
if(p->para==NULL){
koka=koka->pas;
if(koka!=NULL)
koka->para=NULL;
return koka;
}

if(p->pas==NULL){
p->para->pas=NULL;
return koka;
}

p->para->pas=p->pas;
p->pas->para=p->para;
return koka;
}

int main(){
struct nyje * Lista1=NULL;
struct nyje * Lista2=NULL;

Lista1=shtoElFillim(Lista1,5);

Lista1=shtoElFillim(Lista1,7);
Lista1=shtoElFillim(Lista1,10);

Lista2=shtoElFund(Lista2,15);
Lista2=shtoElFund(Lista2,0);
Lista2=shtoElFund(Lista2,3);

afisho(Lista1);
afisho(Lista2);
printf("Shuma e listes se pare: %d\n",shuma(Lista1));
printf("Shuma e listes se dyte: %d\n",shuma(Lista2));
return 0;
}

You might also like