doubly linked list
#include <stdio.h> #include <stdlib.h> #define ELEMTP int typedef struct LNODE {
ELEMTP data;
struct Lnode *prior;
struct Lnode *next;
}lnode;
Lnode *create ()//tail interpolation method to establish a doubly linked list {Lnode *head, *p, *P2;
ELEMTP x;
Head = (Lnode *) malloc (sizeof (Lnode));
Head->next = NULL;
p = head;
scanf_s ("%d", &x);
while (x! =-1) {P2 = (Lnode *) malloc (sizeof (Lnode));
P2->data = x;
P2->next = p->next;
P2->prior = p;
P->next = p2;
p = p->next;
scanf_s ("%d", &x);
} p->next = NULL;
return head;
} lnode *insert (Lnode *head,int i,elemtp x) {Lnode *p, *s;//s is the node to be inserted p = head;
for (int k = 1; k <=i; k++) p = p->next;
s = (Lnode *) malloc (sizeof (Lnode));
S->data = x;
S->prior = p->prior;//The precursor node of P P1 as the precursor node of s s->next = p;
P->prior->next = s;
P->prior = s;//Attention order can not mess up return head;
} lnode *del (Lnode *head,int i) {Lnode *p;
p = head;
for (int k = 1; k <= i; k++) p = p->next;
if (p = = NULL) printf ("Position not valid \ n");
else if (P->next = = null)//Last node does special processing {p->prior->next = null;
Free (p);
} else {p->prior->next = p->next;
P->next->prior = p->prior;
Free (p);
} return head;
} void Outline (Lnode *head) {Lnode *p;
p = head->next;
while (P! = NULL) {printf ("%2d", p->data); p = P-> next; }} int main () {lnode *head; int i;
ELEMTP x;
Head = Create ();
Outline (head); printf ("\ n where to insert");
scanf_s ("%d", &i); printf ("Size to insert");
scanf_s ("%d", &x);
Head = insert (HEAD,I,X);
Outline (head); printf ("\ nthe location to delete");
scanf_s ("%d", &i);
Head = del (head, I);
Outline (head);
return 0; }
set A is represented by a single-linked list of LA, and set B is represented by a single-stranded-list lb, and the design algorithm asks for a and B two sets of differences, i.e. a
Analysis
A-B is an element that belongs to a and is not part of a, for each element of set a, x, is searched in set B, and if there is an element identical to X, delete the node of x in a.
#include <stdio.h> #include <stdlib.h> typedef struct LNODE {int data;
struct Lnode *next;
}lnode;
struct Lnode *create ()//tail interpolation method to establish a linked list, unfamiliar see data structure of the linked list of the basis {Lnode *head, *p, *P2;
Head = (Lnode *) malloc (sizeof (Lnode));
Head->next = NULL;
p = head;
int x;
scanf_s ("%d", &x);
while (x! =-1) {p2= (Lnode *) malloc (sizeof (Lnode));
P2->data = x;
P->next = p2;
p = p2;
scanf_s ("%d", &x);
} p->next = NULL;
return head;
} void Outline (Lnode *head) {Lnode *p;
p = head->next;
while (P! = NULL) {printf ("%2d", p->data);
p = p->next;
}} Lnode *chaji (Lnode *la, Lnode *lb)//belongs to La and does not belong to Lb element {Lnode *p, *prep,*q,*r;
PREP = La; p = la->next;//p is a node in La, Prep is the precursor node of P, while (P! = NULL) {q = lb->next;//sequentially scans the Lb to see if there is a node that is the same as the node in the La table (q
&& q->data! = p->data) Q = q->next;
if (q! = NULL)//finds the same node in LB as the node in LA that points to p, removing the node that P points to (r = p);
Prep->next = p->next;//Delete node p = p->next;//points to the next node free (r);
} else//did not find the same node, continue to find in LA, p pointing to the next, s pointing to p precursor {prep = p; p = p->next;
}} return La;
} int main () {Lnode *la,*lb;
printf ("Input la element,-1 end \ n");
La= Create ();
Outline (La);
printf ("\ n input lb element,-1 end \ n");
Lb = Create ();
Outline (Lb);
printf ("\nla and lb difference set \ n");
La = Chaji (LA, Lb);
Outline (La); }
polynomial add-ons
#include <stdio.h> #include <stdlib.h> #define OK 1 #define STATUS int typedef struct LPOLY {int coef;//coefficient I NT exp;
index struct Lpoly *next;
}lpoly;
Lpoly *creatlist (lpoly *head,int N) {lpoly *p, *p2;
Head = (Lpoly *) malloc (sizeof (Lpoly));
Head->next = NULL;
p = head;
for (int i = 0; i < n; i++) {P2 = (Lpoly *) malloc (sizeof (Lpoly));
printf ("Coef:");
scanf ("%d", & (P2->coef));
printf ("Exp:");
scanf ("%d", & (P2->exp));
P->next = p2;
p = p2;
} p->next = NULL;
return head;
} void Print (Lpoly *head) {Lpoly *p;
p = head->next;
while (p) {printf ("%dx^%d", p->coef,p->exp);
if (p->next) printf ("+");
p = p->next;
} printf ("\ n");
} lpoly *addpolyn (Lpoly *la, Lpoly *lb) {lpoly *pa, *PB,*PC,*LC; PA = la->next;
PB = lb->next;
Lc = PC = La;
while (PA&&PB) {if (Pa->exp > Pb->exp) {pc->next = PA;
PC = PA;
PA = pa-Next; } else if (Pa->exp < Pb->exp) {pc->next = PB;
PC = PB;
PB = pb->next;
} else {if (Pa->coef + Pb->coef! = 0) {Pa->coef = Pa->coef + pb->coef;//Note is a factor that changes the PA and then is connected to the back of the PC
PC = PA;
} PA = pa->next;
PB = pb->next; }} Pc->next = PA?
PA:PB;
return Lc;
} int main () {Lpoly *la, *LB,*LC;
int na, NB;
La = Lb = NULL;
printf ("Create LA, Descending, enter number of items na:\n");
scanf ("%d", &na);
La=creatlist (La, NA);
printf ("Create lb, descending, number of inputs nb:\n");
scanf ("%d", &NB);
LB = creatlist (lb, NB);
printf ("La for:");
Print (La);
printf ("lb is:");
Print (LB);
Lc = Addpolyn (La, Lb);
Print (LC);
return 0;
}
Static Linked list
/* The linked list described in the array is called a static list */#include <stdio.h> #include <stdlib.h> #define MAX #define OK 1 #d
efine Status int typedef struct {int data;
int cur;
}stacklinklist[max];
int av =-1, h =-1;
Status initlist (stacklinklist space)//Initialize {int i;
for (i = 0; i<max-1; i++) Space[i].cur = i + 1;
Space[max-1].cur = -1;//static list is empty, the last element cur is-1; av = 0;
return OK;
} Status Insert (stacklinklist L, int x) {if (av = =-1) printf ("No empty location \ n");
else {int i;
i = av;
AV = l[i].cur;
L[i].data = x;
L[i].cur = h;
h = i;
} return OK;
} int Delete (stacklinklist l,int *x) {int i;
if (H! =-1)//h=-1 represents the static list of all elements {*x = L[h].data;
i = h;
h = l[h].cur;
L[i].cur = av;
AV = i;
} return OK;
} void Outline (Stacklinklist L) { int h = h;//The value of H to H, otherwise it will change the value of H while (h! =-1) {printf ("%2d", l[h].data);
H = L[h].cur;
}} int main () {stacklinklist L;
int cord, result;
Do {printf ("\n1. Initialize static linked list \ n");
printf ("2. Insert new element \ n");
printf ("3. Delete element \ n");
printf ("4. Display \ n");
scanf_s ("%d", &cord);
Switch (Cord) {Case 1: {result = Initlist (L);
if (result = = OK) printf ("successfully initialized \ n");
}break;
Case 2: {int x;
printf ("Enter the element to insert x:");
scanf_s ("%d", &x);
Insert (L, x);
}break;
Case 3: {int x;
if (delete (L, &x)) printf ("Successfully deleted element%d\n", x);
}break;
Case 4: {Outline (L);
}break;
} } while (cord<5);
return 0; }