Tugas Struktur Data (32PPT) – Doubly Linked List

Download: http://pastebin.com/SpCBXAhU

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

struct node
{
int data;
struct node *prev;
struct node *next;
}*head, *last;

void display()
{
struct node *temp;
temp=head;

while(temp!=NULL)
{
printf(” %d “,temp->data);
temp=temp->next;
}
}

void insertAwal()
{
struct node *var,*temp;
var=(struct node *)malloc(sizeof(struct node));
var->data=0;
temp=var;
temp->prev=NULL;
temp->next=head;
head->prev=temp;
head=temp;
}

void insertTengah()
{
struct node *temp,*var,*temp1;
var=(struct node *)malloc(sizeof(struct node));
var->data=2;
temp=head;

while(temp!=NULL && temp->data!=1)
{
temp=temp->next;
}

temp1=temp->next;
temp->next=var;
var->prev=temp;
var->next=temp1;
temp1->prev=var;

last=head;
}

void insertAkhir()
{
struct node *var,*temp;
var=(struct node *)malloc(sizeof(struct node));
var->data=5;

last=head;
while(last!=NULL)
{
temp=last;
last=last->next;
}
last=var;
temp->next=last;
last->prev=temp;
last->next=NULL;
}

int main()
{
int pilih=0;
head=NULL;

struct node *temp;
struct node *var = (struct node *)malloc(sizeof(struct node));
var->data=1;
head=var;
head->prev=NULL;
head->next=NULL;
last=head;

var=(struct node *)malloc(sizeof(struct node));
var->data=3;
last=head;

temp=last;
last=var;
temp->next=last;
last->prev=temp;
last->next=NULL;

do{
printf(“Albert Hendry Harsono/1701296914/StrukDat-32PPT\n\n”);
printf(“Data yang ada di linked list: “);
display();
printf(“\n\n”);

printf(“Menu: \n”);
printf(“1. Tampilkan memasukkan 0 di awal list\n”);
printf(“2. Tampilkan memasukkan 2 di antara 1 dan 3\n”);
printf(“3. Tampilkan memasukkan 5 di akhir list\n”);
printf(“4. Exit\n”);

do{
printf(“Pilihan: “);
scanf(“%d”,&pilih);fflush(stdin);
}while(pilih>4 || pilih<1);

if(pilih==1)
{
insertAwal();
printf(“\n\n”);
}

else if(pilih==2)
{
insertTengah();
printf(“\n\n”);
}

else if(pilih==3)
{
insertAkhir();
printf(“\n\n”);
}
system(“cls”);
}while(pilih!=4);

getchar();
}

This entry was posted in Struktur Data, Uncategorized. Bookmark the permalink.

Leave a Reply

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