双链表基本操作
2016-07-05 22:03
148 查看
/******************************************************************************
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;
}
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;
}