buat inget inget pelajaran struktur data...
program buat nampilin jumlah elemen ganjil yang ada di dalem list..
/*file boolean.h*/
#ifndef BOOLEAN_H
#define BOOLEAN_H
#define true 1
#define false 0
#define boolean unsigned char
#endif
/*selesai */
/*file list.h */
#ifndef LIST_H
#define LIST_H
#include"boolean.h"
#define nil NULL
#define info(p) (*p).info
#define next(p) (p)->next
#define first(l) ((l).first)
#define infotype int
typedef struct telmlist *address;
typedef struct telmlist
{
infotype info;
address next;
}elmlist;
typedef struct
{
address first;
}list;
boolean listempty(list l);
void createlist(list *l);
address alokasi(infotype x);
void printinfo(list l);
void insertlast (list *l, address p);
void insvlast(list *l, infotype x);
void dealokasi (address p);
int jumganjil(list l);
void ganjil(list l);
#endif
/*finish for list.h/
/*file list.c*/
#include "list.h"
#include
#include
#include
boolean listempty(list l)
{
return (first(l)==nil);
}
void createlist(list *l)
{
first(*l)=nil;
}
address alokasi(infotype x)
{
address p;
p=(address)malloc(sizeof(telmlist));
if(p==nil)
{
return nil;
}
else
{
info(p)=x;
next(p)=nil;
return p;
}
}
void dealokasi (address p)
{
free(p);
}
void insvlast(list *l, infotype x)
{
address p;
p=alokasi(x);
if(first(*l)==nil)
{
first(*l)=p;
}
else
{
insertlast(l, p);
}
}
void printinfo(list l)
{
address a;
if(listempty(l))
{
printf("list kosong");
}
else
{
a=first(l);
while(a!=nil)
{
printf("%d ",info(a));
a=next(a);
}
printf("\n");
}
}
void ganjil(list l)
{
address p;
p=first(l);
while(p!=nil)
{
if((info(p)%2)!=0)
{
printf("%i ",info(p));
}
p=next(p);
}
}
int jumganjil(list l)
{
address p;
int a;
p=first(l);
a=0;
while(p!=nil)
{
if((info(p)%2)!=0)
{
a=a+1;
}
p=next(p);
}
return a;
}
void insertlast (list *l, address p)
{
address last;
last = first(*l);
while (next(last) != nil)
{
last = next(last);
}
next(last) = p;
next(p) = nil;
}
/*finish */
// file mlist.c
#include "list.c"
#include
#include
void main()
{
int i,q,k;
list l1;
createlist(&l1);
if(listempty(l1))
{
printf("Elemen list yang dibuat masih kosong");
}
else
{
printf("list tidak kosong");
}
printf("\n1. Silahkan memasukkan banyak elemen list= ");
scanf("%d",&i);
for (k=0;k lebihkecildari i;k++)
{
printf("\nmasukkan nilai elemen ke %d= ",(k+1));scanf("%d",&q);
insvlast(&l1,q);
}
printf("\nMenampilkan Elemen di Dalam List\n");
printinfo(l1);
printf("\nMenampilkan Elemen ganjil di Dalam List\n");
ganjil(l1);
printf("\nJumlah elemen ganjil yang ada pada list = %d \n\n",jumganjil(l1));
}
//finish
semoga bermanfaat readers :)
*nb : ada tulisan k lebih kecil daripada i itu dalam belum notasi lebih kecil yak
:( soalnya gag bisaaa di post klo pake tanda :)
0 komentar:
Posting Komentar