Tugas Struktur Data (32PPT) – Linked List Insertion

Tugas Tutorial Class 32PPT
Input angka (maks. 10) & Output angka dengan Linked list

Albert Hendry Harsono (1701296914)

#include<stdio.h>
#include<stdlib.h>

struct node
{
int x;
struct node *next;
};

int main()
{
int banyak=0;
int j=0;
struct node *root;
struct node *conductor;

do{
printf(“Masukkan banyaknya angka yang ingin diinput [1..10]: “);
scanf(“%d”,&banyak);fflush(stdin);
}while(banyak>10 || banyak<1);

//awal
root=(struct node*)malloc(sizeof(struct node));
root->next=0;
printf(“Input ke-1: “);
scanf(“%d”,&root->x);fflush(stdin);

conductor=root;
conductor->next=(struct node*)malloc(sizeof(struct node));
conductor=conductor->next;

if(conductor==0)
{

}

if(banyak==1){
printf(“Output: %d “,root->x);
}

if(banyak==2){
//akhir
conductor->next=0;
printf(“Input ke-2: “);
scanf(“%d”,&conductor->x);fflush(stdin);

conductor=root;

printf(“Output: “);

if(conductor!=0){
while(conductor->next!=0)
{
printf(” %d “,conductor->x);
conductor=conductor->next;
}
printf(“-> %d”,conductor->x);
}
}

if(banyak>2){
//tengah
for(int i=2;i<banyak;i++){
conductor->next=0;
printf(“Input ke-%d: “,i);
scanf(“%d”,&conductor->x);fflush(stdin);

conductor->next=(struct node*)malloc(sizeof(struct node));
conductor=conductor->next;

if(conductor==0)
{
return 0;
}

j=i;
}

//akhir
conductor->next=0;
printf(“Input ke-%d: “,j+1);
scanf(“%d”,&conductor->x);fflush(stdin);

conductor=root;

printf(“Output: “);

if(conductor!=0){
while(conductor->next!=0)
{
printf(“%d -> “,conductor->x);
conductor=conductor->next;
}
printf(“%d “,conductor->x);
}
}

printf(“\n”);
getchar();
}

This entry was posted in Struktur Data, Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *