最近想学习下数据结构方面的知识,于是上网找了些双链表的实现,资料很多,但是,结果发现基本是不能用的,有些甚至编译不过去
后来,我就在一个别人写的基础上,重新修改了一遍,自己测试是好的
linux + vim + gcc
#include <stdio.h>
#include <stdlib.h>
typedef struct MyNode {
int data;
struct MyNode *left;
struct MyNode *right;
} MyNode, *pmyNode;
pmyNode CreateNode(int data)
{
pmyNode pnode = (pmyNode)malloc(sizeof(MyNode));
pnode->data = data;
pnode->left = pnode->right = pnode;
return pnode;
}
pmyNode CreateList(int head)
{
pmyNode pnode = (pmyNode)malloc(sizeof(MyNode));
pnode->data = head;
pnode->left = pnode->right = pnode;
return pnode;
}
pmyNode InsertNode(pmyNode node, int data)
{
pmyNode pnode = CreateNode(data);
pmyNode ptmp = node->left;
node->left->right = pnode;
pnode->right = node;
node->left = pnode;
pnode->left = ptmp;
return node;
}
pmyNode FindNode(pmyNode node, int data)
{
pmyNode pnode = node->right;
while (pnode != node) {
if(pnode->data == data) return pnode;
pnode = pnode->right;
}
return NULL;
}
pmyNode DelectNode(pmyNode node, int data)
{
pmyNode ptmp;
pmyNode pnode = FindNode(node, data);
if(NULL == pnode) return NULL;
ptmp = pnode->left;
ptmp->right = pnode->right;
pnode->right->left = ptmp;
free(pnode);
return node;
}
int GetLenght(pmyNode node)
{
int nCount = 0;
pmyNode pnode = node->right;
while (pnode != node) {
nCount++;
pnode = pnode->right;
}
return nCount;
}
void PrintList(pmyNode node)
{
pmyNode pnode;
if(NULL == node) return;
pnode = node->right;
while (pnode != node && pnode != NULL) {
printf("%d ", pnode->data);
pnode = pnode->right;
}
printf("\n");
}
void ReverPrintList(pmyNode node)
{
pmyNode pnode;
if(NULL == node) return;
pnode = node->left;
while (pnode != node) {
printf("%d ", pnode->data);
pnode = pnode->left;
}
printf("\n");
}
void DelectList(pmyNode node)
{
if(NULL == node) return;
pmyNode pnode = node->right;
pmyNode ptmp;
while (pnode != node) {
ptmp = pnode;
pnode = pnode->right;
free(ptmp);
}
free(node);
}
void ClearList(pmyNode node)
{
if(NULL == node) return;
pmyNode pnode = node->right;
pmyNode ptmp;
while (pnode != node) {
ptmp = pnode;
pnode = pnode->right;
free(ptmp);
}
node->right = node->left = node;
}
int main(void)
{
int choose;
int data;
int flag = 1;
pmyNode pnode;
pmyNode list = CreateList(0);
while (flag) {
printf("======== Main Menu ========\n");
printf("1 InsertNode\n");
printf("2 DelectNode\n");
printf("3 FindNode\n");
printf("4 GetLenght\n");
printf("5 PrintList\n");
printf("6 ReverPrintList\n");
printf("7 ClearList\n");
printf("0 quit\n\n");
printf("===========================\n");
scanf("%d", &choose);
switch(choose) {
case 1:
printf("Input the data to insert: ");
scanf("%d", &data);
list = InsertNode(list, data);
break;
case 2:
printf("Input the data to delete: ");
scanf("%d", &data);
DelectNode(list, data);
break;
case 3:
printf("Input the data to find: ");
scanf("%d", &data);
pnode = FindNode(list, data);
if(NULL != pnode) printf("Find succeed..!!\n");
else printf("Find failed..!!\n");
break;
case 4:
printf("The list's length is %d", GetLenght(list));
break;
case 5:
PrintList(list);
break;
case 6:
ReverPrintList(list);
break;
case 7:
ClearList(list);
break;
case 0:
DelectList(list);
flag = 0;
break;
default:
printf("Please select 0-7 numbers..!!\n");
break;
}
printf("\n");
}
return 0;
}