嵌入式 双向链表的头尾中插入法以及释放
2013-12-02 14:57
267 查看
BIN = DoubleListDemo
CC = gcc
SRC = DoubleList.c main.c
OBJS = DoubleList.o main.o
CFLAGS = -Wall -O2
$(BIN): $(OBJS)
$(CC) -o $(BIN) $(OBJS) $(CFLAGS)
$(OBJS): $(SRC)
gcc -c $(SRC) $(CFLAGS)
clean:
rm -rf *.o
rm -rf $(BIN)
distclean:
-rm *.o
DoubleList.h
[cpp]
viewplaincopyprint?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef
int KEY;
typedef
struct {
KEY key;
int
date;
}DATE;
typedef
structnode{
DATE date;
struct
node *pre;
struct
node *next;
}NODE;
typedef
struct{
int
num;
NODE *head;
NODE *tail;
}DOUBLE_LIST;
voidInitDoubleList(DOUBLE_LIST *list);
voidDoubleListPrint(DOUBLE_LIST list);
void
PrintNode(NODE *node);
voidHeadInsterNode(DOUBLE_LIST *list, DATEdate);
void
XInsterNode(DOUBLE_LIST*list, DATE date, int
x);
voidTailInsterNode(DOUBLE_LIST *list, DATEdate);
voidHeadDeleteNode(DOUBLE_LIST *list);
void
XDeleteNode(DOUBLE_LIST*list, KEY key);
voidTailDeleteNode(DOUBLE_LIST *list);
void
FreeList(DOUBLE_LIST*list);
DoubleList.c
[cpp]
viewplaincopyprint?
#include "DoubleList.h"
voidInitDoubleList(DOUBLE_LIST *list)
{
list->head = NULL;
list->tail = NULL;
list->num = 0;
return
;
}
voidDoubleListPrint(DOUBLE_LIST list)
{
int
i =0;
NODE * p = NULL;
p = list.head;
if(0 ==list.num){
printf("Your List isEmpty!\n");
return;
}
for(i = 0; i < list.num;i++){
PrintNode(p);
p = p->next;
}
return;
}
void
PrintNode(NODE *node)
{
printf("key:%d\tdate:%d\n",node->date.key,node->date.date);
return;
}
voidHeadInsterNode(DOUBLE_LIST * list, DATEdate)
{
NODE * newnode = (NODE *)malloc(sizeof(NODE));
newnode->date.key = date.key;
newnode->date.date =date.date;
if(0 ==list->num){
newnode->next = NULL;
newnode->pre = NULL;
list->head = newnode;
list->tail = newnode;
list->num++;
return;
}
newnode->pre = NULL;
newnode->next = list->head;
list->head->pre = newnode;
list->head = newnode;
list->num++;
return;
}
void
XInsterNode(DOUBLE_LIST*list, DATE date, int
x)
{
int
i;
NODE * newnode = NULL;
NODE * prenode = NULL;
NODE * nextnode = NULL;
if(x < 0 ||x >=list->num){
printf("X Inster: x is notright!\n");
return;
}
if(0 ==x){
HeadInsterNode(list, date);
return;
}
if(list->num == x -1){
TailInsterNode(list, date);
return;
}
prenode = NULL;
nextnode = list->head;
for(i = 0; i < x;i++){
prenode = nextnode;
nextnode = nextnode->next;
}
newnode = (NODE *)malloc(sizeof(NODE));
newnode->date.key = date.key;
newnode->date.date =date.date;
prenode->next = newnode;
newnode->pre = prenode;
newnode->next = nextnode;
nextnode->pre = newnode;
list->num++;
return
;
}
voidTailInsterNode(DOUBLE_LIST *list, DATEdate)
{
NODE * newnode = (NODE *)malloc(sizeof(NODE));
newnode->date.key = date.key;
newnode->date.date = date.date;
if(0 ==list->num){
newnode->next = NULL;
newnode->pre = NULL;
list->head = newnode;
list->tail = newnode;
list->num++;
return;
}
newnode->next = NULL;
newnode->pre = list->tail;
list->tail->next = newnode;
list->tail = newnode;
list->num++;
return;
}
voidHeadDeleteNode(DOUBLE_LIST *list)
{
NODE * tmpnode = NULL;
if(0 == list->num){
printf("%s:NoNode InList!\n",__FUNCTION__);
return;
}
if(1 ==list->num){
free(list->head);
list->head = NULL;
list->tail = NULL;
list->num = 0;
return
;
}
tmpnode = list->head;
list->head = list->head->next;
free(tmpnode);
tmpnode = NULL;
list->num--;
return
;
}
void
XDeleteNode(DOUBLE_LIST*list, KEY key)
{
;
}
voidTailDeleteNode(DOUBLE_LIST *list)
{
NODE * tmpnode = NULL;
if(0 == list->num){
printf("%s:NoNode InList!\n",__FUNCTION__);
return;
}
if(1 ==list->num){
free(list->head);
list->head = NULL;
list->tail = NULL;
list->num = 0;
return
;
}
tmpnode = list->tail;
list->tail = list->tail->pre;
list->tail->next = NULL;
free(tmpnode);
tmpnode = NULL;
list->num--;
return;
}
void
FreeList(DOUBLE_LIST*list)
{
int
i;
NODE * tmpnode = NULL;
int
num;
num = list->num;
for(i = 0; i < num;i++){
tmpnode = list->head;
list->head =list->head->next;
free(tmpnode);
tmpnode = NULL;
list->num--;
}
list->head = NULL;
list->tail = NULL;
return;
}
main.c
[cpp]
viewplaincopyprint?
#include "DoubleList.h"
int
main(int
argc,char*argv[])
{
DOUBLE_LIST list;
int
i;
DATE date[10];
for(i = 0; i < 10;i++){
date[i].key = i + 1;
date[i].date = i + 11;
}
#if1
printf("init listtest\n");
InitDoubleList(&list);
DoubleListPrint(list);
#endif
#if1
//Head inster test
printf("headinster test\n");
HeadInsterNode(&list,date[0]);
HeadInsterNode(&list, date[1]);
HeadInsterNode(&list,date[2]);
HeadInsterNode(&list, date[3]);
HeadInsterNode(&list,date[4]);
DoubleListPrint(list);
#endif
#if1
//tail inster test
printf("\ntailinster test\n");
TailInsterNode(&list,date[0]);
TailInsterNode(&list, date[1]);
TailInsterNode(&list,date[2]);
TailInsterNode(&list, date[3]);
TailInsterNode(&list,date[4]);
DoubleListPrint(list);
#endif
#if1
//x inster test
printf("\nxinster test\n");
XInsterNode(&list, date[5],5);
XInsterNode(&list, date[6], 6);
XInsterNode(&list, date[7],7);
XInsterNode(&list, date[8], 8);
XInsterNode(&list, date[9],9);
DoubleListPrint(list);
#endif
#if1
//head delete test
printf("\nheaddelete test\n");
HeadDeleteNode(&list);
HeadDeleteNode(&list);
HeadDeleteNode(&list);
HeadDeleteNode(&list);
HeadDeleteNode(&list);
DoubleListPrint(list);
#endif
#if 1
//tail deletetest
printf("\ntail deletetest\n");
TailDeleteNode(&list);
TailDeleteNode(&list);
TailDeleteNode(&list);
TailDeleteNode(&list);
TailDeleteNode(&list);
DoubleListPrint(list);
#endif
#if 1
//free listtest
printf("\nfree listtest\n");
FreeList(&list);
DoubleListPrint(list);
#endif
return
0;
}
CC = gcc
SRC = DoubleList.c main.c
OBJS = DoubleList.o main.o
CFLAGS = -Wall -O2
$(BIN): $(OBJS)
$(CC) -o $(BIN) $(OBJS) $(CFLAGS)
$(OBJS): $(SRC)
gcc -c $(SRC) $(CFLAGS)
clean:
rm -rf *.o
rm -rf $(BIN)
distclean:
-rm *.o
BIN = DoubleListDemo CC = gcc SRC = DoubleList.c main.c OBJS = DoubleList.o main.o CFLAGS = -Wall -O2 $(BIN): $(OBJS) $(CC) -o $(BIN) $(OBJS) $(CFLAGS) $(OBJS): $(SRC) gcc -c $(SRC) $(CFLAGS) clean: rm -rf *.o rm -rf $(BIN) distclean: -rm *.o
DoubleList.h
[cpp]
viewplaincopyprint?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef
int KEY;
typedef
struct {
KEY key;
int
date;
}DATE;
typedef
structnode{
DATE date;
struct
node *pre;
struct
node *next;
}NODE;
typedef
struct{
int
num;
NODE *head;
NODE *tail;
}DOUBLE_LIST;
voidInitDoubleList(DOUBLE_LIST *list);
voidDoubleListPrint(DOUBLE_LIST list);
void
PrintNode(NODE *node);
voidHeadInsterNode(DOUBLE_LIST *list, DATEdate);
void
XInsterNode(DOUBLE_LIST*list, DATE date, int
x);
voidTailInsterNode(DOUBLE_LIST *list, DATEdate);
voidHeadDeleteNode(DOUBLE_LIST *list);
void
XDeleteNode(DOUBLE_LIST*list, KEY key);
voidTailDeleteNode(DOUBLE_LIST *list);
void
FreeList(DOUBLE_LIST*list);
#include #include typedef int KEY; typedef struct { KEY key; int date; }DATE; typedef struct node{ DATE date; struct node * pre; struct node * next; }NODE; typedef struct{ int num; NODE *head; NODE *tail; }DOUBLE_LIST; void InitDoubleList(DOUBLE_LIST *list); void DoubleListPrint(DOUBLE_LIST list); void PrintNode(NODE * node); void HeadInsterNode(DOUBLE_LIST *list, DATE date); void XInsterNode(DOUBLE_LIST *list, DATE date, int x); void TailInsterNode(DOUBLE_LIST *list, DATE date); void HeadDeleteNode(DOUBLE_LIST *list); void XDeleteNode(DOUBLE_LIST *list, KEY key); void TailDeleteNode(DOUBLE_LIST *list); void FreeList(DOUBLE_LIST *list);
DoubleList.c
[cpp]
viewplaincopyprint?
#include "DoubleList.h"
voidInitDoubleList(DOUBLE_LIST *list)
{
list->head = NULL;
list->tail = NULL;
list->num = 0;
return
;
}
voidDoubleListPrint(DOUBLE_LIST list)
{
int
i =0;
NODE * p = NULL;
p = list.head;
if(0 ==list.num){
printf("Your List isEmpty!\n");
return;
}
for(i = 0; i < list.num;i++){
PrintNode(p);
p = p->next;
}
return;
}
void
PrintNode(NODE *node)
{
printf("key:%d\tdate:%d\n",node->date.key,node->date.date);
return;
}
voidHeadInsterNode(DOUBLE_LIST * list, DATEdate)
{
NODE * newnode = (NODE *)malloc(sizeof(NODE));
newnode->date.key = date.key;
newnode->date.date =date.date;
if(0 ==list->num){
newnode->next = NULL;
newnode->pre = NULL;
list->head = newnode;
list->tail = newnode;
list->num++;
return;
}
newnode->pre = NULL;
newnode->next = list->head;
list->head->pre = newnode;
list->head = newnode;
list->num++;
return;
}
void
XInsterNode(DOUBLE_LIST*list, DATE date, int
x)
{
int
i;
NODE * newnode = NULL;
NODE * prenode = NULL;
NODE * nextnode = NULL;
if(x < 0 ||x >=list->num){
printf("X Inster: x is notright!\n");
return;
}
if(0 ==x){
HeadInsterNode(list, date);
return;
}
if(list->num == x -1){
TailInsterNode(list, date);
return;
}
prenode = NULL;
nextnode = list->head;
for(i = 0; i < x;i++){
prenode = nextnode;
nextnode = nextnode->next;
}
newnode = (NODE *)malloc(sizeof(NODE));
newnode->date.key = date.key;
newnode->date.date =date.date;
prenode->next = newnode;
newnode->pre = prenode;
newnode->next = nextnode;
nextnode->pre = newnode;
list->num++;
return
;
}
voidTailInsterNode(DOUBLE_LIST *list, DATEdate)
{
NODE * newnode = (NODE *)malloc(sizeof(NODE));
newnode->date.key = date.key;
newnode->date.date = date.date;
if(0 ==list->num){
newnode->next = NULL;
newnode->pre = NULL;
list->head = newnode;
list->tail = newnode;
list->num++;
return;
}
newnode->next = NULL;
newnode->pre = list->tail;
list->tail->next = newnode;
list->tail = newnode;
list->num++;
return;
}
voidHeadDeleteNode(DOUBLE_LIST *list)
{
NODE * tmpnode = NULL;
if(0 == list->num){
printf("%s:NoNode InList!\n",__FUNCTION__);
return;
}
if(1 ==list->num){
free(list->head);
list->head = NULL;
list->tail = NULL;
list->num = 0;
return
;
}
tmpnode = list->head;
list->head = list->head->next;
free(tmpnode);
tmpnode = NULL;
list->num--;
return
;
}
void
XDeleteNode(DOUBLE_LIST*list, KEY key)
{
;
}
voidTailDeleteNode(DOUBLE_LIST *list)
{
NODE * tmpnode = NULL;
if(0 == list->num){
printf("%s:NoNode InList!\n",__FUNCTION__);
return;
}
if(1 ==list->num){
free(list->head);
list->head = NULL;
list->tail = NULL;
list->num = 0;
return
;
}
tmpnode = list->tail;
list->tail = list->tail->pre;
list->tail->next = NULL;
free(tmpnode);
tmpnode = NULL;
list->num--;
return;
}
void
FreeList(DOUBLE_LIST*list)
{
int
i;
NODE * tmpnode = NULL;
int
num;
num = list->num;
for(i = 0; i < num;i++){
tmpnode = list->head;
list->head =list->head->next;
free(tmpnode);
tmpnode = NULL;
list->num--;
}
list->head = NULL;
list->tail = NULL;
return;
}
#include "DoubleList.h" void InitDoubleList(DOUBLE_LIST *list) { list->head = NULL; list->tail = NULL; list->num = 0; return ; } void DoubleListPrint(DOUBLE_LIST list) { int i = 0; NODE * p = NULL; p = list.head; if(0 == list.num){ printf("Your List is Empty!\n"); return; } for(i = 0; i < list.num; i++){ PrintNode(p); p = p->next; } return; } void PrintNode(NODE * node) { printf("key:%d\tdate:%d\n",node->date.key,node->date.date); return; } void HeadInsterNode(DOUBLE_LIST * list, DATE date) { NODE * newnode = (NODE *)malloc(sizeof(NODE)); newnode->date.key = date.key; newnode->date.date = date.date; if(0 == list->num){ newnode->next = NULL; newnode->pre = NULL; list->head = newnode; list->tail = newnode; list->num++; return; } newnode->pre = NULL; newnode->next = list->head; list->head->pre = newnode; list->head = newnode; list->num++; return; } void XInsterNode(DOUBLE_LIST *list, DATE date, int x) { int i; NODE * newnode = NULL; NODE * prenode = NULL; NODE * nextnode = NULL; if(x < 0 ||x >= list->num){ printf("X Inster: x is not right!\n"); return; } if(0 == x){ HeadInsterNode(list, date); return; } if(list->num == x - 1){ TailInsterNode(list, date); return; } prenode = NULL; nextnode = list->head; for(i = 0; i < x; i++){ prenode = nextnode; nextnode = nextnode->next; } newnode = (NODE *)malloc(sizeof(NODE)); newnode->date.key = date.key; newnode->date.date = date.date; prenode->next = newnode; newnode->pre = prenode; newnode->next = nextnode; nextnode->pre = newnode; list->num++; return ; } void TailInsterNode(DOUBLE_LIST *list, DATE date) { NODE * newnode = (NODE *)malloc(sizeof(NODE)); newnode->date.key = date.key; newnode->date.date = date.date; if(0 == list->num){ newnode->next = NULL; newnode->pre = NULL; list->head = newnode; list->tail = newnode; list->num++; return; } newnode->next = NULL; newnode->pre = list->tail; list->tail->next = newnode; list->tail = newnode; list->num++; return; } void HeadDeleteNode(DOUBLE_LIST *list) { NODE * tmpnode = NULL; if(0 == list->num ){ printf("%s:No Node In List!\n",__FUNCTION__); return; } if(1 == list->num){ free(list->head); list->head = NULL; list->tail = NULL; list->num = 0; return ; } tmpnode = list->head; list->head = list->head->next; free(tmpnode); tmpnode = NULL; list->num--; return ; } void XDeleteNode(DOUBLE_LIST *list, KEY key) { ; } void TailDeleteNode(DOUBLE_LIST *list) { NODE * tmpnode = NULL; if(0 == list->num ){ printf("%s:No Node In List!\n",__FUNCTION__); return; } if(1 == list->num){ free(list->head); list->head = NULL; list->tail = NULL; list->num = 0; return ; } tmpnode = list->tail; list->tail = list->tail->pre; list->tail->next = NULL; free(tmpnode); tmpnode = NULL; list->num--; return; } void FreeList(DOUBLE_LIST *list) { int i; NODE * tmpnode = NULL; int num; num = list->num; for(i = 0; i < num; i++){ tmpnode = list->head; list->head = list->head->next; free(tmpnode); tmpnode = NULL; list->num--; } list->head = NULL; list->tail = NULL; return; }
main.c
[cpp]
viewplaincopyprint?
#include "DoubleList.h"
int
main(int
argc,char*argv[])
{
DOUBLE_LIST list;
int
i;
DATE date[10];
for(i = 0; i < 10;i++){
date[i].key = i + 1;
date[i].date = i + 11;
}
#if1
printf("init listtest\n");
InitDoubleList(&list);
DoubleListPrint(list);
#endif
#if1
//Head inster test
printf("headinster test\n");
HeadInsterNode(&list,date[0]);
HeadInsterNode(&list, date[1]);
HeadInsterNode(&list,date[2]);
HeadInsterNode(&list, date[3]);
HeadInsterNode(&list,date[4]);
DoubleListPrint(list);
#endif
#if1
//tail inster test
printf("\ntailinster test\n");
TailInsterNode(&list,date[0]);
TailInsterNode(&list, date[1]);
TailInsterNode(&list,date[2]);
TailInsterNode(&list, date[3]);
TailInsterNode(&list,date[4]);
DoubleListPrint(list);
#endif
#if1
//x inster test
printf("\nxinster test\n");
XInsterNode(&list, date[5],5);
XInsterNode(&list, date[6], 6);
XInsterNode(&list, date[7],7);
XInsterNode(&list, date[8], 8);
XInsterNode(&list, date[9],9);
DoubleListPrint(list);
#endif
#if1
//head delete test
printf("\nheaddelete test\n");
HeadDeleteNode(&list);
HeadDeleteNode(&list);
HeadDeleteNode(&list);
HeadDeleteNode(&list);
HeadDeleteNode(&list);
DoubleListPrint(list);
#endif
#if 1
//tail deletetest
printf("\ntail deletetest\n");
TailDeleteNode(&list);
TailDeleteNode(&list);
TailDeleteNode(&list);
TailDeleteNode(&list);
TailDeleteNode(&list);
DoubleListPrint(list);
#endif
#if 1
//free listtest
printf("\nfree listtest\n");
FreeList(&list);
DoubleListPrint(list);
#endif
return
0;
}
相关文章推荐
- ora-02049,解决一例 数据库死锁问题
- css3高级选择器使用方法
- 嵌入式 多线程条件变量相关操作
- 嵌入式 获取当前时间,并返回当前时间的秒数
- 嵌入式 Linux平台mysql相关操作
- 嵌入式 Linux_copy_to_user与copy_from_user函数解析
- 嵌入式 烧写flash的文件系统为jffs2的时出现的校验
- 嵌入式 显示软件或者程序最先建立的时间以及版本信息宏的实现
- 嵌入式 多线程socket互斥锁和条件变量实现广播包
- 嵌入式 互斥锁和条件变量基础常识
- 嵌入式 Linux C 多线程编程 互斥锁与条件变量
- 嵌入式 双向循环列表使用范例
- 嵌入式 pthread_cond_broadcast相关
- 嵌入式 双向链表实现视频预录(hi3518c)
- 嵌入式 hi3518c平台上无线网卡rtl8188eu系列网卡移植小结
- 最大熵工具包的使用
- 俞敏洪:一个优秀的人的标志和特点
- WIN7下默认网关丢失该如何解决
- 学习单片机之忌
- File类的方法