#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 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 || *p == NULL ) { cout<<"error!"<<endl; return ; } if (i < 0) { printf("i must be larger than 0 !"); return ; } struct strlnode *pTemp = *p;//指向头结点 struct strlnode *newNode; newNode = (struct strlnode *)malloc(sizeof(struct strlnode)); newNode->data = x; if (0 == i)//插入到头结点 { newNode->pnext = pTemp; pTemp->plast = newNode; newNode->plast = NULL; *p = newNode; return; } int k = i; while(k > 1 && pTemp->pnext != NULL)//查找第i个结点 { pTemp = pTemp->pnext; k--; } if (pTemp->pnext == NULL) { pTemp ->pnext = newNode; newNode->plast = pTemp; newNode->pnext = NULL; } else { newNode->pnext = pTemp->pnext; newNode->plast = pTemp; pTemp->pnext->plast = newNode; pTemp->pnext = newNode; } return ; } void deletenode(struct strlnode **p, int i) /* 删除链表中第i个节点 */ { /* 代码在这里实现 */ if (p == NULL || *p == NULL ) { printf("error!"); return ; } if (i < 0) { printf("i must be larger than 0 !"); return ; } if (i == 0 && (*p)->pnext == NULL)//仅有一个结点 { free(*p); p = NULL; return; } struct strlnode *pTemp = *p;//指向头结点 int k = i; while(k > 1 && pTemp != NULL)//查找第i-1个结点 { pTemp = pTemp->pnext; k--; } struct strlnode *qTemp; qTemp = pTemp ->pnext; if (qTemp->pnext != NULL) { pTemp->pnext = qTemp->pnext; qTemp->pnext->plast = pTemp; free(qTemp); } else { pTemp->pnext = qTemp->pnext; free(qTemp); } } int getnodenum(struct strlnode **p) /*获取链表中节点个数*/ { int nodenum = 0; /* 代码在这里实现 */ if (NULL == p) { return nodenum; } struct strlnode *pTemp ; pTemp = *p; while(pTemp != NULL) { nodenum++; pTemp = pTemp->pnext; } return nodenum; } void bignumberplus(struct strlnode **plus, struct strlnode **p, struct strlnode **q) /* 使用链表实现大整数相加 */ { /* 代码在这里实现 */ if (NULL == p || NULL == *p) { *plus = *q; return; } if (NULL == q || NULL == *q) { *plus = *p; return; } struct strlnode *pTemp = *p; struct strlnode *qTemp = *q; while(NULL != pTemp->pnext) { pTemp = pTemp->pnext; } while(NULL != qTemp->pnext) { qTemp = qTemp->pnext; } int carry = 0; struct strlnode *sum = (struct strlnode *)malloc(sizeof(struct strlnode)); sum->data = (pTemp->data + qTemp ->data)%10; carry = (pTemp->data + qTemp ->data)/10; sum->plast = NULL; sum->pnext = NULL; pTemp = pTemp->plast; qTemp = qTemp->plast; struct strlnode *temp; while(NULL != pTemp && NULL != qTemp) { temp = (struct strlnode *)malloc(sizeof(struct strlnode)); temp->data = (pTemp->data + qTemp ->data + carry)%10; carry = (pTemp->data + qTemp ->data + carry)/10; temp->pnext= sum; temp->plast = NULL; sum->plast = temp; pTemp = pTemp->plast;//链表均向前移动 qTemp = qTemp->plast; sum = sum->plast; } while(NULL != pTemp) { temp = new struct strlnode; temp->data = (pTemp->data + carry)%10; carry = (pTemp->data + carry)/10; temp->pnext= sum; temp->plast = NULL; sum->plast = temp; pTemp = pTemp->plast; sum = sum->plast; } while(NULL != qTemp) { temp = new struct strlnode; temp->data = (qTemp ->data + carry)%10; carry = (qTemp ->data + carry)/10; temp->pnext= sum; temp->plast = NULL; sum->plast = temp; qTemp = qTemp->plast; sum = sum->plast; } while (carry != 0) { temp = new struct strlnode; temp->data = carry%10; carry /= 10; temp->pnext= sum; temp->plast = NULL; sum->plast = temp; sum = sum->plast; } *plus = sum; 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; }
时间: 2024-10-21 19:44:21