Coding buat Doubly linked list

#include
#include

struct node{
struct node *prev;
int data;
struct node *next;
};
void tambahdiawal(struct node **s,int nilai){
struct node *temp;
temp = (struct node *) malloc(sizeof(struct node));

temp->prev = NULL;
temp->data = nilai;
temp->next = *s;
(*s)->prev = temp;
*s = temp;
}
void tambahdiakhir(struct node **s,int nilai){
struct node *temp, *bantu;

if(*s !=NULL){
bantu = *s;

while (bantu->next !=NULL){
bantu = bantu->next;
}
temp = (struct node*)malloc(sizeof(struct node));
temp->prev= bantu;
temp->data= nilai;
temp->next= NULL;

bantu->next =temp;
}else{
*s = (struct node *)malloc(sizeof(struct node));
(*s)->prev = NULL;
(*s)->data = nilai;
(*s)->next = NULL;
}
}
void tampilannilai(struct node *s){
while (s !=NULL){
printf(“%d\n”,s->data);
s = s->next;
}
}
int main (void){
struct node *ptr;

ptr = NULL;

tambahdiakhir(&ptr,10);
tambahdiakhir(&ptr,20);
tambahdiakhir(&ptr,30);
tambahdiakhir(&ptr,40);

printf(“original state : \n”);
tampilannilai(ptr);

tambahdiawal(&ptr,400);

printf(“\nwhen added at the begining : \n”);
tampilannilai(ptr);
getchar();
return 0;
}

semoga bermanfaat..^^

About Dx

My name is Degris
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *