CodeHighlight

2013年9月28日 星期六

[DS]Queue 實作 by C

這次像上篇一樣,不過改練習Queue的實作,
基本概念就是先進先出,是只做了enQueue、deQueue、listQueue三個

是說很久沒有寫了,在enQueue的地方還有點懷疑這樣寫到底正不正確XD


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

typedef struct node{
        int data;
        struct node *next;
}NODE;

NODE* inQueue(NODE *top, int input){
        NODE *newNode;
        NODE *headPtr = top;
        newNode = (NODE*)malloc(sizeof(NODE));
        newNode->data = input;
        newNode->next = NULL;
        if(top == NULL){
                return newNode;
        }
        while(top->next != NULL){       //move to last
                top = top->next;
        }
        top->next = newNode;
        return headPtr;
}

NODE* deQueue(NODE *top){
        NODE *tmpNode = top;
        if(top == NULL){return top;}
        top = top->next;
        free(tmpNode);
        return top;
}

void listQueue(NODE* top){
        if(top == NULL){
                printf("EMPTY QUEUE\n");
                return;
        }
        while(top != NULL){
                printf("%d ", top->data);
                top = top->next;
        }
        printf("\n");
}

int main(){
        NODE *node;
        node = inQueue(node, 1);
        listQueue(node);
        node = inQueue(node, 2);
        listQueue(node);
        node = inQueue(node, 3);
        listQueue(node);
        node = deQueue(node);
        listQueue(node);
        node = deQueue(node);
        listQueue(node);
        node = deQueue(node);
        listQueue(node);
        node = deQueue(node);
        listQueue(node);
        node = inQueue(node, 1);
        node = inQueue(node, 2);
        node = inQueue(node, 3);
        node = inQueue(node, 4);
        node = inQueue(node, 5);
        listQueue(node);
        node = deQueue(node);
        node = deQueue(node);
        node = deQueue(node);
        listQueue(node);
        return 0;
}

2013年9月15日 星期日

[DS]Stack 實作 by C

最近看論文陷入一個瓶頸,感覺還是寫code實際,
突然想到很久沒有寫看看基本的資料結構了,所以決定回頭用C寫個stack,
順便複習一下指標的用法。
#include<stdio.h>
#include<stdlib.h>

typedef struct node{
        char data1;
        struct node *Link;
}NODE;

NODE* initSteak(NODE *top){
        top = NULL;
        return top;
}

NODE* pushSteak(NODE* top, char data){
        NODE *newBlock;
        newBlock = (NODE*)malloc(sizeof(NODE));
        newBlock->data1 = data;
        newBlock->Link = top;
        top = newBlock;
        return top;
}

NODE* popSteak(NODE *top){
        NODE *tmpBlock = top;
        top = top->Link;
        free(tmpBlock);
        return top;
}

void listSteak(NODE *top){
        while(!isEmpty(top)){
                printf("%c ", top->data1);
                top = top->Link;
        }
        printf("\n");
}

int isEmpty(NODE* top){
        return (top == NULL);
}

int main(){
        NODE *node;
        node = initSteak(node);

        node = pushSteak(node, 'a');
        node = pushSteak(node, 'b');
        listSteak(node);
        node = popSteak(node);
        listSteak(node);
        node = pushSteak(node, 'c');
        node = pushSteak(node, 'd');
        node = pushSteak(node, 'e');
        listSteak(node);
        node = popSteak(node);
        node = popSteak(node);
        node = popSteak(node);
        listSteak(node);
}