top of page
Search

05- Review

  • gardionsaltana
  • Mar 30, 2020
  • 4 min read

Well let's recap what we have learn. We have learned about linked list, double linked list, stack, queue, prefix, postfix, infix, Hashing and Binary tree.


Linked list and double linked list

Linked list and double linked list are almost of the same, except double linked list has previous. The concept is basically from the one point to another point like riding a bus, it can go forward and backword.

And make sure when you code the previous and next are correct since if it's not it will crash.


Stack

Stack is the concept of first in and last out, what ever data you put last will go out first.

Like a jar of cookies, the first you put will be at the very bottom and the last cookie you put will be at the top. This can use linked list and take the head(last data) and go forward.


Queue

Queue is the concept first in and first out, like when you're waiting in the line. The first one who wait will get to enter first. Queue have 2 concept circular queue and linear queue. Circular queue is the queue which connect the last place to the first place and linear queue is only a straight line. When the first person get in to the club, if it's linear queue the place will not be placed by anything, if it's circular queue until you reached the max amount of queue then it will start at the first place again. This can use array concept but if you want to use linked list concept it is okay.


Prefix Postfix Infix

Infix is basic math, Prefix is when the operator is first then the number or symbol. Postfix is when the number or symbol first then the operator.

Becarefull when coding the position must be right for example 9 / 3, the position must be / 9 3 in prefix not / 3 9 since the answer will be different and the way you read the prefix, postfix is still left to right.


Hashing

Hashing is when you try to encrypt something, by using this technique you can make a search way-way faster, and make secret code that only the decoder can understand. This can also concept us the concept of array combine with linked list. Well you can use linked list for both of them but it will be more complicated.


Binary Tree

Binary Tree is the concept of comparing data and determine whether it will go left or right, just like double linked list but imagine you starting in the middle of the data rather than one of the end. This can only store int or something that has one value, well technically it can store many value like string but it will be harder to code it. This can use linked list or double linked list, and there's 3 ways to view it, inorder, preorder, postorder.

inorder is go to the left, print and then go to the right.

preorder is print first then go to the left and go to the right.

postorder is go to the most left then go to the most right then print.

This can use to transform from infix to prefix and postfix using binary tree expression.

This method use recursion since it will be easier.


Here's an example of a code that can enter a product(auto sort), edit, delete using double linked list:


#include<stdio.h> #include<malloc.h> #include<stdlib.h> #include<ctype.h> #include<time.h> #include<string.h> struct Data{ char namaProduct[21]; int qty; int price; struct Data *next; struct Data *prev; }*head=NULL, *tail; int menu(){ int choice=0; printf("1. Add Product\n2. Edit Product\n3. Display\n4. Check-Out\n5. Exit\n"); do{ printf("Menu: "); scanf("%d",&choice); getchar(); }while(choice<1||choice>5); printf("\n"); return choice; } void pushProduct(struct Data input){ char currChar,posChar=tolower(input.namaProduct[0]); struct Data *node=(Data*) malloc (sizeof(Data)); input.namaProduct[0]=toupper(input.namaProduct[0]); *node=input; node->next=NULL; node->prev=NULL; if(head==NULL){ head=node; tail=node; }else if(strcmp(head->namaProduct,input.namaProduct)<0){ node->next=head; head->prev=node; head=node; }else if(strcmp(tail->namaProduct,input.namaProduct)>0){ tail->next=node; node->prev=tail; tail=node; }else{ struct Data *curr=head; while(tolower(curr->namaProduct[0])==posChar&&curr->next!=NULL){ curr=curr->next; } if(tolower(curr->namaProduct[0])==posChar){ struct Data *temp; temp=curr; while(strcmp(temp->namaProduct,node->namaProduct)<0){ temp=temp->prev ; } curr=temp; int specEject=0; temp=head; while(temp!=NULL){ if(strcmp(temp->namaProduct,node->namaProduct)==0){ specEject=1; break; } temp=temp->next; } if(specEject!=0){ printf("You cannot put the same item twice\n"); } else{ node->next=curr->next; node->prev=curr; curr->next->prev=node; curr->next=node; } }else{ node->next=curr->next; node->prev=curr; curr->next->prev=node; curr->next=node; } } } void addProduct(){ struct Data input; int flag=0; do{ if(flag!=0){ printf("Name must be between 2 to 20\n"); } printf("Product name[2..20]: "); scanf("%[^\n]",input.namaProduct); getchar(); flag++; }while(strlen(input.namaProduct)<2||strlen(input.namaProduct)>20); do{ if(flag==0){ printf("Quantity cannot be zero\n"); } printf("Product Quantity: "); scanf("%d",&input.qty); getchar(); flag=0; }while(input.qty==0); srand(time(NULL)); input.price=(rand()%99)+1; pushProduct(input); } void editProduct(){ char input[50]; int flag=0,choice=0; struct Data *curr=head; printf("Input product name: "); scanf("%[^\n]",input); getchar(); while(curr!=NULL){ if(!strcmp(curr->namaProduct,input)){ flag=1; break; } curr=curr->next; } if(flag==1){ printf("1. Edit\n2. Delete\n"); do{ printf("Input: "); scanf("%d",&choice); getchar(); }while(choice<1||choice>2); if(choice==1){ flag=1; do{ if(flag==0){ printf("Quantity cannot be zero\n"); } printf("Product Quantity: "); scanf("%d",&curr->qty); getchar(); flag=0; }while(curr->qty==0); }else{ if(curr==head&&curr==tail){ free(curr); head=NULL; tail=NULL; }else if(curr==head){ head=head->next; head->prev=NULL; free(curr); }else if(curr==tail){ tail=tail->prev; free(curr); tail->next=NULL; }else{ curr->prev->next=curr->next; curr->next->prev=curr->prev; free(curr); } } }else{ printf("There's no product called %s\n",input); } } void displayProduct(){ struct Data *curr=tail; int total=0; while(curr!=NULL){ total+=curr->price*curr->qty; printf("Name: %s | Qty: %d | Price: $%d| Total Price: $%d\n",curr->namaProduct,curr->qty,curr->price,curr->price*curr->qty); curr=curr->prev; } printf("Total: $%d\n",total); } void finale(){ struct Data *curr; while(head!=NULL){ curr=head; head=head->next; free(curr); } } int main(){ int choice; do{ system("cls"); choice=menu(); switch(choice){ case 1:{ addProduct(); break; } case 2:{ editProduct(); break; } case 3:{ printf("List product: \n"); displayProduct(); break; } } if(choice<=3){ printf("\nPress enter to continue"); getchar(); } }while(choice<=3); if(choice==4){ printf("\t\t\tTha Market\n"); displayProduct(); printf("\nKindness is free\n"); } finale(); return 0; }




 
 
 

Recent Posts

See All
06 - AVL

AVL is another version of Binary Search Tree. The concept of AVL is still the same with binary search tree, but this time they limit to...

 
 
 
03 Hash

Now we're going to learn about hash and hash table You can imagine array and linked list combine, you get hash Complexity depends on the...

 
 
 

Comments


Post: Blog2_Post

Subscribe Form

Thanks for submitting!

  • Facebook
  • Twitter
  • LinkedIn

©2020 by Gardion Learning Site. Proudly created with Wix.com

bottom of page