Mar 20

#include stdio.h
#include stdlib.h

struct node
{
int angka;
struct node *next, *prev;
}*head, *tail, *curr, *temp;

void pushDepan(int n)
{
curr=(struct node*)malloc(sizeof(struct node));
curr->angka=n;
if(head==NULL)
{
head=tail=curr;
}
else
{
head->prev=curr;
curr->next=head;
head=curr;// masih ragu
}
tail->next = NULL;
head->prev = NULL;
}

void pushBelakang(int n)
{
curr=(struct node*)malloc (sizeof(struct node));
curr->angka=n;
if(head==NULL)
{
head=tail=NULL;
}
else{
tail->next=curr;
curr->prev=tail;
tail=curr;
}
tail->next=head->prev=NULL;
}

void pushTengah(int n,int position){
curr=head;
if(head!=NULL && position>0){
for(int i=0;inext;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->angka=n;
if(position==1)
{
pushDepan(n);
}
else if(curr==NULL)
{
pushBelakang(n);
}
else{
temp->next=curr;
temp->prev=curr->prev;
curr->prev->next=temp;
curr->prev=temp;
}
}
}

void printAll(){
if(head==NULL)
{
printf(“Empty!\n”);
}
else{
curr=head;
while(curr!=NULL){
printf(” %d”,curr->angka);
curr=curr->next;
}
}
printf(“\n\n”);
}

void menu()
{
int pilihan,num,posisi;
do
{
printf(“1. Masukkan di depan\n2. Masukkan di tengah\n3. Masukkan di belakang\n4. Keluar”);
printf(“\nInput : “);
scanf(“%d”, &pilihan); fflush(stdin);
}while(pilihan4);

switch (pilihan)
{
case 1 : printf(“Input num : “);
scanf(“%d”, &num);
fflush(stdin);pushDepan(num);break;
case 2 :
printf(“Input Num : “);
scanf(“%d”, &num);
fflush(stdin);
printf(“Posisi : “);
scanf(“%d”, &posisi); fflush(stdin);
pushTengah(num, posisi);break;
case 3 : printf(“Input Num : “);
scanf(“%d”, &num); fflush(stdin);
pushBelakang(num);break;
case 4 : exit(1);break;
}
printAll();
}

void main()
{
pushDepan(1);
pushBelakang(3);
printAll();
menu();
getchar();
}

Leave a Reply