Single Linked List: //pilihan

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Single Linked List

#include <stdio.h>
#include <malloc.h>
typedef struct nod
{ int info;
struct nod *next;}NOD, *ptrNod;
ptrNod nodbaru(int d) ;
int main()
{ ptrNod head, baru, tampil, bantu, tail, prev;
head = NULL;
head = nodbaru(1);
tail = nodbaru(2);
head -> next = tail;
//pilihan
int x, y, z, loop;
int input;
for (loop=1; loop>0; loop++){
printf("\n-- Menu Selection --\n");
printf("0) Quit\n");
printf("1) tambah depan\n");
printf("2) tambah belakang\n");
printf("3) sisip tengah\n");
printf("pilihan anda : ");
scanf("%d", &input);
if(input == 1){
//insert di awal
printf("sisipkan depan : ");
scanf("%d", &x);
ptrNod depan;
depan = nodbaru(x);
depan -> next = head;
head = depan;}
else if(input == 2){
//insert di akhir
printf("sisipkan belakang : ");
scanf("%d", &y);
ptrNod belakang;
belakang = nodbaru(y);

Nama : Asep Iqbal Arifin


NIM : 1127070013
Kelas : Teknik Elektro 3A

tail -> next = belakang;


tail = belakang;}
else if(input == 3){
//insert diantara 2 node
printf("sisip tengah :");
scanf("%d", &z);
ptrNod cari, bantu, insert;
insert = nodbaru(z);
cari = head;
while(cari -> info < insert -> info && cari -> next != NULL)
{prev = cari;
cari = cari -> next;}
if (cari -> info > insert -> info)
{insert -> next = cari;
prev -> next = insert;}
else {cari -> next = insert;} }
//close
else if(input == 0) {printf("press any key to close");break;}
else {printf ("angka tidak ada\n") ;continue;}
//menampilkan linked list
{tampil=head;
while (tampil -> next != NULL)
{ printf("%d\t", tampil -> info);
tampil = tampil -> next;}
if (tampil -> next == NULL)
{ printf("%d\n\n", tampil -> info);}}}
getch();
}
ptrNod nodbaru(int d)
{
ptrNod baru;
baru = (ptrNod) malloc (sizeof(NOD));
if (baru != NULL){
baru -> info = d;
baru -> next = NULL;
return baru;}
}

Double Linked List


#include <stdio.h>
#include <malloc.h>
typedef struct nod
{ int info;
struct nod *kiri;
struct nod *kanan;}NOD, *ptrNod;
ptrNod nodbaru(int d) ;
int main()
{
ptrNod head, bantu, tampil, tail;
head = NULL;
bantu = NULL;
bantu = nodbaru(1);
head = bantu;
tail = nodbaru(2);
head -> kanan = tail;
head -> kiri = NULL;
tail -> kiri = head;
//pilihan
int x, y, z, loop;
int input;
for (loop=1; loop>0; loop++){
printf("\n-- Menu Selection --\n");
printf("0) Quit\n");
printf("1) tambah depan\n");
printf("2) tambah belakang\n");
printf("3) sisip tengah\n");
printf("pilihan anda : ");
scanf("%d", &input);
if(input == 1){
//insert di awal
printf("sisipkan depan : ");
scanf("%d", &x);
ptrNod front;
front = nodbaru(x);
head -> kiri = front;
front -> kanan = head;
head = front;}

else if(input == 2){


//insert di akhir
printf("sisipkan belakang : ");
scanf("%d", &y);
ptrNod back;
back = nodbaru(y);
tail -> kanan = back;
back -> kiri = tail;
tail = back;}
else if(input == 3){
//insert diantara 2 node
printf("sisip tengah :");
scanf("%d", &z);
ptrNod insert, cari, prev;
insert = nodbaru(z);
cari = head;
while(cari -> info < insert -> info && cari -> kanan != NULL)
{prev = cari;
cari = cari -> kanan;}
if (cari -> info > insert -> info)
{insert -> kanan = cari;
prev -> kanan = insert;}
else {cari -> kanan = insert;}}
//close
else if(input == 0) {printf("press any key to close");break;}
else {printf ("angka tidak ada\n") ;continue;}
//menampilkan linked list
{tampil=head;
while (tampil -> kanan != NULL)
{
printf("%d\t", tampil -> info);
tampil = tampil -> kanan;}
if (tampil -> kanan == NULL)
{
printf("%d\n\n", tampil -> info);}}}
getch();
}

ptrNod nodbaru(int d)
{
ptrNod baru;
baru = (ptrNod) malloc (sizeof(NOD));
if (baru != NULL){
baru -> info = d;
baru -> kiri = NULL;
baru -> kanan = NULL;
return baru;}
}

You might also like