Hafızada önceden oluşturulmuş tek bağlı bir listeyi çift bağlı bir listeye çeviren program
#include<stdio.h>
#include<stdlib.h>
struct tekbagli {
int info;
struct tekbagli *next;
};
typedef struct tekbagli *TEKBAGLIPTR;
struct ciftbagli
{
int info;
struct ciftbagli *next;
struct ciftbagli *before;
};
typedef struct ciftbagli *CIFTBAGLIPTR;
void insertTekBagli(TEKBAGLIPTR *listptr,int deger)
{
if(*listptr==NULL) {
*listptr=(TEKBAGLIPTR)malloc(sizeof(struct tekbagli));
(*listptr)->info=deger;
(*listptr)->next=NULL;
}
else
insertTekBagli(&((*listptr)->next),deger);
}
void printtekbagli(TEKBAGLIPTR tekStart)
{
if(tekStart != NULL)
{
printf("%5d",tekStart->info);
printtekbagli(tekStart->next);
}
}
void printciftbagli(CIFTBAGLIPTR ciftStart)
{
if(ciftStart != NULL)
{
printf("%5d",ciftStart->info);
printciftbagli(ciftStart->next);
}
}
void insertCiftBagli(CIFTBAGLIPTR *ciftStart,int deger)
{
CIFTBAGLIPTR currentPtr,previousPtr,newPtr;
newPtr = (CIFTBAGLIPTR)malloc(sizeof(struct ciftbagli));
newPtr->info=deger;
currentPtr = (*ciftStart);
previousPtr = NULL;
while(currentPtr != NULL && currentPtr->info < deger )
{
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
if(previousPtr == NULL)
{
(*ciftStart) = newPtr;
newPtr->next = currentPtr;
newPtr->before = NULL;
}
else
{
previousPtr->next = newPtr;
newPtr->before = previousPtr;
newPtr->next = currentPtr;
if(currentPtr != NULL)
currentPtr->before = newPtr;
}
}
void tektocift(TEKBAGLIPTR *tekstart,CIFTBAGLIPTR *ciftstart)
{
TEKBAGLIPTR currentPtr,temp;
int deger;
currentPtr=(*tekstart);
while(currentPtr!=NULL) {
deger=currentPtr->info;
insertCiftBagli(ciftstart,deger);
temp=currentPtr;
currentPtr=currentPtr->next;
free(temp);
}
}
int main()
{
TEKBAGLIPTR tekStart = NULL;//tek bağlı listenin başlangıç adresi...
CIFTBAGLIPTR ciftStart = NULL;
int kayitSayisi;
int deger;
printf("Kac tane kayit girmek istiyorsunuz.... ");
scanf("%d",&kayitSayisi);
flushall();
for(int i = 0;i < kayitSayisi;i++)
{
printf("%d. eleman.. ",i+1);
scanf("%d",°er);
flushall();
insertTekBagli(&tekStart,deger);
}
printtekbagli(tekStart);
printf(" NULL
tek bagliyi cift bagliya cevirelim..
");
tektocift(&tekStart,&ciftStart);
printciftbagli(ciftStart);
printf("NULL");
return 0;
}
Hafızada önceden oluşturulmuş bir kuyruğun elemanlarını bir ikili ağaca yerleştiren program
#include "stdio.h"
#include "stdlib.h"
struct kuyruk{
int info;
struct kuyruk *next;
};
typedef struct kuyruk *KUYRUKPTR;
struct tree{
int info;
struct tree *left;
struct tree *right;
};
typedef struct tree *TREENODEPTR;
void insertKuyruk(KUYRUKPTR *bas,KUYRUKPTR *son,int deger)
{
KUYRUKPTR newptr;
newptr=(KUYRUKPTR)malloc(sizeof(struct kuyruk));
newptr->info=deger;
newptr->next=NULL;
if(*bas==NULL)
*bas=newptr;
else
(*son)->next=newptr;
*son=newptr;
}
int remove(KUYRUKPTR *bas,KUYRUKPTR *son)
{
KUYRUKPTR tempptr;
int deger;
deger=(*bas)->info;
tempptr=(*bas);
*bas=(*bas)->next;
if((*bas)==NULL)
*son=NULL;
free(tempptr);
return deger;
}
void printKuyruk(KUYRUKPTR tempptr)
{
while(tempptr!=NULL){
printf("%5d",tempptr->info);
tempptr=tempptr->next;
}
}
void insertTree(TREENODEPTR *treeptr,int deger)
{
if((*treeptr)==NULL) {
(*treeptr)=(TREENODEPTR)malloc(sizeof(struct tree));
(*treeptr)->info=deger;
(*treeptr)->left=NULL;
(*treeptr)->right=NULL;
}
else {
if(deger>(*treeptr)->info)
insertTree(&((*treeptr)->right),deger);
else if(deger<(*treeptr)->info)
insertTree(&((*treeptr)->left),deger);
}
}
void printTree(TREENODEPTR treeptr)
{
if(treeptr!=NULL) {
printf("%5d",treeptr->info);
printTree(treeptr->left);
printTree(treeptr->right);
}
}
void kuyrukToTree(KUYRUKPTR *bas,KUYRUKPTR *son,TREENODEPTR *treeptr)
{
KUYRUKPTR currentptr;
int deger;
currentptr = (*bas);
while(currentptr != NULL)
{
deger= remove(bas,son);
insertTree(treeptr,deger);
currentptr = (*bas);
}
}
int main()
{
TREENODEPTR kokPtr = NULL;
KUYRUKPTR bas = NULL;
KUYRUKPTR son = NULL;
int kayitSayisi;
int deger;
printf("Elaman sayisi... ");
scanf("%d",&kayitSayisi);
flushall();
for(int i=0;i<kayitSayisi;i++)
{
printf("%d.eleman ",i+1);
scanf("%d",°er);
flushall();
insertKuyruk(&bas,&son,deger);
}
printKuyruk(bas);
printf("
Kuyrugu agaca cevir..
");
kuyrukToTree(&bas,&son,&kokPtr);
printTree(kokPtr);
return 0;
}
Hafızada önceden oluşturulmuş ikili ağacı tek bağlı listeye aktaran program
#include<stdio.h>
#include<stdlib.h>
struct tekbagli {
int info;
struct tekbagli *next;
};
typedef struct tekbagli *TEKBAGLIPTR;
struct agac {
int info;
struct agac *left,*right;
};
typedef struct agac *AGACPTR;
void tekbagliyaekle(TEKBAGLIPTR *listptr,int deger)
{
if(*listptr==NULL) {
*listptr=(TEKBAGLIPTR)malloc(sizeof(struct tekbagli));
(*listptr)->info=deger;
(*listptr)->next=NULL;
}
else
tekbagliyaekle(&((*listptr)->next),deger);
}
void printlist(TEKBAGLIPTR tekStart)
{
if(tekStart != NULL)
{
printf("%5d",tekStart->info);
printlist(tekStart->next);
}
}
void agacaekle(AGACPTR *treeptr,int deger)
{
if(*treeptr==NULL){
*treeptr=(AGACPTR)malloc(sizeof(struct agac));
(*treeptr)->info=deger;
(*treeptr)->left=NULL;
(*treeptr)->right=NULL;
}
else
if(deger<(*treeptr)->info)
agacaekle(&((*treeptr)->left),deger);
else if(deger>(*treeptr)->info)
agacaekle(&((*treeptr)->right),deger);
}
void printTree(AGACPTR treePtr)
{
if(treePtr != NULL)
{
printf("%d->",treePtr->info);
printTree(treePtr->left);
printTree(treePtr->right);
}
}
void treetolist(AGACPTR treeptr,TEKBAGLIPTR *listptr)
{
if(treeptr!=NULL) {
tekbagliyaekle(listptr,treeptr->info);
treetolist(treeptr->left,listptr);
treetolist(treeptr->right,listptr);
}
}
int main()
{
TEKBAGLIPTR startptr=NULL;
AGACPTR kokptr=NULL;
int deger,kayitsayisi=0;
printf("kayit sayisini girin..");
scanf("%d",&kayitsayisi);
for(int i=0;i<kayitsayisi;i++)
{
printf("%d. elemani girin.
",i+1);
scanf("%d",°er);
tekbagliyaekle(&startptr,deger);
}
printf("Tek bagli liste:");
printlist(startptr);
printf("
**************
");
listtotree(startptr,&kokptr);
printf("listeyi agaca cevirdik.
");
printTree(kokptr);
printf("NULL
");
return 0;
}
|