- 不用那么多废话,双向链表的插入、删除、查询等操作,欢迎大家提出修改意见:
-
doubleList.h
-
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
typedef struct Node
-
{
-
int data;
-
struct Node * next;
-
struct Node * prior;
-
}NODE, *PNODE;
-
-
PNODE create_list(void); //创建节点
-
void traverse_list(PNODE pHead); //遍历链表(打印)
-
int length_list(PNODE pHead); //求链表长度
-
void sort_list(PNODE pHead); //排序
-
void insert_list(PNODE pHead, int val); //插入节点
-
void delete_list(PNODE pHead, int pos); //删除节点
-
int find_list(PNODE pHead, int val); //查找元素
-
-
-
PNODE create_list(void) //从文件中读取数据链表的初始化值
-
{
-
int i, len;
-
int value;
-
char str[10];
-
FILE *pf;
-
PNODE pTail, pNew;
-
PNODE pHead = (PNODE)malloc(sizeof(NODE));
-
if (NULL == pHead)
-
{
-
fprintf(stdout, "Memory molloc erroe !\n");
-
exit(-1);
-
}
-
pTail = pHead;
-
pTail->next = NULL;
-
-
pf = fopen("./data.txt", "r");
-
/*while(fgets(str, 10, pf) != NULL)
-
{
-
printf("str = %s \n", str);
-
}*/
-
len = 0;
-
while (1)
-
{
-
fgets(str, 10, pf);
-
if (feof(pf))
-
{
-
fprintf(stdout, "file read finish !\n");
-
break;
-
}
-
sscanf(str, "%d", &value);
-
len++; // 记录初始化多少个节点
-
-
pNew = (PNODE)malloc(sizeof(NODE));
-
if (NULL == pNew)
-
{
-
fprintf(stdout, "New Node Memory's malloc error !\n");
-
exit(-1);
-
}
-
/*创建一个新的节点*/
-
pNew->data = value;
-
-
pTail->next = pNew;
-
pNew->prior = pTail;
-
pTail = pNew;
-
pTail->next = NULL;
-
}
-
fprintf(stdout, "Initialize %d node !\n", len);
-
return pHead;
-
}
-
-
void traverse_list(PNODE pHead)
-
{
-
if (NULL == pHead)
-
{
-
fprintf(stdout, "List is NULL !\n");
-
exit(-1);
-
}
-
while(pHead != NULL)
-
{
-
printf("%d\n", pHead->data);
-
pHead = pHead->next;
-
}
-
return;
-
}
-
-
int length_list(PNODE pHead)
-
{
-
int length = 0;
-
PNODE pNode = NULL;
-
pNode = pHead;
-
if (NULL == pNode)
-
{
-
fprintf(stdout, "List is NULL !\n");
-
exit(-1);
-
}
-
while(pNode)
-
{
-
length++;
-
pNode = pNode->next;
-
}
-
return length;
-
}
-
-
void sort_list(PNODE pHead)
-
{
-
int length;
-
int i, j;
-
int tmp;
-
PNODE pnode = NULL;
-
PNODE qnode = NULL;
-
printf("sort_list ... \n");
-
-
if(NULL == pHead)
-
{
-
fprintf(stdout, "pHead is NULL !\n");
-
exit(-1);
-
}
-
length = length_list(pHead);
-
-
fprintf(stdout, "List Length is %d\n", length);
-
-
for(i = 0, pnode = pHead; i < length; i++, pnode = pnode->next)
-
{
-
for(j = i+1, qnode = pnode->next; j < length; j++, qnode = qnode->next)
-
{
-
if (pnode->data > qnode->data)
-
{
-
tmp = pnode->data;
-
pnode->data = qnode->data;
-
qnode->data = tmp;
-
}
-
-
}
-
}
-
return;
-
}
-
-
void insert_list(PNODE pHead, int value)
-
{
-
PNODE pNew;
-
PNODE pTail = NULL;
-
-
if (NULL == pHead)
-
{
-
fprintf(stdout, "pHead is NULL !\n");
-
exit(-1);
-
}
-
-
pNew = (PNODE)malloc(sizeof(NODE));
-
if (NULL == pNew)
-
{
-
fprintf(stdout, "in_node malloc error !\n");
-
exit(-1);
-
}
-
memset(pNew, 0, sizeof(NODE));
-
pNew->data = value;
-
-
pTail = pHead;
-
while(pTail->next != NULL)
-
{
-
pTail = pTail->next;
-
}
-
pTail->next = pNew;
-
pNew->prior = pTail;
-
pTail = pNew;
-
pTail->next = NULL;
-
fprintf(stdout, "Success insert new_node, value is %d\n", pNew->data);
-
//traverse_list(pHead);
-
return;
-
}
-
-
-
void delete_list(PNODE pHead, int pos)
-
{
-
int length, i = 1;
-
PNODE pDelNode = NULL;
-
PNODE pPreNode = NULL;
-
-
fprintf(stdout, "Delete node !\n");
-
if (NULL == pHead)
-
{
-
fprintf(stdout, "pHead is NULL !\n");
-
exit(-1);
-
}
-
length = length_list(pHead);
-
if (length < pos)
-
{
-
fprintf(stdout, "delete's pos is error !\n");
-
exit(-1);
-
}
-
pPreNode = pHead;
-
pDelNode = pHead;
-
while(pDelNode->next != NULL)
-
{
-
if(i == pos)
-
{
-
break;
-
}
-
i ++;
-
pPreNode = pDelNode;
-
pDelNode = pDelNode->next;
-
}
-
pPreNode->next = pDelNode->next;
-
pDelNode->next->prior = pPreNode;
-
free(pDelNode);
-
return;
-
}
-
-
int find_list(PNODE pHead, int value)
-
{
-
PNODE pFindNode = NULL;
-
int pos = 1;
-
if (NULL == pHead)
-
{
-
fprintf(stdout, "List is NULL !\n");
-
exit(-1);
-
}
-
pFindNode = pHead;
-
while(pFindNode->next != NULL)
-
{
-
if (pFindNode->data == value)
-
{
-
return pos;
-
}
-
pFindNode = pFindNode->next;
-
pos++;
-
}
-
}
-
-
-
-
测试文件doubleList.c
-
-
#include "../include/doubleList.h"
-
-
int main()
-
{
-
int length;
-
PNODE pHead = NULL;
-
pHead = create_list();
-
traverse_list(pHead);
-
length = length_list(pHead);
-
fprintf(stdout, "Total %d nodes !\n", length);
-
insert_list(pHead, 88);
-
sort_list(pHead);
-
traverse_list(pHead);
-
fprintf(stdout, "Total %d nodes !\n", length);
-
delete_list(pHead, 4);
-
traverse_list(pHead);
-
fprintf(stdout, "Total %d nodes !\n", length);
-
-
fprintf(stdout, "FindPos is %d \n", find_list(pHead, 0));
-
return 0;
-
}
-
-
其中文件中用到的data.txt数据文件只是一个简单的文件,里面包含了我初始化的一些数据项。数据格式(附件包含有程序和数据): doubleList.rar
-
11
-
22
-
33
-
4
-
45
-
34
运行环境和命令:
Linux
gcc -g -o doubleList doublist.c