#include <stdlib.h> #include <iostream>using namespace std; #define NULL 0#define MAXSIZE 50struct strlnode{ int data;struct strlnode *plast;struct strlnode *pnext;}; void Create (struct strlnode **p, int x)/* Create a doubly linked list (table header node) */{struct strlnode *q;q = (struct Strlnode *) malloc (sizeof (struct ST Rlnode)); q->data = X;q->plast = Null;q->pnext = Null;*p = Q;return;} void Insertnode (struct strlnode **p, int i, int x)/* The node where the data equals x is inserted in the list I of the link */{/* code is implemented here */if (p = = NULL | | *p = = NULL) {Cou t<< "error!" <<endl;return;} if (I < 0) {printf ("I must be larger than 0!"); return;} struct Strlnode *ptemp = *p;//points to the head node struct strlnode *newnode;newnode = (struct Strlnode *) malloc (sizeof (struct strlnode)); Newnode->data = x;if (0 = = i)//Insert head node {newnode->pnext = Ptemp;ptemp->plast = Newnode;newnode->plast = NULL;*p = Newnode;return;} int k = I;while (k > 1 && ptemp->pnext! = NULL)//Find the first node {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)/* Delete the list I node */{/* code is implemented here */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)//Only one node {free (*p);p = Null;return;} struct Strlnode *ptemp = *p;//points to head node int k = I;while (k > 1 && ptemp! = NULL)//Find first i-1 node {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)/* Gets the number of nodes in the list */{int Nodenum = 0;/* code is implemented here */if (NULL = = p) {return nodenum;} struct Strlnode *ptemp;p temp = *p;while (ptemp! = NULL) {nodenum++;p temp = Ptemp->p Next;} return nodenum;} void Bignumberplus (struct strlnode **plus, struct strlnode **p, struct strlnode **q)/* Use a linked list to implement a large integer add */{/* code is implemented here */if (NU LL = = 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 STRLN Ode *) malloc (sizeof (struct strlnode)), Temp->data = (Ptemp->data + qtemp->data + carry)%10;carry = (Ptemp->da Ta + qtemp->data + carry)/10;temp->pnext= sum;temp->Plast = Null;sum->plast = Temp;ptemp = ptemp->plast;//list is moved forward qtemp = Qtemp->plast;sum = Sum->plast;} while (NULL! = ptemp) {temp = new struct Strlnode;temp->data = (ptemp->data + carry)%10;carry = (Ptemp->data + Carr Y)/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 + CA Rry)/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)/* Writes an array to the list, the order of the data in the linked list is consistent with the sequence in the arrays */{int j = 0;int data = 0;str UCT 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)/* Writes the list to the array, the order of the data in the array and the order in the list are consistent */{int j = 0;struct Strlnode *s = P;wh Ile (s! = null) {* (A + j) = S->data;s = s->pnext;j++;} return;}
Double-linked List basic operation