Double linkedlist
#include <stdio.h>
#include <stdlib.h>
typedef struct element
{
int data;
struct element * prev;
struct element * next;
}element;
element * createdelement(int data)
{
element * new = NULL;
new = (element*)malloc(sizeof(element));
new->data = data;
new->prev = NULL;
new->next = NULL;
return new;
}
void depan(int data,element ** list)
{
element * buat = createdelement(data);
buat->next = (*list);
(*list)->prev = buat;
(*list) = buat;
}
void tengah(int data,int cari,element * list)
{
element * buat = createdelement(data);
element * temp = NULL;
temp = list;
list = buat;
while(temp->data != cari)
{
temp = temp->next;
}
buat->next = temp->next;
temp->next = buat;
buat->prev = temp;
}
void belakang(int data,element * list)
{
element * buat = createdelement(data);
element * temp = NULL;
temp = list;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = buat;
buat->prev = temp;
}
void print(element * list)
{
element * temp = list;
do
{
printf("%d ",temp->data);
temp = temp->next;
}while(temp != NULL);
}
void delpan(element ** list)
{
element * hapus = (*list);
(*list) = hapus->next;
(*list)->prev = hapus;
hapus->next = NULL;
free(hapus);
}
void delte(int cari,element * list)
{
element * hapus = list;
while(hapus->data != cari)
{
hapus = hapus->next;
}
hapus->prev->next = hapus->next;
}
void delbelakang(element * list)
{
element * hapus = list;
while(hapus->next != NULL)
{
hapus = hapus->next;
}
hapus->prev->next = NULL;
free(hapus);
}
int main()
{
element * list = createdelement(9);
depan(10,&list);
tengah(11,9,list);
belakang(12,list);
//delpan(&list);
//delte(11,list);
delbelakang(list);
print(list);
return 0;
}
Komentar
Posting Komentar