You are on page 1of 6

#include <stdio.

h> }
#include<math.h> else{ //ganjil
void cetakArray(int a[], int n){ k= a[n/2];
int i; }
for(i=0; i<n; i++){ return k;
printf("%i ", a[i]); }
}
} int main()
{
void isiArray(int a[], int n){ int n = 10;
int i; int i,a[n];
for(i=0; i<n; i++){
a[i] = i+1; isiArray(a, 10);
}
} cetakArray(a, 10);

int min(int a[], int n){ printf("Nilai Maximal :


int k = a[0]; %i\n", max(a, n));
for(int i=1; i<n; i++){ printf("Nilai Minimal :
if (a[i]<k) %i\n", min(a, n));
k=a[i];
} printf("%f\n",mean(a,n));
return k; printf("%f\n",stdDev(a,n));
} printf("%f\n",median(a,n));
return 0;
int max(int a[], int n){ }
int k = a[0];
for(int i=1; i<n; i++){
if (a[i]>k) Ini yang kedua
k= a[i]; #include <stdio.h>
} #include <stdlib.h>
return k; #include <string.h>
}
typedef struct node{
double mean(int a[], int n){ char name[30];
int i; char NIM[10];
double m=0; int age;
for (i=0; i<n; i++){ struct node *next;
m+=a[i]; }Node;
}
return m/n; void createLL(Node **head, char name[],
} char NIM[], int age);
void printLL(Node **head);
double kuadrat(double x){ void sortLL(Node **head, int lim);
return x*x; int datacount(Node **head);
} void changeint(int *a, int *b);
void changestr(char str1[], char str2[])
double stdDev(int a[], int n){ ;
double rt=mean(a,n); void searchLLname(Node **head, char str[
int i; ]);
double sigma=0; int strcompare(char str1[], char str2[])
for(i=0;i<n;i++){ ;
sigma=sigma+kuadrat(a[i]-rt); int main()
} {
return sqrt(sigma/n); int lim;
} Node *head1 = NULL;
char name1[30] = "Arif Ikhwan Hendra";
double median(int a[], int n){ char NIM1[10] = "221810193";
float k; int age1 = 19;
if (n%2==0){ //genap createLL(&head1, name1, NIM1, age1);
k= (a[n/2] + a[(n/2)-1])/2.0;
//printf("%s\n", head1->name); if (*head == NULL)
//printf("%s\n", head1->NIM); {
//printf("%i\n", head1->age); *head = newnode;
strcpy(newnode->name, name);
printf("\n"); strcpy(newnode->NIM, NIM);
char name2[30] = "lol lol"; newnode->age = age;
char NIM2[10] = "221810942"; newnode->next = NULL;
int age2 = 15; }
createLL(&head1, name2, NIM2, age2); else
{
char name3[30] = "opo opo"; while (temp->next != NULL)
char NIM3[10] = "221810292"; {temp = temp->next;}
int age3 = 20; temp->next = newnode;
createLL(&head1, name3, NIM3, age3); strcpy(newnode->name, name);
strcpy(newnode->NIM, NIM);
// newnode->age = age;
lim = datacount(&head1); newnode->next = NULL;
sortLL(&head1, lim); }
// }

printLL(&head1); void printLL(Node **head)


searchLLname(&head1, "Arif Ikhwan {
Hendra"); Node *temp;
temp = *head;
int i = 1;
//char tempstr[50]; while (temp->next != NULL)
//strcpy(tempstr, name1); {
//strcpy(name1, name2); printf("Name[%i] ::
//strcpy(name2, tempstr); %s\n", i, temp->name);
printf("NIM [%i] ::
//changestr(name2,name1); %s\n", i, temp->NIM);
printf("Age [%i] ::
printf("\ndatas::%i\n", datacount(&head1 %i\n", i, temp->age);
)); temp = temp->next;
//printf("name1 :: %s\n", name1); i += 1;
printf("\nresn1n2:: }
%i\n", strcompare(name1, name2)); if (temp->next == NULL)
return 0; {
} printf("Name[%i] ::
%s\n", i, temp->name);
void changeint(int *a, int *b) printf("NIM [%i] ::
{ %s\n", i, temp->NIM);
int c = *a; printf("Age [%i] ::
*a = *b; %i\n", i, temp->age);
*b = c; }
} }

void changestr(char str1[], char str2[]) int datacount(Node **head)


{ {
char tempstr[50]; Node *temp;
strcpy(tempstr, str1); temp = *head;
strcpy(str1, str2); int i = 0;
strcpy(str2, tempstr); while (temp->next != NULL)
} {
i += 1;
void createLL(Node **head, char name[], temp = temp->next;
char NIM[], int age) }
{ i += 1;
Node *newnode = malloc(sizeof(Node)); return i;
Node * temp; }
temp = *head;
void sortLL(Node **head, int lim) {
{ printf("\n%s exist in
int i = 0; this list\n", str);
Node *temp; return;
Node *p; }
p = *head; temp = temp->next;
//printf("\n\n%i\n\n", lim); }
while (i < lim) if (temp->next == NULL)
{ {
temp = *head; if (strcompare(str, temp-
while (temp->next != NULL) >name) == 1)
{ {
if (p->age > temp->age) printf("\n%s exist in
{ this list\n", str);
changeint(&(p- return;
>age), &(temp->age)); }
changestr(p- else
>name , temp->name); {
changestr(p- printf("\n%s doesnt
>NIM , temp->NIM); exist in this
} list\n", str);
temp = temp->next; return;
} }
if (p->age > temp->age) }
{ }
changeint(&(p-
>age), &(temp->age));
changestr(p- Ini yang ketiga
>name , temp->name); #include <stdio.h>
changestr(p- #include <stdlib.h>
>NIM , temp->NIM); #include <string.h>
}
p = p->next; typedef struct {
i += 1; char nim[10];
} char nama[50];
} int umur;
} Mhs;
int strcompare(char str1[], char str2[])
{ typedef struct node {
int lim1 = strlen(str1); Mhs data;
//int lim2 = strlen(str2); struct node *next;
int c = 0; } Node;
for (int i = 0; i < lim1; i += 1)
{ void cetakList(Node *head){
if (str1[i] == str2[i]) //cetak list
c += 1; Node *tmp = head;
} if(head == NULL){
if (c == lim1) printf("List Kosong");
return 1; } else {
else while(tmp != NULL){
return 0; printf("Nim: %s \n",
} tmp->data.nim); //item
ke-1 s.d. terakhir
void searchLLname(Node **head, char str[ printf("Nama: %s \n",
]) tmp->data.nama);
{ printf("Umur: %i \n",
Node *temp = *head; tmp->data.umur);
while (temp->next != NULL) printf("---------------
{ --------- \n");
if (strcompare(str, temp- tmp = tmp->next;
>name) == 1) }
}
} void changestr(char str1[], char str2[])
{
void insertNode(Node **head, Mhs char tempstr[50];
dataBaru){ strcpy(tempstr, str1);
Node *n1, *tmp; strcpy(str1, str2);
n1 = malloc(sizeof(Node)); strcpy(str2, tempstr);
n1->data = dataBaru; }
n1->next = NULL;
void sortLL(Node **head)
if(*head == NULL){ {
*head = n1; int i = 0, lim;
} else { Node *temp;
tmp = *head; Node *p;
while(tmp->next != p = *head;
NULL){ temp = *head;
tmp = tmp->next; while (temp->next != NULL) {
} i += 1;
tmp->next = n1; temp = temp->next;
} }
} i += 1;
lim = i;
void deleteNode(Node **head, char i = 0;
nim[]){
Node *tmp, *tmp2; while (i < lim)
tmp = *head; {
if(*head == NULL){ //node kosong temp = *head;
printf("List while (temp->next !=
kosong\n"); NULL)
} else if(strcmp(tmp->data.nim, {
nim) == 0){//item yg dicari if (p->age > temp->age)
adalah item no 1 {
*head = tmp->next; changeint(&(p->age),
free(tmp); &(temp->age));
} else { changestr(p->name ,
while(strcmp(tmp->next- temp->name);
>data.nim, nim) != 0 && changestr(p->NIM ,
tmp->next->next != temp->NIM);
NULL){ }
tmp = tmp->next; temp = temp->next;
} }
if(strcmp(tmp->next- if (p->age > temp->age)
>data.nim, nim) == 0){ {
//ketemu changeint(&(p->age),
tmp2 = tmp->next; &(temp->age));
tmp->next = tmp2->next; changestr(p->name ,
free(tmp2); temp->name);
} else { //tidak ketemu changestr(p->NIM ,
printf("Tidak temp->NIM);
ketemu\n"); }
} p = p->next;
i += 1;
} }
} }

void changeint(int *a, int *b)


{
int c = *a; int main()
*a = *b; {
*b = c; Node *head;
} head = NULL;
mhsw *current;
Mhs m1; current=(mhsw*)malloc(sizeof(mhsw));
strcpy(m1.nim, "001"); current=ada;
strcpy(m1.nama, "Andi"); while (current!=NULL){
m1.umur = 19; printf("%s\n",(*current).nama);
printf("%d\n",(*current).umur);
insertNode(&head, m1); current=(*current).next;
}
Mhs m2; }
strcpy(m2.nim, "002");
strcpy(m2.nama, "Budi"); void hapus(mhsw **ada){
m2.umur = 15; mhsw *current;
current=*ada;
insertNode(&head, m2); *ada=(*current).next;
delete current;
Mhs m3; }
strcpy(m3.nim, "003");
strcpy(m3.nama, "Charlie"); void cari(mhsw *ada){
m3.umur = 20; int u;
mhsw *current;
insertNode(&head, m3); current=ada;
scanf("%d",&u);
//deleteNode(&head, m2.nama); while (current!=NULL){
if ((*current).umur=u){
cetakList(head); printf("%s",(*current).nama)
;
break;
return 0; } else {
} current=(*current).next;
}
Ini yang keempat woy }
#include <stdio.h> if ((*current).umur==u){
#include <stdlib.h> printf("data tidak ditemukan");
#include <string.h> }
}
struct mhs{
int umur; void bubblesort(mhsw **ada){
char nama[50]; mhsw *current;
struct mhs *next; current=*ada;
}; int tertukar,temp1;
char temp2[50];
typedef struct mhs mhsw; mhsw *cek = NULL;
if (*ada == NULL)
void isi(mhsw **ada){ return;
int x; do{
char s[50]; tertukar=0;
mhsw *current; current=*ada;
current=(mhsw*)malloc(sizeof(mhsw)); while((*current).next != cek) {
scanf("%s",(*current).nama); if (((*current).umur) >
scanf("%d",&x); ((*current).next->umur)){
(*current).umur=x; temp1=(*current).umur;
if (*ada==NULL) { strcpy(temp2,
*ada=current; (*current).nama);
(**ada).next=NULL; (*current).umur=(*curren
} else { t).next->umur;
(*current).next=*ada; strcpy((*current).nama,
*ada=current; (*current).next->nama);
} (*current).next-
} >umur=temp1;
strcpy((*current).next-
void tampil(mhsw *ada){ >nama, temp2);
tertukar = 1;
}
current=current->next;
}
cek=current;
}
while (tertukar);
}

int main(){
int n;
mhsw *head=NULL;
while (1){
scanf("%d",&n);
switch (n){
case 1: isi(&head); break;
case 2: tampil(head); break;
case 3: hapus(&head); break;
case 4: cari(head); break;
case 5: bubblesort(&head);
break;
}
}
return 0;
}

You might also like