Last active
May 7, 2020 06:13
-
-
Save dbwodlf3/be722f832a610c0406f91d913ba4bb43 to your computer and use it in GitHub Desktop.
double linked list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cmake_minimum_required(VERSION 3.1) | |
| project(toy) | |
| include_directories(${CMAKE_CURRENT_SOURCE_DIR}) | |
| add_executable(toy | |
| main.c | |
| double.c | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <double.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| void printList(listNode* h){ | |
| listNodeItem* p = h->first; | |
| int count = 0; | |
| while(1){ | |
| printListNodeItem(p); | |
| count++; | |
| if(count >= h->length)break; | |
| p = p->rLink; | |
| } | |
| printf("Node Length: %d\n", h->length); | |
| } | |
| int insertNode(listNode* h, listNodeItem* n, int order){ | |
| if(order > h->length){ | |
| printf("out of length.\n"); | |
| return 1; | |
| } | |
| else if(order==0){ | |
| insertFirst(h, n); | |
| return 0; | |
| } | |
| else if(order == h->length){ | |
| insertLast(h, n); | |
| return 0; | |
| } | |
| else{ | |
| listNodeItem* target = getNodeItem(h, order); | |
| listNodeConnect(target->lLink, n); | |
| listNodeConnect(n, target); | |
| h->length +=1; | |
| return 0; | |
| } | |
| } | |
| int insertFirst(listNode* h, listNodeItem* n){ | |
| listNodeConnect(n, h->first); | |
| h->first = n; | |
| h->length += 1; | |
| return 0; | |
| } | |
| int insertLast(listNode* h, listNodeItem* n){ | |
| listNodeItem* previous_item = getNodeItem(h, (h->length -1)); | |
| listNodeConnect(previous_item, n); | |
| h->length +=1; | |
| return 0; | |
| } | |
| int deleteNode(listNode* h, int order){ | |
| if(order>= h->length){ | |
| printf("out of length.\n"); | |
| return 1; | |
| } | |
| else if(order==0){ | |
| deleteFirst(h); | |
| return 0; | |
| } | |
| else if(order == h->length-1){ | |
| deleteLast(h); | |
| return 0; | |
| } | |
| else{ | |
| listNodeItem* target = getNodeItem(h, order); | |
| listNodeConnect(target->lLink, target->rLink); | |
| free(target); | |
| h->length -=1; | |
| return 0; | |
| } | |
| } | |
| int deleteFirst(listNode* h){ | |
| free(h->first); | |
| h->first->lLink = NULL; | |
| h->first = h->first->rLink; | |
| h->length -=1; | |
| return 0; | |
| }; | |
| int deleteLast(listNode* h){ | |
| listNodeItem* lastItem = getNodeItem(h, (h->length -1)); | |
| lastItem->lLink->rLink = NULL; | |
| free(lastItem); | |
| h->length -=1; | |
| return 0; | |
| }; | |
| //helper Function | |
| void printListNodeItem(listNodeItem* n){ | |
| if(n==NULL){ | |
| printf("NULL\n"); | |
| printf("==========\n"); | |
| return 1; | |
| } | |
| printf("printListNode\n"); | |
| printf("key: %d\n", n->key); | |
| printf("rLink: %p\n", n->rLink); | |
| printf("lLink: %p\n", n->lLink); | |
| printf("addr: %p\n", n); | |
| printf("==========\n"); | |
| } | |
| listNodeItem* createListNodeItem(int key){ | |
| listNodeItem* returnNode = malloc(sizeof(listNodeItem)); | |
| returnNode -> key = key; | |
| returnNode -> rLink = NULL; | |
| returnNode -> lLink = NULL; | |
| return returnNode; | |
| } | |
| listNode* createListNode(listNodeItem* firstNode){ | |
| listNode* returnListNode = malloc(sizeof(listNode)); | |
| returnListNode -> length = 1; | |
| returnListNode -> first = firstNode; | |
| return returnListNode; | |
| } | |
| void listNodeConnect(listNodeItem* previous_item, listNodeItem* next_item){ | |
| previous_item->rLink = next_item; | |
| next_item->lLink = previous_item; | |
| } | |
| /** | |
| * 0~n 까지. | |
| */ | |
| listNodeItem* getNodeItem(listNode* inputListNode, int order){ | |
| int count = 0; | |
| listNodeItem* p = inputListNode -> first; | |
| while(1){ | |
| if(count>=order)break; | |
| p = p -> rLink; | |
| count++; | |
| } | |
| return p; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef MY_DOUBLE_LINK | |
| #define MY_DOUBLE_LINK | |
| #include <stdint.h> | |
| typedef struct NodeWrapper listNode; | |
| typedef struct Node listNodeItem; | |
| //datatypes | |
| struct Node{ | |
| int key; | |
| struct Node* lLink; | |
| struct Node* rLink; | |
| }; | |
| struct NodeWrapper{ | |
| int length; | |
| listNodeItem* first; | |
| }; | |
| //functions | |
| void printList(listNode* h); | |
| int insertNode(listNode* h, listNodeItem* n, int order); | |
| int insertFirst(listNode* h, listNodeItem* n); | |
| int insertLast(listNode* h, listNodeItem* n); | |
| int deleteNode(listNode* h, int order); | |
| int deleteFirst(listNode* h); | |
| int deleteLast(listNode* h); | |
| //helper functions | |
| void printListNodeItem(listNodeItem* n); | |
| void listNodeConnect(listNodeItem* previous_item, listNodeItem* next_item); | |
| listNodeItem* getNodeItem(listNode* inputListNode, int order); | |
| listNodeItem* createListNodeItem(int key); | |
| listNode* createListNode(listNodeItem* firstNode); | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include<stdlib.h> | |
| #include<double.h> | |
| int main(int argc, char* argv){ | |
| listNode* myListNode = createListNode(createListNodeItem(10)); | |
| //myListNode 10 | |
| insertNode(myListNode, createListNodeItem(20), 0); //insertFirst | |
| //myListNode 20 10 | |
| insertNode(myListNode, createListNodeItem(30), 2); //insertLast | |
| //myListNode 20 10 30 | |
| insertNode(myListNode, createListNodeItem(40), 2); //insertNode | |
| // myListNode 20 10 40 30 | |
| insertNode(myListNode, createListNodeItem(50), 2); //insertNode | |
| // myListNode 20 10 50 40 30 | |
| printList(myListNode); | |
| printf("\n=====inesrtNode,insertFirst,insertLast test====\n"); | |
| deleteNode(myListNode, 0); //deleteFirst | |
| //myListNode 10 50 40 30 | |
| deleteNode(myListNode, 3); //deleteLast | |
| //myListNode 10 50 40 | |
| deleteNode(myListNode, 1); //deleteNode | |
| //myListNode 10 40 | |
| printList(myListNode); | |
| printf("=====deleteNode,deleteFirst,deleteLast test====\n"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment