linklist.c
*
* Created on: 2013年11月12日
*
*/
#include "linklist.h"
void add(int val){
Node *v = malloc(sizeof(Node));
v->value = val;
if(head == NULL){
head = v;
return;
}
Node* current = head;
while(1){
if(current->next == NULL){
current->next = v;
break;
}
current = current->next;
}
size++;
// printf("the size of linklist:%d",size);
}
void travel(){
Node* current = head;
do{
printf("%d\n",current->value);
}while((current = current->next)!=NULL);
}
int del(Node *node){
return -1;
}
Node* get(int i){
printf("the size:%d\n",size);
if(i >= size){
printf("array out of index\n");
return NULL;
}
Node* current = head;
int index = 0;
while(index != i){
current = current->next;
if(++index == i) break;
}
printf("-----------------\n");
return current;
}
void destroy(){
Node* current = head;
Node* nt = NULL;
while(current != NULL){
nt = current->next;
free(current);
current = nt;
}
}
int length(){
return size;
}
int main(){
int j = 0;
for(;j<10;j++)
add(j+1);
travel();
destroy();
return -1;
}
头文件 linklist.h
/*
linklist.h
*
* Created on: 2013年11月12日
*
*/
#ifndef LINKLIST_H_
#define LINKLIST_H_
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
/****************
* the node of the linklist
*/
typedef struct LinkNode{
int value;
struct LinkNode *next;
}Node;
/****************
* represent the linklist
*/
static Node *head = NULL;
static unsigned int size = 1;
/*****add Node******/
void add(int val);
/*******remove node****/
int del(Node *node);
/*******get Node***/
Node* get(int i);
/******create link*/
void destroy();
/*******travel*****/
void travel();
/*****the size of linklist***/
int length();
#endif /* LINKLIST_H_ */