Advertisement
mydiaz

6 - SLL Insert Akhir

Mar 11th, 2022
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct simpul Node;
  5. struct simpul{
  6.     int data;
  7.     Node *next;
  8. };
  9. Node *head = NULL, *p;
  10. void alokasi();
  11. void akhir();
  12. void tampil();
  13.  
  14. int main(){
  15.     char jwb;
  16.     puts("Single Link List - Insert Akhir");
  17.     do{
  18.         fflush(stdin);
  19.         alokasi();
  20.         akhir();
  21.         fflush(stdin);
  22.         printf("Lagi? (y/t) ");
  23.         jwb = getchar();
  24.     }while(jwb == 'Y'||jwb == 'y');
  25.     tampil();
  26.     return 0;
  27. }
  28. void tampil(){
  29.     Node *baca;
  30.     printf("\n\nData yg ada dlm SLL\n");
  31.     baca = head;
  32.     while(baca != NULL){
  33.         printf("%d\n", baca->data);
  34.         baca = baca->next;
  35.     }
  36. }
  37. void alokasi(){
  38.     int x;
  39.     printf("Nilai yang mau disimpan : ");
  40.     scanf("%d", &x);
  41.     p = (Node *) malloc(sizeof(Node));
  42.     if(p==NULL){
  43.         puts("Alokasi Gagal");
  44.         exit(0);
  45.     }else{
  46.         p->data = x;
  47.         p->next = NULL;
  48.     }
  49. }
  50. void akhir(){
  51.     Node *tail;
  52.     if(head==NULL){
  53.         head = p;
  54.     }else{
  55.         tail = head;
  56.         while(tail->next != NULL)
  57.             tail = tail->next;
  58.         tail->next = p;
  59.         tail = tail->next;
  60.  
  61.  
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement