Jumat, 21 Maret 2014

linked list part 3


link list
: singgle(next), double(next, prev), multi(left, right, parent, child);
implementasi
: stack(enstack(nambah), destack(ngurang)) , queue(front(depan) , rear(belakang)) ;

berikut saya berikan contoh kodingan program push dalam link list yang nantinya bisa menentukan pisisi pushnya secara manual:

int jumlahData=0

void push(char vegeName[], int qty, int posisi)
{
struct grocery *curr=(struct grocery*)malloc(sizeof(struct grocery));
strcpy(curr->vegeName,vegeName);
curr->qty=qty;

if(head==NULL)
{
head=tail=curr;
tail->next=NULL;
head->prev=NULL;
}
else
{
if(posisi==1)
{

curr->next=head;
head->prev=curr;
head=curr;
head->prev=NULL;
}
else if(posisi==jumlahData+1)
{
tail->next=curr;
curr->prev=tail;
tail=curr;
tail->next=NULL;
}
else
{
temp=head;
int i=1;
while(temp!=tail && i!=posisi-1)
{
temp=temp->next;
i++;
}
curr->next=temp->next;
temp->next->prev=curr;
temp->next=curr;
curr->prev=temp;
}
}
jumlahData++;
}



berikut adalah kodingan program yang inngin menghapus (pop) suatu spesifik data:


void pop(char vegeName[])
{
if(head==NULL)
{
printf("not found");
}
else
{
if(strcmpi(head->vegeName,vegeName)==0)
{
if(head==tail)
{
free(head);
head=tail=NULL;
}
else
{
head=head->next;
free(head->prev);
head->prev=NULL;
}
}
else if(strcmpi(tail->vegeName,vegeName)==0)
{

tail=tail->prev;
free(tail->next);
tail->next=NULL;
}
else
{
temp=head;
int isFound=0;
while(temp!=NULL )
{
if(strcmpi(vegeName,temp->vegeName)==0)
{
isFound=1;
break;
}
temp=temp->next;
}
if(isFound==1)
{
temp->next->prev=temp->prev;
temp->prev->next=temp->next;
free(temp);
}
else
{
printf("\nnot found ");
getch();
}
}
}
}





Tidak ada komentar:

Posting Komentar