双链表

一个完整的双向链表程序(C语言版) 

#include <stdio.h>
#include <stdlib.h>

typedef struct tagDbNode
{
int data;
struct tagDbNode * left;
struct tagDbNode * right;
} DbNode, * pdbNode;


//创建结点
pdbNode CreateNode(int data)
{
pdbNode pnode = (pdbNode)malloc(sizeof(DbNode));
pnode->data = data;
pnode->left = pnode->right = pnode;    //创建新结点时,让其前驱和后继指针都指向自身

return pnode;
}

//=========================================================================

//创建链表
pdbNode CreateList(int head) //参数给出表头结点数据 (表头结点不作为存放有意义数据的结点)
{
pdbNode pnode = (pdbNode)malloc(sizeof(DbNode));
pnode->data = head;
pnode->left = pnode->right = pnode;

return pnode;
}

//插入新结点,总是在表尾插入; 返回表头结点
pdbNode InsertNode(pdbNode node, int data) // 参数1是链表的表头结点,参数2是要插入的结点(结点数据为data)
{
pdbNode pnode = CreateNode(data);
pdbNode ptmp = node->left;

// 从左到右恢复链接
node->left->right = pnode;
pnode->right = node;

// 从右到左恢复链接
node->left = pnode;
pnode->left = ptmp;

return node;
}

//查找结点,成功则返回满足条件的结点指针,否则返回NULL
pdbNode FindNode(pdbNode node, int data) // 参数1是链表的表头结点,参数2是要查找的结点(其中结点数据为data)
{
pdbNode pnode = node->right;
while (pnode->right != node && pnode->data != data)
{
   pnode = pnode->right;
}

if (pnode->right == node) return NULL;

return pnode;
}

//删除满足指定条件的结点, 返回表头结点, 删除失败返回NULL(失败的原因是不存在该结点)
pdbNode DeleteNode(pdbNode node, int data) // 参数1是链表的表头结点,参数2是要删除的结点(其中结点数据为data)
{
pdbNode ptmp;
pdbNode 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 GetLength(pdbNode node) // 参数为链表的表头结点
{
int nCount = 0;
pdbNode pnode = node->right;

while (pnode->right != node)
{
   nCount++;
}

return nCount;
}

//打印整个链表
void PrintList(pdbNode node) // 参数为链表的表头结点
{
pdbNode pnode;

if (NULL == node) return;

pnode= node->right;

while (pnode != node)
{
   printf("%d   ", pnode->data);
   pnode = pnode ->right;
}

printf("\n");
}

//将链表反序打印
void ReverPrint(pdbNode node) //参数为链表的表头结点
{
pdbNode pnode;

if (NULL == node) return;

pnode= node->left;

while (pnode != node)
{
   printf("%d   ", pnode->data);
   pnode = pnode->left;
}

printf("\n");
}

//删除链表
void DeleteList(pdbNode node) //参数为链表表头结点
{
pdbNode pnode = node->right;
pdbNode ptmp;

if (NULL == node) return; 

while (pnode != node)
{
   ptmp = pnode->right;
   free(pnode);
   pnode = ptmp;
}

free(node);
}

//测试程序

#include <stdio.h>
#include <stdlib.h>
#include "dblist.h"

#define FALSE 0
#define TRUE 1

typedef unsigned int bool;


void main()
{
int nChoose;
int data;
//int length;
bool bFlag = FALSE;
pdbNode pnode;
pdbNode list = CreateList(0);

while(FALSE == bFlag)
{
   printf("Main Menu\n");
   printf("1. Insert\n");
   printf("2. Delete Node\n");
   printf("3. Find\n");
   printf("4. Length\n");
   printf("5. Positive Print\n");
   printf("6. Negative Print\n");
   printf("7. Delete List\n");
   printf("0. quit\n\n");
  
   scanf("%d", &nChoose);
  
   switch(nChoose)
   {
   case 1:
    printf("Input the data to insert:");
    scanf("%d", &data);
    list = InsertNode(list, data);
    PrintList(list);
    break;
   case 2: 
    printf("Input the data to delete:");
    scanf("%d", &data);
    DeleteNode(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", GetLength(list));
    break;
   case 5:
    PrintList(list);
    break;
   case 6:
    ReverPrint(list);
    break;
   case 7:
    DeleteList(list);
    break;
   case 0:
    DeleteList(list);
    bFlag = TRUE;
   }
}
}



第二版本

双向链表的逆序:
typedef struct dnode
{
        int data;
        struct dnode *prior;
        struct dnode *next;
}DNode;
DNode *reverseDNode(DNode *head)
{
        DNode *p = head->next;
        DNode *q = p->next;
        DNode *r = q->next;
        if(head == NULL || head->next ==NULL)
                return head;
        p->next = NULL;
        while(r != NULL)
        {
                q->next = p;
                p->prior = q;
                p = q;
                q = r;
                r = r->next;
        }
        q->next = p;
        p->prior = q;
        head->next = q;
        q->prior = head;
        return head;
}
双向链表的销毁:
typedef struct dnode
{
        int data;
        struct dnode *prior;
        struct dnode *next;
}DNode;
void destroryDNode(DNode *head)
{
        DNode *p = head->next;
        DNode *q = NULL;
        DNode *r = NULL;
        for(q=p; q->next!=NULL; q=r)
        {
                r = q->next;
                free(q);
        }
        free(r);
        free(head);
}
双链表的插入:
typedef struct dnode
{
        int data;
        struct dnode *prior;
        struct dnode *next;
}DNode;
/*在所指定位置之前插入*/
void before_insert(DNode *head, int position, int value)
{
        DNode *p = head;
        DNode *q = NULL;
        int i = 0;
        while(p && i++ < position - 1)
                p = p->next;
        if((q = (DNode *)malloc(sizeof(DNode)))==NULL)
        {
                printf("分配内存失败n");
                exit(0);
        }
        q->data = value;
        q->next = p->next;
        p->next = q;
        p->next->prior = q;
        q->prior = p;
}
/*在所指定位置之后插入*/
void behind_insert(DNode *head, int position, int value)
{
        DNode *p = head;
        DNode *q = NULL;
        int i = 0;
        while(p && i++ < position)
                p = p->next;
        if((q = (DNode *)malloc(sizeof(DNode)))==NULL)
        {
                printf("分配内存失败n");
                exit(0);
        }
        q->data = value;
        q->next = p->next;
        p->next = q;
        p->next->prior = q;
        q->prior = p;
}
文章出处:http://www.diybl.com/course/3_program/c++/cppxl/2008107/148954.html
双链表的查找:
typedef struct dnode
{
        int data;
        struct dnode *prior;
        struct dnode *next;
}DNode;
/*返回value值在链表中的位置,没有该值将返回0*/
int searchDNode(DNode *head, int value)
{
        int i = 1;
        DNode *p = head->next;
        while(p)
        {
                if(p->data == value)
                        return i;
                else
                {
                        p = p->next;
                        i++;
                }
        }
        return 0;
}
双链表的删除
typedef struct dnode
{
        int data;
        struct dnode *prior;
        struct dnode *next;
}DNode;
void node_delete(DNode *head, int position)
{
        DNode *p = head;
        DNode *q = NULL;
        int i = 0;
        while(p && i++ < position)
                p = p->next;
        p->prior->next = p->next;
        p->next->prior = p->prior;
        free(p);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值