Linklist Michael Wijaya 1701293010
Posted March 21st, 2014 by michaelwijaya96#include<stdio.h>
#include<stdlib.h>
struct data{
int x;
struct data* next;
struct data* prev;
}*connector;
void print(){
if(connector!=0)
{
while(connector->next!=0)
{
printf(“%d”,connector->x);
connector = connector->next;
}
printf(“->%d”,connector->x);
}
printf(“\n”);
}
int main()
{
struct data *head;
struct data *tail;
struct data *insert;
//masukkan data 1 dan 3
head =(struct data*)malloc(sizeof(struct data));
tail =(struct data*)malloc(sizeof(struct data));
connector =(struct data*)malloc(sizeof(struct data));
head->x=1;
head->next=0;
head->prev=0;
connector = head;
tail = head;
connector->next =(struct data*)malloc(sizeof(struct data));
connector->prev =(struct data*)malloc(sizeof(struct data));
connector = connector->next;
connector->x=3;
connector->next=0;
connector->prev=head;
tail=connector;
connector = head;
print();
//masukkan data 2 diantara 1 dan 3
connector = head;
if(connector->next!=0){
while(connector->next!=0){
if(connector->x==1&&connector->next->x==3)
{
insert = (struct data*)malloc(sizeof(struct data));
insert->x=2;
insert->next=connector->next;
insert->prev=head->next;
connector->next = insert;
}
}
}
connector = head;
print();
getchar();
//masukkan 0 di depan
connector = head;
insert = (struct data*)malloc(sizeof(struct data));
insert->x=0;
insert->next=head;
head=insert;
connector = head;
print();
//masukkan 5 di paling akhir
insert= (struct data*)malloc(sizeof(struct data));
insert->x = 5;
insert->next = 0;
insert->prev=tail;
tail->next=insert;
tail=tail->next;
connector = head;
print();
getchar();
return 0;
}
Leave a Reply