OJ 系列之【中级】双链表基本操作

#include <stdlib.h>

#define null 0
#define MAXSIZE 50

struct strlnode {
	int data;
	struct strlnode *plast;
	struct strlnode *pnext;
};

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;

	return;
}

void insertnode(struct strlnode **p, int i, int x) /* 在链表第i个位置插入数据等于x的节点 */
{
	/* 代码在这里实现 */
	if(!*p||!p)
		return;
	
	struct strlnode *ptemp=*p,*psave=*p;
	struct strlnode *t = NULL;
	int pnodeLen=0;
	
	while(ptemp->pnext!=NULL) {
		pnodeLen++;
		ptemp=ptemp->pnext;
	}
	if(i<0||i>pnodeLen+1)
		return;

	/*申请该节点的地址*/
	struct strlnode *q=(strlnode *)malloc(sizeof(strlnode));
	
	q->data=x;
	/*插入为第一个位置时候*/
	if(i==0) {
		q->pnext=psave;
		q->plast=NULL;
		psave->plast=q;
		*p=q;
		
		return;
	/*最后位置*/	
	} else if(i==pnodeLen+1) {
		q->pnext = NULL;
		q->plast = ptemp;
		ptemp->pnext = q;
		
		return;
	/*中间位置*/
	} else {
		int pnodeCur=0;
		
		while(pnodeCur!=i) {
			pnodeCur++;
			t = psave;
			psave = psave->pnext;
		}
		q->pnext=t->pnext;
		psave->plast=q;
		q->plast=t;
		t->pnext=q;
		
		return;
	}

	
}

void deletenode(struct strlnode **p, int i) /* 删除链表中第i个节点 */
{
	/* 代码在这里实现 */	
	if(!*p||!p)
		return;
	if(i<0)
		return;

	struct strlnode *ptemp=*p,*psave=*p;
	struct strlnode *t = NULL;
	int pnodeLen=0;
	
	while(ptemp->pnext!=NULL) {
		pnodeLen++;
		ptemp=ptemp->pnext;
	}
	if(i<0||i>pnodeLen)
		return;

	/*删除为第一个位置时候*/
	if(i==0) {
		if(pnodeLen==0) 
			*p = NULL;
		t = psave->pnext;
		t->plast = NULL;
		free(psave);
		psave = NULL;
		*p = t; /*注意这里,记得返回*/
		
		return;

	/*其它位置*/
	} else {
		int pnodeCur=0;
		
		while(pnodeCur!=i) {
			pnodeCur++;
			t = psave;
			psave = psave->pnext;
		}
		t->pnext = psave->pnext;
		(psave->pnext)->plast = t;
		free(psave);

		return;
	}
}

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

/* 使用链表实现大整数相加 */
void bignumberplus(struct strlnode **plus, struct strlnode **p, struct strlnode **q)
{
	int lenp = getnodenum(p);
	int lenq = getnodenum(q);
	/*初始化为0很重要,便于后面的处理*/
	int a[10000] = {0};
	int b[10000] = {0};
	/*看哪个整数更大*/
	int len = lenp > lenq ? lenp : lenq;
	struct strlnode *pl = *p;
	struct strlnode *ql = *q;
	/*将链表数据取出来,分别存入不同数组中*/
	for(int i = lenp - 1; i>= 0; i--) {
		a[i] = pl->data;
		pl = pl ->pnext;
	}
	for(int i = lenq - 1; i>= 0; i--) {
		b[i] = ql->data;
		ql = ql ->pnext;
	}
	for(int i = 0; i < len; i++) {
		a[i] = a[i] + b[i];
		if(a[i]>=10) {
			a[i+1] += a[i] / 10;
			a[i] %= 10;
		}
	}
	
	if(a[len] != 0)//最后进位后会多出来的
		len++;
	/*从高到低依次存储*/
	(*plus)->data = a[len -1];
	int j = 1;
	for(int  i = len - 2; i>= 0 ;i--) {
		int data = a[i];
		insertnode(plus, j, data);
		j++;
	}
	
	return;
}

/* 将数组写入链表中,链表中的数据的先后顺序和数组中的顺序要保持一致 */
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;
}







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂奔的乌龟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值