C – Tek Yönlü Bağlı Liste

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

//tek yon bagli liste yapisi
//next = NULL olduğunda sonlanır 
struct node {
  int data;
  struct node *next;
};

int main(){

	struct node *head; //henüz yer kaplamıyor

	//liste için bellekte yer ayıralım.
	//C++'ta head -> next = new node()
	//C'de memory allocation ile bellekte yer ayırıyoruz.
	head = (struct node*)malloc(sizeof(struct node));

	//listeye, datası 1 olan eleman ekleyelim.
	head->data = 1;
	//listenin next'ine null atayarak sonlandırıyoruz..
	head->next = NULL;

	printf("1. eleman data: %i\n", head->data);
	printf("1. eleman next: %p\n\n", head->next);

	//listeye eleman ekleyelim
	printf("listenin sonuna eklemek için bellekten yer ayır.\n");
	head -> next = (struct node *)malloc(sizeof(struct node));
	printf("1. eleman next'ine yeni elemanın adresi eklendi.\n");
	printf("1. eleman next: %p\n\n", head->next);
	

	head -> next -> data = 3;
	head -> next -> next = NULL; 

	printf("2. eleman adres: %p\n", head->next);
 	printf("2. eleman data: %i\n", head->next->data);
 	printf("2. eleman next: %p\n", head->next->next);

}