Double Linked List Flashcards Preview

Data Structures > Double Linked List > Flashcards

Flashcards in Double Linked List Deck (8):
1

PushTengah

void pushTengah(char nama[],int usia){
struct Mahasiswa *curr = (struct Mahasiswa*)malloc(sizeof(struct Mahasiswa));
strcpy(curr->nama,nama);
curr->usia = usia;
if(head == NULL)
{
head = tail = curr;
tail->next = NULL;
head->prev = NULL;
}
else
{
if(head==tail)
{
//ini sama keq push belakang
tail->next = curr;
curr->prev = head;
tail = curr;
tail->next = NULL; //bisa dihilangkan kl ud pake smart push.
}else
{
struct Mahasiswa *temp;
for(temp=head;temp->next!=NULL;temp=temp->next){
if(strcmpi(temp->next->nama,nama)>0){
break;
}
}
temp->next->prev = curr;
curr->prev = temp;
curr->next = temp->next;
temp->next = curr;
}
}
}

2

popTengah

void popTengah(int usia)
{
struct Mahasiswa *curr;
if(head == NULL)
{
//Case-1: No data at all
printf("Tidak ada data\n");
getchar();
}else
{
if(head == tail){
//Case-2: 1 data left
head = tail = NULL;
free(head);
}else{
int isFound = 0;
//Case-3: There is more than 1 data
struct Mahasiswa *temp;
for(temp=head;temp!=NULL;temp=temp->next){
if(temp->usia == usia){
isFound = 1;
break;
}
}
if(isFound == 1)
{
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
free(temp);
}else
{
printf("Sorry coi, gak ada yg bs didelete, gk nemu coi");
getchar();
}
}
}
}

3

popDepan

void popDepan()
{
struct Mahasiswa *curr;
if(head == NULL)
{
//Case-1: No data at all
printf("Tidak ada data\n");
getchar();
}else
{
if(head == tail){
//Case-2: 1 data left
head = tail = NULL;
free(head);
}else{
//Case-3: There is more than 1 data
curr = head;
head=head->next;
free(curr);
head->prev = NULL;
}
}
}

4

popBelakang

void popBelakang(){
struct Mahasiswa *curr;
if(head == NULL)
{
//Case-1: No data at all
printf("Tidak ada data\n");
getchar();
}else
{
if(head == tail){
//Case-2: 1 data left
head = tail = NULL;
free(head);
}else{
//Case-3: There is more than 1 data
curr = tail;
tail = tail->prev;
free(curr);
tail->next = NULL;
}
}
}

5

pushDepan

void pushDepan(char nama[],int usia){
struct Mahasiswa *curr = (struct Mahasiswa*)malloc(sizeof(struct Mahasiswa));
strcpy(curr->nama,nama);
curr->usia = usia;
if(head == NULL)
{
head = tail = curr;
tail->next = NULL;
}
else
{
curr->next = head;
head->prev = curr;
head = curr;
}
head->prev = NULL;
}

6

pushBelakang

void pushBelakang(char nama[],int usia){
struct Mahasiswa *curr = (struct Mahasiswa*)malloc(sizeof(struct Mahasiswa));
strcpy(curr->nama,nama);
curr->usia = usia;
if(head == NULL)
{
head = tail = curr;
head->prev = NULL;
}
else
{
tail->next = curr;
curr->prev = tail;
tail = curr;
}
tail->next = NULL;
}

7

smartPop

void smartPop(int usia){
if(head != NULL){
if(usia == head->usia){
popDepan();
}else if(usia == tail->usia){
popBelakang();
}else{
popTengah(usia);
}
}else{
printf("Belum ada data coi\n");
getchar();
}
}

8

smartPush

void smartPush(char nama[],int usia){
if(head == NULL){
pushDepan(nama,usia);
}else{
if(strcmp(nama,head->nama)nama)>0){
pushBelakang(nama,usia);
}else{
pushTengah(nama,usia);
}
}
}