Create (destroy) linked list

xiaoxiao2021-03-05  23

/ * #Include "dstruct.h" * /

#include

/ * CREATLIST * /

Typedef struct node

{

CHAR DATA;

Struct Node * Next;

} Node, * linklist;

LinkList CreatFromhead (Void)

/ * Head interpolation * /

{

Linklist L, T;

Node * S;

Char C;

INT flag = 1;

L = (LinkList) Malloc (Sizeof (Node));

L -> next = NULL;

T = L;

While (flag)

{

C = getchar ();

IF (c! = '$')

/ * INPUT '$' to EXIT .. * /

{

s = (node ​​*) malloc (sizeof (node));

s -> data = C;

/ * Head interpolation here and the tail interpolation is different! ~ * /

S -> next = L -> next;

L -> next = s;

}

Else Flag = 0;

}

Return T;

}

/ ************************************************** ************ /

/ * I have made a mistake to add a space behind the * and / in the middle of TC. * /

LinkList CreateFromtail ()

{

Linklist L;

Node * r, * S;

INT FLAG = 1; char C;

L = (node ​​*) malloc (sizeof (node));

L -> next = NULL;

R = L;

While (flag)

{

C = getchar ();

IF (c! = '$')

{

s = (node ​​*) malloc (sizeof (node));

/ * Tail interpolation here and the head interpose is different! ~ * /

s -> data = c;

r -> next = s;

R = S;

}

Else

{

FLAG = 0;

r -> next = null;

}

}

Return L;

}

Void show (Node * T)

{

Node * p = T;

INT size = 0;

While (p -> next! = null)

{ size;

Printf ("% c", p -> data);

P = P -> next;

}

Printf ("% d", p -> data);

Printf ("/ n length is ___ / b / b% d.", size);

}

Void Destroy (Node * T)

/ * Data should be destroyed before you have to withdraw from ... Prevent memory leakage! * /

{

While (t)

{

Node * TEMP = T;

T = T -> NEXT;

Free (TEMP);

}

Return;

}

/ * Test code. . * /

int main ()

{

Node * t = creATFROMHEAD ();

SHOW (T);

DESTROY (T);

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-35566.html

New Post(0)