Single-link table function

xiaoxiao2021-03-06  119

Single-link list is very complete, increase, delete, sort, all have

#include

#include

Typedef struct node

{

Int ndate;

Struct Node * PstNext;

} Node;

// Link list output

Void Output (Node * HEAD)

{

Node * p = head-> pstnext;

While (null! = P)

{

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

P = P-> PstNext;

}

Printf ("\ r \ n");

}

// Link table creation

Node * creat ()

{

Node * head = null, * p = null, * s = NULL;

INT DATE = 0, cycle = 1;

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

IF (null == HEAD)

{

Printf ("allocated memory failed \ r \ n");

Return NULL;

}

Head-> pstnext = NULL;

P = head;

While (cycle)

{

PRINTF ("Please enter the data and end input \ r \ n" when entering data is 0);

Scanf ("% D", & Date);

IF (0! = DATE)

{

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

IF (null == s)

{

Printf ("allocated memory failed \ r \ n");

Return NULL;

}

S-> ndate = date;

P-> pstnext = s;

P = S;

}

Else

{

CYCLE = 0;

}

}

P-> PstNext = NULL;

Return (HEAD);

}

// Single-link table

Void Length (Node * HEAD)

{

Node * p = head-> pstnext;

INT j = 0;

While (null! = P)

{

P = P-> PstNext;

J ;

}

Printf ("% d \ r \ n", j);

}

// Link table Find

Void study_date (node ​​* head, int Date)

{

Node * p;

INT n = 1;

P = head-> pstnext;

While (null! = p && date! = p-> ndate)

{

P = P-> PstNext;

N;

}

IF (NULL == P)

{

Printf ("This value is not found in the list");

} else if (date == p-> ndate)

{

Printf ("Value% D to find in the chain in the chain of D position \ r \ n", DATE, N);

}

Return;

}

/ / Find sequence number

Void study_number (node ​​* head, int Num)

{

Node * p = head;

INT i = 0;

While (NULL! = P && I

{

P = P-> PstNext;

i ;

}

IF (p == null) {

Printf ("Look for Location is not legal \ r \ n");

Else IF (i == 0)

{

Printf ("lookup location is head knot \ r \ n");

Else IF (i == Num)

{

Printf ("% D \ n", i, p-> ndate);

}

}

/ / Insert a new knot before the specified element

Void insert_1 (Node * Head, Int i, int newdate)

{

Node * pre = head, * new = null;

INT j = 0;

While (null! = pre && j

{

pre = pre-> pstnext;

J ;

}

IF (NULL == Pre || J> I-1)

{

Printf ("Insert No \ r \ n");

Else

{

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

IF (null == new)

{

Printf ("allocated memory failed \ r \ n");

Return;

}

NEW-> ndate = newdate;

New-> PstNext = pre-> pstnext;

Pre-> PstNext = New;

}

}

/ / Insert a new node after the specified element

Void insert_2 (Node * Head, Int i, int newdate)

{

Node * pre = head, * new = null;

INT j = 0;

While (null! = pre-> PstNext && J

{

pre = pre-> pstnext;

J ;

}

IF (j == i)

{

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

IF (null == new)

{

Printf ("allocated memory failed \ r \ n");

Return;

}

NEW-> ndate = newdate;

New-> PstNext = pre-> pstnext;

Pre-> PstNext = New;

Else

{

Printf ("Insert No \ r \ n");

}

}

/ / Delete the specified node

Void delete_1 (Node * HEAD, INT I3)

{

Node * p = head, * pre = null;

INT j = 0;

While (NULL! = P && J

{

pre = P;

P = P-> PstNext;

J ;

}

IF (NULL == P)

{

Printf ("Delete position does not exist \ r \ n");

Else

{

Pre-> PstNext = P-> PstNext;

Free (p);

}

}

/ / Specify a number of data in a single-chain list and statistics to delete this data

INT delete_2 (Node * Head, INT DELETE_DATE)

{

INT count = 0;

Node * p = head, * q;

While (NULL! = P-> PstNext)

{

Q = P-> PstNext;

IF (q-> ndate == delete_date) {

P-> PstNext = Q-> PstNext;

Free (q);

COUNT;

}

Else

{

P = q;

}

}

Return count;

}

/ / Link table

Void Reverse_List (Node * HEAD)

{

Node * Q, * S;

IF (null == head-> pstnext || null == head-> pstnext-> PstNext)

{

Return;

}

Q = Head-> PstNext-> PstNext;

Head-> PstNext-> PstNext = NULL;

While (null! = q)

{

S = Q-> PstNext;

Q-> PstNext = head-> pstnext;

Head-> pstnext = q;

Q = S;

}

}

// Single-link table

Void Connect_List (Node * Head, Node * Head_New)

{

Node * p = head;

While (NULL! = P-> PstNext)

{

P = P-> PstNext;

}

P-> pstnext = head_new-> pstnexT;

}

// single-chain table destroy

Void Destroy_List (Node * HEAD)

{

While (NULL! = HEAD)

{

Node * TEMP = HEAD;

Head = head-> pstNext;

Free (TEMP);

}

}

Main ()

{

INT DATE, NUM; // Waiting for data

INT i3; // Specify the location of the deletion element

INT I1, I2, NewDate_1, NewDate_2; // New data to be inserted

INT delete_date, k; // The data to be deleted is with the number of numbers

Node * head = null; // Define head node

Node * Head_new = null;

// Link table creation

HEAD = CREAT ();

Printf ("Single List of Output Single List \ R \ n");

Output (Head);

// Single-link table

Printf ("Single Lin Table Length is \ r \ n");

Length (Head);

// Link table Find

Printf ("Please enter the data \ r \ n" to be found);

Scanf ("% D", & Date);

Research_date (head, date);

// Link table look up

Printf ("Please enter the serial number \ r \ n");

Scanf ("% D", & num);

Research_Number (Head, Num);

/ / Insert the new element NewDate before specifying the i1 element

Printf ("Insert a new element newdate before specifying the i-th element");

Printf ("Please enter i with the element and use comma interval \ r \ n");

Scanf ("% D,% D", & I1, & newdate_1);

INSERT_1 (Head, I1, NewDate_1);

Printf ("Insert New Links \ R \ N");

Output (Head);

/ / Insert a new element NewDate after specifying the ith element

Printf ("Insert a new element newdate after specifying the i-th element);

Printf ("Please enter i and element and comma \ r \ n"); scanf ("% D,% D", & I2, & newdate_2);

INSERT_2 (Head, I2, NewDate_2);

Printf ("Insert New Links \ R \ N");

Output (Head);

/ / Specify delete I3 elements

Printf ("Delete Element Location \ r \ n");

Scanf ("% D", & i3);

DELETE_1 (HEAD, I3);

Printf ("Remove New Link Links \ R \ N");

Output (Head);

/ / Specify a number of data in a single-chain list and statistics to delete this data

Printf ("Please enter the element \ r \ n" to be deduced);

Scanf ("% d", & delete_date);

K = delete_2 (head, delete_date);

Printf ("Remove New Link Links \ R \ N");

Output (Head);

Printf ("Delete the number of specified elements in the list:");

Printf ("% d \ r \ n", k);

// Single-link table

Reverse_list (hEAD);

Printf ("Output \ r \ n" inversion after ");

Output (Head);

// Single-link table

Printf ("Building a new list \ r \ n");

HEAD_NEW = Creat ();

Printf ("Output New Link");

Output (Head);

Printf ("Connect the new list to the tail of the original linked list and output \ r \ n");

Connect_list (head, head_new);

Output (Head);

Destroy_List (HEAD);

}

Below is the output result:

VS2010 under debugging results

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

New Post(0)