您的位置:首页 > 其它

嵌入式 双向链表的头尾中插入法以及释放

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
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: