Friday, April 3, 2015

[Data Structure - C] Circular Double Linked List


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

typedef struct _Node {
    char Data;
    struct _Node *Next;
    struct _Node *Prev;
} Node;

Node *head, *tail, *temp;

void initList(void);
void insertNode(Node *);
void deleteNode(Node *);
Node *makeNode(char);
Node *findNode(char);
void printList(void);

int main(int argc, const char * argv[]) {
    int command = 0;
    char input = 'A';
    
    Node *tempNode;
    
    printf("Hello, this is Circular Double Linkded List!\n");
    while(1) {
        printf ("Command List\n");
        printf ("1. List Initialization\n");
        printf ("2. Insert Node\n");
        printf ("3. Delete Node\n");
        printf ("4. Print List\n");
        printf ("5. Exit List\n");
        printf ("Enter your command: ");
        scanf ("%d",&command);
        getchar();
        switch (command) {
            case 1:
                printf("List initializing\n");
                initList();
                break;
            case 2:
                printf("Insert Node\nPlease enter a number what you want to insert : ");
                scanf("%c",&input);
                getchar();
                tempNode = makeNode(input);
                insertNode(tempNode);
                break;
            case 3:
                printf("Delete Node\nPlease enter a number what you want to delete : ");
                scanf("%c",&input);
                getchar();
                tempNode = findNode(input);
                if(tempNode->Data==input) {
                    deleteNode(tempNode);
                } else {
                    printf("There is no charactor you insert!\n");
                }
                break;
            case 4:
                printf("Node List\n");
                printList();
                break;
            case 5:
                printf("Exit List. Bye bye!");
                exit(0);
            default:
                printf("There is no command like that!\nPlease retry!");
                break;
        }
    }
    return 0;
}

void initList(void) {
    head = (Node *)malloc(sizeof(Node));
    tail = (Node *)malloc(sizeof(Node));
    head->Next = tail;
    head->Prev = tail;
    tail->Next = head;
    tail->Prev = head;
}

void insertNode(Node *node) {
    node->Next = tail;
    node->Prev = tail->Prev;
    tail->Prev->Next = node;
    tail->Prev = node;
}

void deleteNode(Node *node) {
    node->Prev->Next = node->Next;
    node->Next->Prev = node->Prev;
    free(node);
}

Node *makeNode(char input) {
    Node *node = (Node *)malloc(sizeof(Node));
    node->Data = input;
    return node;
}

Node *findNode(char input) {
    Node *currentNode = head->Next;
    while(currentNode!=tail&&currentNode->Data != input) {
        currentNode = currentNode->Next;
    }
    return currentNode;
}

void printList(void) {
    Node *currentNode = head->Next;
    while(currentNode != tail) {
        printf("Node Data is %c \n",currentNode->Data);
        currentNode = currentNode->Next;
    }
}

No comments:

Post a Comment