双链表基本操作

详细描述:

  • 数据结构(结构体strlnode)定义:

  • data保存节点的值

  • plast指向上一个节点

  • pnext指向下一个节点

  • 下面的所有接口实现中,头结点也表示一个保存实际值的节点

  • 实现接口,每个接口实现1个基本操作:

  • voidcreate(structstrlnode **p,intx):创建1个头节点,头节点表示第0个节点,x表示头节点保存的值,该函数已经实现。

  • voidinsertnode(structstrlnode **p,inti,intx):在链表的第i个位置插入数值为x的节点,需考虑i异常。

  • voiddeletenode(structstrlnode **p,inti):删除链表的第i个节点,需释放该节点占用的内存,需考虑i异常。

  • intgetnodenum(structstrlnode **p):获取链表的节点个数,空链表返回0。

  • voidbignumberplus(structstrlnode **plus,structstrlnode **p,structstrlnode **q):实现两个大整数相加,链表p和链表q中存储两个大整数,相加后的整数存储到plus中。

  • voidreadtolnode(structstrlnode **p,int*a,intsize):将数组a中的数据存储到链表p当中,size为存储的数值的个数。该函数已经实现,举例如下:

123+4567=4690

p: [plast|data=1|pnext]<-> [plast|data=2|pnext] <-> [plast|data=3|pnext]<-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><->

q:[plast|data=4|pnext]<-> [plast|data=5|pnext] <-> [plast|data=6|pnext] <-> [plast|data=7|pnext]<-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><->

plus:[plast|data=4|pnext]<-> [plast|data=6|pnext] <-> [plast|data=9|pnext] <-> [plast|data=0|pnext]<-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><->

a[3] = {1,2,3},存储到链表中的顺序和在数组中顺序保持一致:

[plast|data=1|pnext]<-> [plast|data=2|pnext] <-> [plast|data=3|pnext]<-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><->

  • voidwritetosqlist(int*a,structstrlnode *p):将链表p中的数据存储到数组a中。该函数已经实现,举例如下:

[plast|data=1|pnext]<-> [plast|data=2|pnext] <-> [plast|data=3|pnext]<-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><->,存储到数组中的顺序和在链表中的顺序保持一致:a[3] = {1,2,3}

  • 题目框架中有7个参考用例,其它用例请执行编写

  • 重要说明:代码没有实现之前跑用例会访问越界,不用急于解决,代码实现之后问题消失。


#include <stdlib.h>
#include <iostream>
using namespace std;


#define null 0
#define MAXSIZE 50


struct strlnode
{
int data;
struct strlnode *plast;
struct strlnode *pnext;
};
void readtolnode(struct strlnode **p, int *a, int size);
int getnodenum(struct strlnode **p);  
void writetosqlist(int *a, struct strlnode *p);
void readtolnode(struct strlnode **p, int *a, int size);


void create(struct strlnode **p, int x)  /*创建双链表(表头节点)*/
{
struct strlnode *q;


q = (struct strlnode *)malloc(sizeof(struct strlnode));
q->data = x;
q->plast = null;
q->pnext = null;


*p = q;

}


void insertnode(struct strlnode **p, int i, int x) /* 在链表第i个位置插入数据等于x的节点 */
{
/* 代码在这里实现 */
struct strlnode *q, *q0;


q = (struct strlnode *)malloc(sizeof(struct strlnode));
q0 = (struct strlnode *)malloc(sizeof(struct strlnode));
q0 = *p;

q->data = x;
int number = getnodenum(&*p);
if ((i<= number) && (i > 0))
{
for (int j = 0; j < i-1; j++)
{
q0 = q0->pnext;
}
if (q0->pnext == NULL)
{
q->pnext = q0->pnext;
q0->pnext = q;
q->plast = q0;
}
else
if (q0->pnext != NULL)
{
q->pnext = q0->pnext;
q->plast = q0;
q0->pnext->plast = q;
q0->pnext = q;
}
}

else if (i == 0)
{
q->pnext = q0;
q0->plast = q;
*p = q;
}
}


void deletenode(struct strlnode **p, int i) /* 删除链表中第i个节点 */
{
/* 代码在这里实现 */
struct strlnode *q;
q = (struct strlnode *)malloc(sizeof(struct strlnode));
q = *p;
int number = getnodenum(&*p);


if (i <= number)
{
for (int j = 0; j < i; j++)
{
q= q->pnext;
}
}
if (q->pnext == NULL)
{
q->plast->pnext = NULL;
free(q);
}
else if(q->plast == NULL)
{
q->pnext->plast = q->plast;
free(q);
}
else if((q->plast != NULL) && (q->pnext != NULL))
{
q->plast->pnext = q->pnext;
q->pnext->plast = q->plast;
free(q);
}

}


int getnodenum(struct strlnode **p)  /*获取链表中节点个数*/
{
int nodenum = 1;
/* 代码在这里实现 */
struct strlnode *q;
q = *p;
while ((q->pnext!=*p) && (q->pnext!=NULL))
{
q = q->pnext;
nodenum ++;
}
return nodenum;
}


void bignumberplus(struct strlnode **plus, struct strlnode **p, struct strlnode **q) /* 使用链表实现大整数相加 */
{
/* 代码在这里实现 */
int num1, num2, maxnum;
num1 = getnodenum(p);
num2 = getnodenum(q);
maxnum = (num1>num2?num1: num2);


struct strlnode *tempp;
struct strlnode *tempq;


tempp = *p;
tempq = *q;


int *a = new int [num1];
int *b = new int [num2];
int *c = new int [maxnum];
int flag = 0;
int temp_value;


writetosqlist(a, &*tempp);
writetosqlist(b, &*tempq);


int i, j, k;
for (i = num1-1, j = num2-1, k = maxnum-1; (i >= 0) && (j >= 0) && (k >= 0); i--, j--, k--)
{
temp_value = a[i] + b[j] + flag;
if (temp_value >= 10)
{
flag = 1;
}
if (temp_value < 10)
{
flag = 0;
}
c[k] = temp_value % 10;
}


if (i > 0)
{
for (; i>=0; i--,k--)
{
c[k] = a[i] + flag;
flag = 0;
}



}


if (j > 0)
{
for (; j>=0; j--,k--)
{
c[k] = b[j] + flag;
flag = 0;
}
}


readtolnode(plus, c, maxnum);


if (flag == 1)
{
insertnode(plus, 0, 1);
}




delete []a;
delete []b;
delete []c;
}




void readtolnode(struct strlnode **p, int *a, int size)  /* 将数组写入链表中,链表中的数据的先后顺序和数组中的顺序要保持一致 */
{
int j = 0;
int data = 0;
struct strlnode *s = *p;


s->data = *(a + (size-1));


for(j = 2; j < (size+1); j++)
{
data = *(a + (size-j));
insertnode(p, 0, data);
}


return;
}




void writetosqlist(int *a, struct strlnode *p)  /* 将链表写入数组中,数组中的数据的先后顺序和链表中的顺序要保持一致 */
{
int j = 0;
struct strlnode *s = p;


while(s != null)
{
*(a + j) = s->data;
s = s->pnext;
j++;
}


return;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值