Arkadaşlar aşağıda linked list için sona eleman ekleme kısmının kodlarını hazırladım aşağıya atıyorum bütün her adım yorum satırında yazmaktadır umarım anlaşılır olur ki olduğuna inanıyorum :)
#include<stdio.h>
#include<stdlib.h>struct node{
int data;
struct node * next;
};
// ilk dugumumuz
struct node * head=NULL;void addData(int data){
// temp gecici node ilerde kullanacagimiz , ptr ise eklenecek eleman
struct node * temp,*ptr;
// bellekten yerimizi ayırıyoruz
ptr=(struct node *) malloc(sizeof(struct node));
// datamizi yeni ekleyeceğimiz elemana aktariyoruz
ptr->data=data;
// head NULL yani eleman hiç yok ise yeni eklenen elemani atiyoruz
if(head == NULL){
head=ptr;
head->next=NULL;
}
// eğer ki eleman varsa
else{
temp=head;
// en son düğümü buluyoruz
while(temp->next!=NULL){
temp=temp->next;
}
// son eleman yeni eklenen eleman ptr'ı gösterecek
temp->next=ptr;
// ptr son eleman olduğu için NULL gösterecek.
ptr->next=NULL;
}
}void display(){
struct node *temp;
temp=head;
printf("\n");
while(temp->next!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
printf("%d",temp->data);}
int main(){
int choice,data;
while(1){
printf("\n1)Sona Ekle\n2)Verileri Goruntule\nSeciminizi giriniz : ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Data degerini giriniz : ");scanf("%d",&data);addData(data);break;
case 2: display();break;
}
}
return 0;
}