OJ 系列之【中级】双链表基本操作
来源:互联网 发布:ubuntu查看组的用户 编辑:程序博客网 时间:2024/05/16 09:37
#include <stdlib.h>#define null 0#define MAXSIZE 50struct 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 0
- OJ 系列之【中级】双链表基本操作
- OJ 系列之字符串基本操作
- 【中级】双链表基本操作
- SQL系列之基本操作
- 华为oj中级 二维数组操作
- 华为OJ之中级篇Ⅰ
- 华为OJ之中级篇Ⅱ
- 华为oj之【中级】单词倒排
- SI446x系列之路--基本操作
- Git 简洁教程之 中级操作
- 【OJ中级】坐标移动
- 华为oj中级 笔画
- 华为oj中级 线性插值
- 转 -- Oracle RAC系列之:ASM基本操作维护
- Oracle RAC系列之:ASM基本操作维护
- Oracle RAC系列之:ASM基本操作维护
- Oracle RAC系列之:ASM基本操作维护
- Oracle RAC系列之:ASM基本操作维护
- JQuery的父、子、兄弟节点查找方法
- coredata 查找,增,删,改
- 深圳郎仁科技iobd2利用代码
- HI258摄像头旋转配置问题
- 监听返回键并退出所有Activity示例
- OJ 系列之【中级】双链表基本操作
- cc2530-基于contiki系统读取DHT11问题总结
- not allowed to access to crontab because of pam configuration
- Ubuntu android studio 创建虚拟设备问题
- sqlserver删除所有表(表结构和数据)
- java 常见的2种单例模式
- Vert.x 3学习笔记---06
- PowerDesigner使用教程
- js与jquery常用方法总结