链表源码之双向链表

===============================================================================

C 数据结构双向链表

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
typedef struct PNode
{
          int data;
          struct PNode *next,*last;
}Node;
void CreateList(Node *&N)
{
          N =(Node *)malloc(sizeof(Node));
          N->data = 0;
          N->next = N->last = NULL;
}
int GetLength(Node *N)
{
          assert(N);
          int i = 0;
          Node *p = N;
          while(p != NULL)
          {
                    p = p->next;
                    i++;
          }
          return i;
}
void DisplayList(Node *N)
{
          if(N == NULL)
                    return;
          Node *p = N->next;
          while(p != NULL)
          {
                    printf("%d/n",p->data);
                    p = p->next;
          }
}
int InsertList(Node *N,int n,int x)
{
          assert(N);
          int i = 1;
          Node *p = N;
          Node *L = (Node *)malloc(sizeof(Node));
          L->next = L->last = NULL;
          L->data = x;


          if(n <=0 || n > GetLength(N))
          {
                    printf("please enter a right number!!");
                    return 0;
          }


          while(i < n)
          {
                    p = p->next;
                    i++;
          }
 
          L->next = p->next;
          if(p->next != NULL)      //注意这里的条件判断 
                    p->next->last = L;     


          p->next = L;

          L->last = p;


          return 1;
}
//注:无论是插入还是删除都是找要插入或删除的前一个结点
int DeletetList(Node *N,int n)
{
          assert(N);
          int i = 1;
          Node *p = N,*q;
          if(n <=0 || n >= GetLength(N))
          {
                    printf("please enter a right number!!");
                    return 0;
          }
          while(i < n)
          {
                    p = p->next;
                    i++;
          }
          q = p->next;
          p->next = q->next;


          if(q->next != NULL)
                    p->next->last = q;
          free(q);
          return 1;
}
void main()
{
          Node *Node = NULL;
          int a[]={1, 2, 3, 4, 5};
          CreateList(Node);
          printf("%d/n",GetLength(Node));
         /* if(Node != NULL)
                    DisplayList(Node);*/
          if(InsertList(Node,1,2))
                    printf("Insert success!!/n");


          if(InsertList(Node,1,324))
                    printf("Insert success!!/n");


          if(InsertList(Node,1,435))
                    printf("Insert success!!/n");


          if(InsertList(Node,1,567))
                    printf("Insert success!!/n");
          DisplayList(Node);


          if(DeletetList(Node,3))
                    printf("Delete success!!/n");
          DisplayList(Node);
}

===============================================================================

C数据结构双向循环链表

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
typedef struct PNode
{
          int data;
          struct PNode *next,*last;
}Node;
void CreateList(Node *&N)
{
          N =(Node *)malloc(sizeof(Node));
          N->data = 0;
          N->next = N->last = N;
}
int GetLength(Node *N)
{
          assert(N);
          int i = 1;
          Node *p = N->next;
          while(p != N)
          {
                    p = p->next;
                    i++;
          }
          return i;
}
void DisplayList(Node *N)
{
          if(N == NULL)
                    return;
          Node *p = N->next;
          while(p != N)
          {
                    printf("%d/n",p->data);
                    p = p->next;
          }
}
int InsertList(Node *N,int n,int x)
{
          assert(N);
          int i = 1;
          Node *p = N;
          Node *L = (Node *)malloc(sizeof(Node));
          L->next = L->last = NULL;
          L->data = x;
          if(n <=0 || n > GetLength(N))
          {
                    printf("please enter a right number!!");
                    return 0;
          }
          while(i < n)
          {
                    p = p->next;
                    i++;
          }
 
          L->next = p->next;
          if(p->next != N)      //注意这里的条件判断 
                    p->next->last = L;     


          p->next = L;

          L->last = p;
          return 1;
}
//注:无论是插入还是删除都是找要插入或删除的前一个结点
int DeletetList(Node *N,int n)
{
          assert(N);
          int i = 1;
          Node *p = N,*q;
          if(n <=0 || n >= GetLength(N))
          {
                    printf("please enter a right number!!");
                    return 0;
          }
          while(i < n)
          {
                    p = p->next;
                    i++;
          }
          q = p->next;
          p->next = q->next;
          if(q->next != NULL)
                    p->next->last = q;


          free(q);
          return 1;
}
void main()
{
          Node *Node = NULL;
          int a[]={1, 2, 3, 4, 5};
          CreateList(Node);
          if(Node != NULL)
                    printf("%d/n",GetLength(Node));
         /* if(Node != NULL)
                    DisplayList(Node);*/
          if(InsertList(Node,1,2))
                    printf("Insert success!!/n");
          if(InsertList(Node,1,324))
                    printf("Insert success!!/n");
          if(InsertList(Node,1,435))
                    printf("Insert success!!/n");
          if(InsertList(Node,1,567))
                    printf("Insert success!!/n");
          DisplayList(Node);


          if(DeletetList(Node,3))
                    printf("Delete success!!/n");
          DisplayList(Node);
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值