View Single Post
  #1  
Old 20th November 2011
Guesuper Guesuper is offline
Ceriwiser
 
Join Date: Nov 2011
Posts: 408
Rep Power: 14
Guesuper mempunyai hidup yang Normal
Default [TANYA]yg bisa bhs C "single linked list" tolongin ane dong?

nih gan=



//STACK USING SINGLE LINKED LIST

#include

#include



struct data

{

int angka;

struct data *next;

}*head,*curr,*tail;



void push(int angka)

{

curr=(struct data*)malloc(sizeof(struct data));

curr->angka=angka;



if(head==NULL)

{

head=tail=curr;

}

else

{

tail->next=curr;

tail=curr;

}

tail->next=NULL;



printf("Push berhasil.");

getchar();

}



void pop()

{

curr=head;

if(head==NULL)

{

printf("Data masih kosong");

}

else

{

if(head==tail)

{

free(head);

head=NULL;

}

else

{

head=head->next;

free(curr);

}

}

}



void cetak()

{

curr=head;



while(curr)

{

printf("%d",curr->angka);

curr=curr->next;

}

}



void main()

{

int choose,angka;



do{

system("cls");

cetak();



printf("\n\nMenu:\n");

printf("1. Push.\n");

printf("2. Pop.\n");

printf("3. Exit.\n");

printf("Pilih(1-3): ");

do{

scanf("%d",&choose);

fflush(stdin);

}while(choose3);



switch (choose)

{

case 1:

printf("\nAngka berapa yang ingin dipush: ");

scanf("%d",&angka);

fflush(stdin);

push(angka);

break;

case 2:

pop();

break;

}

}while(choose!=3);



printf("\n\n\nTERIMAKASIH\n");

printf("%c%c%c%c%c%c%c%c%c%c%c",219,219,219,219,21 9,219,219,219,219,219);



getchar();

}



pertanyaan ane, kenapa yang ane bold harus ada? padahal kan udah di free, kenapa perlu di NULLin. kan datanya cuma satu.



(ini bukan tugas, sekedar ingin tau saja)



Reply With Quote