Hai semua.. dalam kesempatan kali ini saya akan memposting penggunaan struct yang versi ketiganya. melanjutkan dari versi sebelumnya. yuk disimak. jika ada yang mau ditanyakan, monggo komen aja :)
#include <stdio.h>
#include <stdlib.h>
struct complex{
int realp;
int imagp;
struct complex *next;
struct complex *prev;
};
int length(struct complex *head){
struct complex *bantu;
bantu=head;
int tamp=0;
while(bantu!=NULL){
tamp++;
bantu=bantu->next;
}
return tamp;
}
void push(struct complex **head_ref,int r,int i){
struct complex *tamp;
tamp=(struct complex*)malloc(sizeof(struct complex));
tamp->realp=r;
tamp->imagp=i;
if(*head_ref==NULL){
*head_ref=tamp;
tamp->next=NULL;
tamp->prev=NULL;
}else{
tamp->next=*head_ref;
tamp->prev=NULL;
*head_ref=tamp;
}
}
void insertafter(struct complex **head_ref,int loc,int r,int i){
struct complex *tamp,*b1,*b2;
int a=0,x;
tamp=(struct complex*)malloc(sizeof(struct complex));
tamp->realp=r;
tamp->imagp=i;
b1=*head_ref;
b2=*head_ref;
x=length(*head_ref);
if(x<loc || loc<=0){
printf("tidak ada node ke : %d",loc);
}else if(*head_ref==NULL){
*head_ref=tamp;
tamp->next=NULL;
tamp->prev=NULL;
}else{
while(a<loc){
b2=b1;
b1=b1->next;
a++;
}
b2->next=tamp;
tamp->next=b1;
tamp->prev=b2;
b1->prev=tamp;
}
}
void insertbefore(struct complex **head_ref,int loc,int r,int i){
struct complex *tamp,*b1,*b2;
int a=0,x;
tamp=(struct complex*)malloc(sizeof(struct complex));
tamp->realp=r;
tamp->imagp=i;
b1=*head_ref;
b2=*head_ref;
x=length(*head_ref);
if(x<loc || loc<=0){
printf("tidak ada node ke : %d",loc);
}else if(*head_ref==NULL){
*head_ref=tamp;
tamp->next=NULL;
tamp->prev=NULL;
}else{
while(a<loc-1){
b2=b1;
b1=b1->next;
a++;
}
b2->next=tamp;
tamp->next=b1;
tamp->prev=b2;
b1->prev=tamp;
}
}
int ismax(struct complex *head){ //ismax belum jadi -_-
struct complex *b1,*max;
int tamp=1;
b1=head;
max=head;
while(b1!=NULL){
if(b1->realp>max->realp){
max=b1;
}else if(b1->realp==max->realp){
if(b1->imagp>max->imagp){
max=b1;
}
}
b1=b1->next;
}
printf("max is : %d + %di",max->realp,max->imagp);
b1=head;
while(b1!=NULL){
if(b1->realp==max->realp || b1->imagp==max->imagp){
printf("\n di node ke %d",tamp);
}
b1=b1->next;
tamp++;
}
}
void append( struct complex **head_ref,int r,int i){
struct complex *tamp,*b1;
int a=0;
tamp=(struct complex*)malloc(sizeof(struct complex));
tamp->realp=r;
tamp->imagp=i;
b1=*head_ref;
if(*head_ref==NULL){
*head_ref=tamp;
tamp->next=NULL;
tamp->prev=NULL;
}else{
while(b1->next!=NULL){
b1=b1->next;
}
b1->next=tamp;
tamp->next=NULL;
tamp->prev=b1;
}
}
void del(struct complex *head,int loc){
struct complex *b1,*b2;
b1=head;
int a,x;
x=length(head);
if(x<loc || loc<=0){
printf("tidak ada node ke: %d",loc);
}else if(head==NULL){
printf("List kosong");
}else{
for(a=1;a<loc;a++){
b2=b1;
b1=b1->next;
}
b2->next=b1->next;
b2->next->prev=b2;
free(b1);
}
}
void deldepan(struct complex *head){
struct complex *b1;
b1=head;
int a;
if(head==NULL){
printf("List kosong");
}else{
head=b1->next;
head->prev=NULL;
free(b1);
}
}
void delbelakang(struct complex *head){
struct complex *b1,*b2;
b1=head;
int a;
if(head==NULL){
printf("List kosong");
}else{
while(b1->next!=NULL){
b2=b1;
b1=b1->next;
}
b2->next=NULL;
b1->prev=NULL;
free(b1);
}
}
void printdblist(struct complex *head){
struct complex *bantu;
bantu=head;
if(head==NULL){
printf("List kosong");
}else{
while(bantu!=NULL){
printf("|%d+%di|<->",bantu->realp,bantu->imagp);
bantu=bantu->next;
}
}
}
int main(){
struct complex *a,*b,*head;
a=(struct complex*)malloc(sizeof(struct complex));
b=(struct complex*)malloc(sizeof(struct complex));
head=a;
a->realp=44;
a->imagp=2;
b->realp=23;
b->imagp=10;
a->next=b;
b->next=NULL;
a->prev=NULL;
b->prev=a;
printdblist(head);
//push(&head,44,55); //jadi
printf("\n");
insertafter(&head,1,11,12);
//ismax(head); //jadi
printdblist(head);
printf("\n");
//del(head,-4); //jadi
printf("\n");
//append(&head,90,99); //jadi
//deldepan(head); //blm
//delbelakang(head); //jadi
insertbefore(&head,3,88,3);
printdblist(head);
return 0;
}
Keterangan: bagian fungsi-fungsi dalam main adalah contoh pemanggilannya
sekian dari saya.. terimakasih sudah berkunjung. semoga bermanfaat :)
#C
#include <stdio.h>
#include <stdlib.h>
struct complex{
int realp;
int imagp;
struct complex *next;
struct complex *prev;
};
int length(struct complex *head){
struct complex *bantu;
bantu=head;
int tamp=0;
while(bantu!=NULL){
tamp++;
bantu=bantu->next;
}
return tamp;
}
void push(struct complex **head_ref,int r,int i){
struct complex *tamp;
tamp=(struct complex*)malloc(sizeof(struct complex));
tamp->realp=r;
tamp->imagp=i;
if(*head_ref==NULL){
*head_ref=tamp;
tamp->next=NULL;
tamp->prev=NULL;
}else{
tamp->next=*head_ref;
tamp->prev=NULL;
*head_ref=tamp;
}
}
void insertafter(struct complex **head_ref,int loc,int r,int i){
struct complex *tamp,*b1,*b2;
int a=0,x;
tamp=(struct complex*)malloc(sizeof(struct complex));
tamp->realp=r;
tamp->imagp=i;
b1=*head_ref;
b2=*head_ref;
x=length(*head_ref);
if(x<loc || loc<=0){
printf("tidak ada node ke : %d",loc);
}else if(*head_ref==NULL){
*head_ref=tamp;
tamp->next=NULL;
tamp->prev=NULL;
}else{
while(a<loc){
b2=b1;
b1=b1->next;
a++;
}
b2->next=tamp;
tamp->next=b1;
tamp->prev=b2;
b1->prev=tamp;
}
}
void insertbefore(struct complex **head_ref,int loc,int r,int i){
struct complex *tamp,*b1,*b2;
int a=0,x;
tamp=(struct complex*)malloc(sizeof(struct complex));
tamp->realp=r;
tamp->imagp=i;
b1=*head_ref;
b2=*head_ref;
x=length(*head_ref);
if(x<loc || loc<=0){
printf("tidak ada node ke : %d",loc);
}else if(*head_ref==NULL){
*head_ref=tamp;
tamp->next=NULL;
tamp->prev=NULL;
}else{
while(a<loc-1){
b2=b1;
b1=b1->next;
a++;
}
b2->next=tamp;
tamp->next=b1;
tamp->prev=b2;
b1->prev=tamp;
}
}
int ismax(struct complex *head){ //ismax belum jadi -_-
struct complex *b1,*max;
int tamp=1;
b1=head;
max=head;
while(b1!=NULL){
if(b1->realp>max->realp){
max=b1;
}else if(b1->realp==max->realp){
if(b1->imagp>max->imagp){
max=b1;
}
}
b1=b1->next;
}
printf("max is : %d + %di",max->realp,max->imagp);
b1=head;
while(b1!=NULL){
if(b1->realp==max->realp || b1->imagp==max->imagp){
printf("\n di node ke %d",tamp);
}
b1=b1->next;
tamp++;
}
}
void append( struct complex **head_ref,int r,int i){
struct complex *tamp,*b1;
int a=0;
tamp=(struct complex*)malloc(sizeof(struct complex));
tamp->realp=r;
tamp->imagp=i;
b1=*head_ref;
if(*head_ref==NULL){
*head_ref=tamp;
tamp->next=NULL;
tamp->prev=NULL;
}else{
while(b1->next!=NULL){
b1=b1->next;
}
b1->next=tamp;
tamp->next=NULL;
tamp->prev=b1;
}
}
void del(struct complex *head,int loc){
struct complex *b1,*b2;
b1=head;
int a,x;
x=length(head);
if(x<loc || loc<=0){
printf("tidak ada node ke: %d",loc);
}else if(head==NULL){
printf("List kosong");
}else{
for(a=1;a<loc;a++){
b2=b1;
b1=b1->next;
}
b2->next=b1->next;
b2->next->prev=b2;
free(b1);
}
}
void deldepan(struct complex *head){
struct complex *b1;
b1=head;
int a;
if(head==NULL){
printf("List kosong");
}else{
head=b1->next;
head->prev=NULL;
free(b1);
}
}
void delbelakang(struct complex *head){
struct complex *b1,*b2;
b1=head;
int a;
if(head==NULL){
printf("List kosong");
}else{
while(b1->next!=NULL){
b2=b1;
b1=b1->next;
}
b2->next=NULL;
b1->prev=NULL;
free(b1);
}
}
void printdblist(struct complex *head){
struct complex *bantu;
bantu=head;
if(head==NULL){
printf("List kosong");
}else{
while(bantu!=NULL){
printf("|%d+%di|<->",bantu->realp,bantu->imagp);
bantu=bantu->next;
}
}
}
int main(){
struct complex *a,*b,*head;
a=(struct complex*)malloc(sizeof(struct complex));
b=(struct complex*)malloc(sizeof(struct complex));
head=a;
a->realp=44;
a->imagp=2;
b->realp=23;
b->imagp=10;
a->next=b;
b->next=NULL;
a->prev=NULL;
b->prev=a;
printdblist(head);
//push(&head,44,55); //jadi
printf("\n");
insertafter(&head,1,11,12);
//ismax(head); //jadi
printdblist(head);
printf("\n");
//del(head,-4); //jadi
printf("\n");
//append(&head,90,99); //jadi
//deldepan(head); //blm
//delbelakang(head); //jadi
insertbefore(&head,3,88,3);
printdblist(head);
return 0;
}
Keterangan: bagian fungsi-fungsi dalam main adalah contoh pemanggilannya
sekian dari saya.. terimakasih sudah berkunjung. semoga bermanfaat :)
#C
Komentar
Posting Komentar