Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Data Structure - Single Linklist Template
- *
- * Buat penggunaan, cukup ganti Attribute dari struct Template aja
- * int value, dipakai untuk sorting,
- * jadi misal butuh fungsi untuk sorting angka, tinggal ganti semua value ke attribute yang diinginkan
- *
- * char string[255], dipakai untuk sorting
- * jadi misal butuh fungsi untuk sorting berdasarkan nama, tinggal ganti semua string[255] jadi nama[255]
- *
- * untuk tambah attribute tinggal ganti dibagian struct & fungsi createNode();
- *
- * tinggal #include "cv_linklist_template.c" di program utama kalian
- *
- * contoh program utama
- * ____________________________________________________________________________________________________________________
- * #include "stdio.h"
- * #include "cv_linklist_template.c"
- *
- * size_t main(int argc, char **argv){
- * Data *pertama = createNode(0x01, "contoh buat node baru");
- * Data *kedua = createNode(0x02, "tinggal panggil fungsi createNode");
- * Data *ketiga = createNode(0x03, "Masukkan parameter sesuai Struct");
- *
- * pushSortAscByInt(kedua);
- * pushSortAscByInt(ketiga);
- * pushSortAscByInt(pertama);
- * viewData();
- * }
- * ____________________________________________________________________________________________________________________
- * Tested in C85, C90, C99, C11 Standard
- */
- #include "malloc.h"
- #include "string.h"
- const int FAILED = 0;
- const int SUCCESS = 1;
- struct Template{
- int value; /* sorting by number */
- char string[255]; /* sorting by string */
- struct Template *next;
- }*head, *tail, *current;
- typedef struct Template Data;
- Data *createNode(int value, char string[255]);
- void pushHead(Data *newNode);
- void pushTail(Data *newNode);
- int popHead();
- int popTail();
- void popAll();
- int viewData();
- int pushFirstData(Data *newNode);
- int popFirstData();
- int isFirstOrLastAscIntData(Data *newNode);
- int isFirstOrLastAscStrData(Data *newNode);
- int isFirstOrLastDescIntData(Data *newNode);
- int isFirstOrLastDescStrData(Data *newNode);
- void pushSortAscByInt(Data *newNode);
- void pushSortAscByStr(Data *newNode);
- void pushSortDescByInt(Data *newNode);
- void pushSortDescByStr(Data *newNode);
- /*Single Linklist starts here*/
- Data *createNode(int value, char string[255]){
- Data *newNode = (Data*)malloc(sizeof(Data));
- newNode->value = value;
- newNode->next = NULL;
- strcpy(newNode->string, string);
- return newNode;
- }
- void pushHead(Data *newNode){
- if( !pushFirstData(newNode) ){
- newNode->next = head;
- head = newNode;
- }
- }
- void pushTail(Data *newNode){
- if( !pushFirstData(newNode) ){
- tail->next = newNode;
- tail = newNode;
- }
- }
- int popHead(){
- if( !head ) return FAILED;
- if( !popFirstData() ){
- current = head;
- head = head->next;
- free(current);
- return SUCCESS;
- }
- }
- int popTail(){
- if( !head ) return FAILED;
- if( !popFirstData() ){
- current = head;
- while( current->next != tail ) current = current->next;
- free(tail);
- tail = current;
- tail->next = NULL;
- return SUCCESS;
- }
- }
- void popAll(){
- while( head ) popHead();
- }
- int viewData(){
- if( !head ) return FAILED;
- current = head;
- int i = 0;
- while( current ){
- printf("%02d. %5d %10s", ++i, current->value, current->string);
- if( current == head ) printf(" <- head ");
- if( current == tail ) printf(" <- tail ");
- puts("");
- current = current->next;
- }
- }
- int pushFirstData(Data *newNode){
- if( !head ){
- head = tail = newNode;
- return SUCCESS;
- }
- return FAILED;
- }
- int popFirstData(){
- if( head == tail ){
- head = tail = NULL;
- return SUCCESS;
- }
- return FAILED;
- }
- int isFirstOrLastAscIntData(Data *newNode){
- if( newNode->value < head->value || newNode->value >= tail->value ){
- if( newNode->value < head->value )
- pushHead(newNode);
- else if( newNode->value >= tail->value )
- pushTail(newNode);
- return SUCCESS;
- }
- return FAILED;
- }
- int isFirstOrLastAscStrData(Data *newNode){
- if( strcmp(head->string, newNode->string) > 0 || strcmp(tail->string, newNode->string) <= 0 ){
- if( strcmp(head->string, newNode->string) > 0 )
- pushHead(newNode);
- else if( strcmp(tail->string, newNode->string) <= 0 )
- pushTail(newNode);
- return SUCCESS;
- }
- return FAILED;
- }
- int isFirstOrLastDescIntData(Data *newNode){
- if( newNode->value > head->value || newNode->value <= tail->value ){
- if( newNode->value > head->value )
- pushHead(newNode);
- else if( newNode->value <= tail->value )
- pushTail(newNode);
- return SUCCESS;
- }
- return FAILED;
- }
- int isFirstOrLastDescStrData(Data *newNode){
- if( strcmp(head->string, newNode->string) < 0 || strcmp(tail->string, newNode->string) >= 0 ){
- if( strcmp(head->string, newNode->string) < 0 )
- pushHead(newNode);
- else if( strcmp(tail->string, newNode->string) >= 0 )
- pushTail(newNode);
- return SUCCESS;
- }
- return FAILED;
- }
- void pushSortAscByInt(Data *newNode){
- if( pushFirstData(newNode) ) return;
- if( isFirstOrLastAscIntData(newNode) ) return;
- current = head;
- while( current ){
- if( current->next->value > newNode->value ) break;
- current = current->next;
- }
- newNode->next = current->next;
- current->next = newNode;
- }
- void pushSortAscByStr(Data *newNode){
- if( pushFirstData(newNode) ) return;
- if( isFirstOrLastAscStrData(newNode) ) return;
- current = head;
- while( current ){
- if( strcmp(current->next->string, newNode->string) > 0 ) break;
- current = current->next;
- }
- newNode->next = current->next;
- current->next = newNode;
- }
- void pushSortDescByInt(Data *newNode){
- if( pushFirstData(newNode) ) return;
- if( isFirstOrLastDescIntData(newNode) ) return;
- current = head;
- while( current ){
- if( current->next->value < newNode->value ) break;
- current = current->next;
- }
- newNode->next = current->next;
- current->next = newNode;
- }
- void pushSortDescByStr(Data *newNode){
- if( pushFirstData(newNode) ) return;
- if( isFirstOrLastDescStrData(newNode) ) return;
- current = head;
- while( current ){
- if( strcmp(current->next->string, newNode->string) < 0 ) break;
- current = current->next;
- }
- newNode->next = current->next;
- current->next = newNode;
- }
- /*Single Linklist ends here*/
Add Comment
Please, Sign In to add comment