# Include <stdio. h>
# Include <iostream>
# Include "Define. H"
Using namespace STD;
Int main (INT argc, char * argv [])
{
// Create a single-cycle linked list
Node * head_cycle = NULL;
Cout <"create the cycle list (by queue):" <Endl;
Head_cycle = createlist_cycle ();
Deletelist (head_cycle );
# Include <stdio. h>
# Include <iostream>
# Include "Define. H"
Using namespace STD;
Int main (INT argc, char * argv [])
{
// Create a single-cycle linked list
Node * head_cycle = NULL;
Cout <"create the cycle list (by queue):" <Endl;
Head_cycle = createlist_cycle ();
Deletelist (head_cycle );
// Create a linked list and return the number of linked lists
Node * head = NULL;
Cout <"create the single list (by queue):" <Endl;
Head = createlist ();
Cout <"print the single list:" <Endl;
Printlist (head );
// Calculate the length of the linked list
Int n = linklength (head );
Cout <"the length of the single list is:" <n <Endl;
// Locate the input element
Char X;
Printf ("Please input an element and return the location of the element! \ N ");
Scanf ("% C", & X );
Fflush (stdin); // clears the standard input buffer so that you can use fflush to clear the buffer next time if multiple input statements exist.
Locate (Head, X );
// Insert an element into the linked list
Cout <"Please input one char and one num (Please insert space between the two chracters):" <Endl;
Char I;
Int num;
Scanf ("% C % d", & I, & num );
Fflush (stdin );
Insertlist (Head, I, num, N );
// Reverse output of the linked list
Printf ("\ noutput The inversing list! \ N ");
Inverselist (head );
Deletelist (head );
Return true;
}
Struct node_link * createlist_cycle ()
{
Char C;
Node * tail;
Node * head = NULL;
While (C = getchar ())! = '\ N ')
{
Node P = (node) malloc (sizeof (node ));
// Node P = (node) malloc (sizeof (node ));
If (P = NULL)
{
Printf ("malloc failed! \ N "); // memory application failed
Return false;
}
P-> DATA = C;
If (Head = NULL)
{
Head = P;
Tail = P;
}
Else
{
Tail-> next = P;
Tail = P;
}
Tail-> next = NULL; // you must apply for a new application and release the application each time.
}
Tail-> next = head;
Node head_insert = head;
While (head_insert)
{
Cout Head_insert = head_insert-> next;
If (head_insert = head)
{
Break;
}
}
Return head;
}
Struct node_link * createlist ()
{
Char C;
Node * tail;
Node * head = NULL;
While (C = getchar ())! = '\ N ')
{
Node P = (node) malloc (sizeof (node ));
If (P = NULL)
{
Printf ("malloc failed! \ N "); // memory application failed
Return false;
}
P-> DATA = C;
If (Head = NULL)
{
Head = P;
Tail = P;
}
Else
{
Tail-> next = P;
Tail = P;
}
Tail-> next = NULL; // you must apply for a new application and release the application each time.
}
Return head;
}
Int linklength (node * head)
{
If (Head = NULL)
Return 0;
Else
Return (1 + linklength (Head-> next ));
// Return (head );
// Return (N );
}
Void printlist (node * head)
{
If (Head = NULL & flag = 0)
Cout <"The list is null! "<Endl;
Else
{
Flag = 1; // set the flag to determine
Cout If (Head-> next = NULL)
{
Cout <Endl;
Return;
}
Printlist (Head-> next );
}
}
Int locate (node * head, char X)
{
Int n = 0;
While (Head! = NULL & head-> data! = X)
{
Head = head-> next;
N ++;
}
If (Head = NULL)
Return (-1 );
Else
Cout <"the location of the element is" <n + 1 <Endl;
Return (n + 1 );
}
Struct node_link * inverselist (node * head)
{
Node head_node;
Node P, Q, R;
P = head;
Q = p-> next;
While (Q! = NULL)
{
R = Q-> next;
Q-> next = P;
P = Q;
Q = R;
}
Head-> next = NULL;
Head_node = P;
Head = P;
While (head)
{
Cout Head = head-> next;
}
Cout <Endl;
Return head_node;
}
Int insertlist (node * head, char X, int I, int Len) // you must pass the pointer to the header.
{
Node head_insert;
Node P = new node [];
P-> DATA = X;
Int J = 1;
If (I = 1) // consider the differences between the inserted header and the inserted end.
{
P-> next = head;
Head = P;
Head_insert = head;
}
Else if (I = (LEN + 1) // use parentheses to prevent errors
{
Head_insert = head;
Int CNT = 0;
While (CNT! = Len-2)
{
Head = head-> next;
}
Head-> next = P;
P-> next = NULL; // There is no guarantee that the tail pointer can also be passed in. insert and create a linked list are two things.
}
Else if (I> = Len + 1)
{
Cout <"the parameter of location is incorrect! "<Endl;
Return 0;
}
Else // The third case is insert to the intermediate position
{
Head_insert = head;
While (j <I & head-> next! = NULL)
{
Head = head-> next;
J ++;
If (j = I)
{
P-> next = head-> next;
Head-> next = P;
}
// Else
// Cout <"the parameter of location is incorrect! "<Endl;
}
}
// Return head_insert;
Cout <"after inserting, print the single list:" <Endl;
While (head_insert)
{
Cout Head_insert = head_insert-> next;
}
Return 1;
}
Void deletelist (node * head)
{
Node p, q;
P = head-> next;
While (P! = Head )//
{
Q = p-> next;
Free (p );//
P = Q;
}
Free (head );
Head = NULL;
// Cout <"head = NULL" <Endl;
}
// Create a linked list and return the number of linked lists
Node * head = NULL;
Cout <"create the single list (by queue):" <Endl;
Head = createlist ();
Cout <"print the single list:" <Endl;
Printlist (head );
// Calculate the length of the linked list
Int n = linklength (head );
Cout <"the length of the single list is:" <n <Endl;
// Locate the input element
Char X;
Printf ("Please input an element and return the location of the element! \ N ");
Scanf ("% C", & X );
Fflush (stdin); // clears the standard input buffer so that you can use fflush to clear the buffer next time if multiple input statements exist.
Locate (Head, X );
// Insert an element into the linked list
Cout <"Please input one char and one num (Please insert space between the two chracters):" <Endl;
Char I;
Int num;
Scanf ("% C % d", & I, & num );
Fflush (stdin );
Insertlist (Head, I, num, N );
// Reverse output of the linked list
Printf ("\ noutput The inversing list! \ N ");
Inverselist (head );
Deletelist (head );
Return true;
}
Struct node_link * createlist_cycle ()
{
Char C;
Node * tail;
Node * head = NULL;
While (C = getchar ())! = '\ N ')
{
Node P = (node) malloc (sizeof (node ));
// Node P = (node) malloc (sizeof (node ));
If (P = NULL)
{
Printf ("malloc failed! \ N "); // memory application failed
Return false;
}
P-> DATA = C;
If (Head = NULL)
{
Head = P;
Tail = P;
}
Else
{
Tail-> next = P;
Tail = P;
}
Tail-> next = NULL; // you must apply for a new application and release the application each time.
}
Tail-> next = head;
Node head_insert = head;
While (head_insert)
{
Cout Head_insert = head_insert-> next;
If (head_insert = head)
{
Break;
}
}
Return head;
}
Struct node_link * createlist ()
{
Char C;
Node * tail;
Node * head = NULL;
While (C = getchar ())! = '\ N ')
{
Node P = (node) malloc (sizeof (node ));
If (P = NULL)
{
Printf ("malloc failed! \ N "); // memory application failed
Return false;
}
P-> DATA = C;
If (Head = NULL)
{
Head = P;
Tail = P;
}
Else
{
Tail-> next = P;
Tail = P;
}
Tail-> next = NULL; // you must apply for a new application and release the application each time.
}
Return head;
}
Int linklength (node * head)
{
If (Head = NULL)
Return 0;
Else
Return (1 + linklength (Head-> next ));
// Return (head );
// Return (N );
}
Void printlist (node * head)
{
If (Head = NULL & flag = 0)
Cout <"The list is null! "<Endl;
Else
{
Flag = 1; // set the flag to determine
Cout If (Head-> next = NULL)
{
Cout <Endl;
Return;
}
Printlist (Head-> next );
}
}
Int locate (node * head, char X)
{
Int n = 0;
While (Head! = NULL & head-> data! = X)
{
Head = head-> next;
N ++;
}
If (Head = NULL)
Return (-1 );
Else
Cout <"the location of the element is" <n + 1 <Endl;
Return (n + 1 );
}
Struct node_link * inverselist (node * head)
{
Node head_node;
Node P, Q, R;
P = head;
Q = p-> next;
While (Q! = NULL)
{
R = Q-> next;
Q-> next = P;
P = Q;
Q = R;
}
Head-> next = NULL;
Head_node = P;
Head = P;
While (head)
{
Cout Head = head-> next;
}
Cout <Endl;
Return head_node;
}
Int insertlist (node * head, char X, int I, int Len) // you must pass the pointer to the header.
{
Node head_insert;
Node P = new node [];
P-> DATA = X;
Int J = 1;
If (I = 1) // consider the differences between the inserted header and the inserted end.
{
P-> next = head;
Head = P;
Head_insert = head;
}
Else if (I = (LEN + 1) // use parentheses to prevent errors
{
Head_insert = head;
Int CNT = 0;
While (CNT! = Len-2)
{
Head = head-> next;
}
Head-> next = P;
P-> next = NULL; // There is no guarantee that the tail pointer can also be passed in. insert and create a linked list are two things.
}
Else if (I> = Len + 1)
{
Cout <"the parameter of location is incorrect! "<Endl;
Return 0;
}
Else // The third case is insert to the intermediate position
{
Head_insert = head;
While (j <I & head-> next! = NULL)
{
Head = head-> next;
J ++;
If (j = I)
{
P-> next = head-> next;
Head-> next = P;
}
// Else
//
Cout <"the parameter of location is incorrect! "<Endl;
}
}
// Return head_insert;
Cout <"after inserting, print the single list:" <Endl;
While (head_insert)
{
Cout Head_insert = head_insert-> next;
}
Return 1;
}
Void deletelist (node * head)
{
Node p, q;
P = head-> next;
While (P! = Head )//
{
Q = p-> next;
Free (p );//
P = Q;
}
Free (head );
Head = NULL;
// Cout <"head = NULL" <Endl;
} Question 1: How much memory is applied and how much memory is released. Although this is the case, you must manually set node P = (node) malloc (sizeof (node ));
// Node P = (node) malloc (sizeof (node); the node cannot be released. The requested struct is different from applying for a struct pointer. Question 2: when using the sanf function, you must clear the cache area twice,
The error is not beyond your grasp!