pada postingan saya kali ini saya akan berbagi mengenai kodingan list linear lengkap dengan bahasa C
#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
void awal();
void tengah();
void akhir();
void menu();
void cetak();
void delawal();
void deltengah();
void delakhir();
void alokasi();
typedef struct simpul node;
struct simpul{
int data;
node *next,*head,*after;
};
node *p,*head,*after,*before,*prevbefore,*tail;
int x,sisip;
main(){
char pil;
do{system("cls");
menu();
printf("kembalike menu>");
fflush(stdin);
pil=getchar();
}while(pil=='y'||pil=='Y');
}
void menu()
{ char pil;
printf("masukanpilihananda> \n");
puts("1. insert first");
puts("2. insert after");
puts("3. insert last");
puts("4. delete first");
puts("5. delete last");
puts("6. delete after");
puts("7. cetak");
puts("8. exit");
printf("masukanpilahananda>");
fflush(stdin);
scanf("%c",&pil);
switch (pil)
{
case '1': {
awal();
}
break;
case '2': {
tengah();
}
break;
case '3': {
akhir();
}
break;
case '4': {
delawal();
}
break;
case '5': {
delakhir();
}
break;
case '6': {
deltengah();
}
break;
case '7': {
cetak();
}
break;
}
}
void alokasi(){
printf("nilai=");
scanf("%d",&x);
p=(node *)malloc(sizeof(node));
p->data=x;
p->next=NULL;
}
void awal(){
char pil;
puts("masukan data");
do{ alokasi();
p->next=head;
head=p;
fflush(stdin);
printf("input lagi=");
pil=getchar();
}while(pil=='y'||pil=='Y');
}
void tengah()
{int cari;
alokasi();
fflush(stdin);
printf("data maudisisipkandimana = ");
scanf("%d",&cari);
after=head;
while(after->data!=cari)
{ if(after->next==NULL)
{printf("data takketemu");
exit(1);}
else
after=after->next;
}
p->next=after->next;
after->next=p;
}
void akhir()
{ char jawab;
do{
alokasi();
if(head == NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = tail->next;
}
fflush(stdin);
printf("masihada data lagi = ");
jawab = getchar();
}while(jawab=='y'||jawab=='Y');
}
void delawal(){
node *hapus;
hapus = head;
head= hapus->next;
free(hapus);
}
void delakhir(){
node *hapus;
hapus=tail->next;
free(hapus);
head=head->next;
}
void deltengah(){
int cari;
node *hapus;
printf("mau di hapus setelah angka berapa?");scanf("%d",&cari);
after=head;
while(after->data!=cari){
if(after->next==NULL){
printf("data takketemu");
exit(1);
}
else{
after=after->next;
}
hapus=after->next;
after->next=(after->next)->next;
free(hapus);
}
}
void cetak()
{ node *baca;
baca=head;
while(baca!=NULL)
{
printf("hasil = %d \n",baca->data);
baca=baca->next;
}
}
#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
void awal();
void tengah();
void akhir();
void menu();
void cetak();
void delawal();
void deltengah();
void delakhir();
void alokasi();
typedef struct simpul node;
struct simpul{
int data;
node *next,*head,*after;
};
node *p,*head,*after,*before,*prevbefore,*tail;
int x,sisip;
main(){
char pil;
do{system("cls");
menu();
printf("kembalike menu>");
fflush(stdin);
pil=getchar();
}while(pil=='y'||pil=='Y');
}
void menu()
{ char pil;
printf("masukanpilihananda> \n");
puts("1. insert first");
puts("2. insert after");
puts("3. insert last");
puts("4. delete first");
puts("5. delete last");
puts("6. delete after");
puts("7. cetak");
puts("8. exit");
printf("masukanpilahananda>");
fflush(stdin);
scanf("%c",&pil);
switch (pil)
{
case '1': {
awal();
}
break;
case '2': {
tengah();
}
break;
case '3': {
akhir();
}
break;
case '4': {
delawal();
}
break;
case '5': {
delakhir();
}
break;
case '6': {
deltengah();
}
break;
case '7': {
cetak();
}
break;
}
}
void alokasi(){
printf("nilai=");
scanf("%d",&x);
p=(node *)malloc(sizeof(node));
p->data=x;
p->next=NULL;
}
void awal(){
char pil;
puts("masukan data");
do{ alokasi();
p->next=head;
head=p;
fflush(stdin);
printf("input lagi=");
pil=getchar();
}while(pil=='y'||pil=='Y');
}
void tengah()
{int cari;
alokasi();
fflush(stdin);
printf("data maudisisipkandimana = ");
scanf("%d",&cari);
after=head;
while(after->data!=cari)
{ if(after->next==NULL)
{printf("data takketemu");
exit(1);}
else
after=after->next;
}
p->next=after->next;
after->next=p;
}
void akhir()
{ char jawab;
do{
alokasi();
if(head == NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = tail->next;
}
fflush(stdin);
printf("masihada data lagi = ");
jawab = getchar();
}while(jawab=='y'||jawab=='Y');
}
void delawal(){
node *hapus;
hapus = head;
head= hapus->next;
free(hapus);
}
void delakhir(){
node *hapus;
hapus=tail->next;
free(hapus);
head=head->next;
}
void deltengah(){
int cari;
node *hapus;
printf("mau di hapus setelah angka berapa?");scanf("%d",&cari);
after=head;
while(after->data!=cari){
if(after->next==NULL){
printf("data takketemu");
exit(1);
}
else{
after=after->next;
}
hapus=after->next;
after->next=(after->next)->next;
free(hapus);
}
}
void cetak()
{ node *baca;
baca=head;
while(baca!=NULL)
{
printf("hasil = %d \n",baca->data);
baca=baca->next;
}
}
No comments