【中级】双链表基本操作

本文介绍了一个双链表数据结构的实现方法,包括创建、插入、删除节点等基本操作,并通过一个示例展示了如何利用双链表进行大整数的加法运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/******************************************************************************

  Copyright (C), 2001-2011, Huawei Tech. Co., Ltd.

 ******************************************************************************
  File Name     : 
  Version       : 
  Author        : 
  Created       : 2012/03/12
  Last Modified :
  Description   : 
  Function List :
              
  History       :
  1.Date        : 2012/03/12
    Author      : 
    Modification: Created file

******************************************************************************/

#include <stdlib.h>
#include <string.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==null)
		return;
	struct strlnode *temp=*p,*save=*p,*t;
	int count=0;
	while(temp->pnext!=null)
	{
		count++;
		temp=temp->pnext;
	}
	if(i<0||i>count+1)
		return;

	/* 代码在这里实现 */
	struct strlnode *q=(strlnode *)malloc(sizeof(strlnode));
	
	q->data=x;
    
	if(i==0)
	{
		q->pnext=save;
		q->plast=null;
		save->plast=q;
		*p=q;
		return;
	}
	else
	if(i==count+1)
	{
		q->pnext=null;
		q->plast=temp;
		temp->pnext=q;
		return;
	}
	else
	{
	count=0;
		 while(count!=i)
		{
		  count++;
		  t=save;
		  save=save->pnext;
		  
		}
		 q->pnext=t->pnext;
		 save->plast=q;
		 q->plast=t;
		 t->pnext=q;
		 return;

	}

}

void deletenode(struct strlnode **p, int i) /* 删除链表中第i个节点 */
{
	/* 代码在这里实现 */
	if(*p==null)
		return;
	struct strlnode *temp=*p,*save=*p,*t;
	int count=0;
	while(temp->pnext!=null)
	{
		count++;
		temp=temp->pnext;
	}
	if(i<0||i>count+1)
		return;
	// delete the node 
	if(i==0)
	{  if(count==0)
	    *p=null;
		save=save->pnext;
		save->plast=null;
        *p=save;
		return;
	}
	else
	if(i==count+1)
	{
		t=temp->plast;
		t->pnext=null;
		free(temp);

		return;
	}
	else
	{
	    count=0;
		 while(count!=i)
		{
		  count++;
		  t=save;
		  save=save->pnext;
		  
		}
		 save->pnext->plast=save->plast;
		 t->pnext=save->pnext;
		 free(save);
		 return;

	}



}

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


	return nodenum+1;
}




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;
}


void bignumberplus(struct strlnode **plus, struct strlnode **p, struct strlnode **q) /* 使用链表实现大整数相加 */
{
	/* 代码在这里实现 */
 
	int lenp = getnodenum(p);
	int lenq = getnodenum(q);
	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;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值