搜尋此網誌

2013年1月29日 星期二

Linked List implementation in C

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

struct Node{
    int value;
    struct Node* nextNode ;
};

typedef struct Node* NodePtr;

void insert(NodePtr  , int value);
void display(NodePtr );

int main (void){

    NodePtr start = (NodePtr)malloc (sizeof(NodePtr));
    start->value = 0;
    start->nextNode = NULL;
    insert(start , 1);
    insert(start , 2);
    insert(start , 3);
    display(start);
    return 0;
}

void insert(NodePtr node , int value){
    NodePtr temp = node;
    while(temp->nextNode != NULL){
        temp = temp->nextNode;
    }
    NodePtr new = (NodePtr)malloc (sizeof(NodePtr));
    new->value = value;
    new->nextNode = NULL;
    temp->nextNode = new;
}

void display(NodePtr node){
    NodePtr temp = node;
    while(temp != NULL){
        printf("%d\n", temp->value);
        temp= temp->nextNode;
    }
}

沒有留言:

張貼留言