Rabu, 09 April 2014

Code Double Linked List *update*

Pada postingan kali ini coding linked list ini sudah lebih baik dikarenakan cara penghapusan yang lebih fleksibel(by user input(by name)) dan memasukkan data nya juga fleksibel, bahkan ada push mid nya juga lho..
So, check this code out :

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

//DLL
struct Grocery{
char vegetableName[25];
int qty;
struct Grocery *next,*prev;
}*tail, *head;

int jumlahData=0;

void push(char vegetableName[], int qty, int pos){
struct Grocery *curr = (struct Grocery*)malloc(sizeof(struct Grocery));
strcpy(curr->vegetableName, vegetableName);
curr->qty = qty;
if(head == NULL){
head = tail = curr;
tail->next = NULL;
head->prev = NULL;
}else{
if(pos == 1){
//push depan
curr->next = head;
head->prev = curr;
head = curr;
head->prev = NULL;
}else if(pos == jumlahData+1){
//push belakang
tail->next = curr;
curr->prev = tail;
tail = curr;
tail->next = NULL;
}else{
//push mid
struct Grocery *temp = head;
int i=1;
while(temp!=tail && i!= pos-1){
temp = temp->next;
i++;
}
curr->next = temp->next;
temp->next->prev = curr;
temp->next = curr;
curr->prev = temp;
}
}
}

void pop(char vegetableName[]){
if(head == NULL){
printf("\nSorry no vegetable at store");
getchar();
}else{
if(strcmp(head->vegetableName,vegetableName)==0){
//pop head
if(head == tail){
//data 1
free(head);
head = tail = NULL;
}else{
//data >1
head = head->next;
free(head->prev);
head->prev = NULL;
}
}else if(strcmp(tail->vegetableName,vegetableName)==0){
//pop tail //code optimization
//if(head == tail){
// //data 1
// free(head);
// head = tail = NULL;
//}else{
//data > 1
tail = tail->prev;
free(tail->next);
tail->next = NULL;
//}
}else{
//pop mid
struct Grocery *temp = head;
int isFound = 0;
while(temp != NULL){
if(strcmp(vegetableName,temp->vegetableName) == 0){
isFound = 1;
break;
}
temp = temp->next;
}
if(isFound == 1){
//kl ktmu, qta hapus
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
free(temp);
}else{
printf("\nSorry, you vegetable is eaten by Zombie..\n");
getchar();
}
}
}
}

void popALL(){
struct Grocery *temp;
while(head!=NULL){
temp = head;
head=head->next;
free(temp);
}
head = tail = NULL;
}

void line(){
int i;
for(i=0;i<27;i++)
{
printf("-");
}
printf("\n");
}

void cetak(){
struct Grocery *temp = head;
line();
printf("|%-15s|%-9s|\n","Vegetable Name","Quantity");
while(temp){
line();
printf("|%-15s|%-9d|\n",temp->vegetableName,temp->qty);
temp = temp->next;
}
line();
}

void menu(){
printf("Happy Groceria\n");
printf("1. Add Stock\n");
printf("2. Buying\n");
printf("3. Exit\n");
printf("Input choice [1..3]: ");
}

void add(){
char vegeName[25];

do{
printf("Input Vegetable Name [3..24]: ");
gets(vegeName);
}while(strlen(vegeName) < 3 || strlen(vegeName) > 24 );

srand(time(NULL));
int qty;
qty = rand()%10+20;

int pos;
do{
printf("Input Position [1..%d]: ",jumlahData+1);
scanf("%d",&pos);
fflush(stdin);
}while(pos < 1 || pos > jumlahData+1);

push(vegeName,qty,pos);
jumlahData++;
}

void buying(){
char vegeName[25];
do{
printf("Input Vegename [3..24]: ");
gets(vegeName);
}while(strlen(vegeName) < 3 || strlen(vegeName) > 24);
pop(vegeName);
}

void clear(){
int i;
for(i=0;i<25;i++){
printf("\n");
}
}

int main(){
int switches;
do{
clear();
if(jumlahData != 0){
cetak();
}
menu();
scanf("%d",&switches);
fflush(stdin);
switch(switches){
case 1: add(); break;
case 2: buying(); break;
}
}while(switches != 3);
return 0;
}

NB : Bahan yang ditulis berdasarkan apa yang pernah saya pelajari dari dosen dan juga slide-slide powerpoint dari Binus University.


Binus University     Skyconnectiva

Tidak ada komentar:

Posting Komentar