1) reverse placement of Arrays
(1) Algorithm
# Indclude <stdio. h>
# Define N 8
Main ()
{
Int array [N] = {, 90, 80, 70, 60, 50, 50 };
Int I, j, t;
For (I = 0, j = N-1; I <j; I ++, j --)
{
T = array [I];
Array [I] = array [j];
Array [j] = t;
}
For (I = 0, I <listsize; I ++)
Printf ("% d", qlist. data [I])
}
(2) time complexity
Since only loop N/2 can be used to complete the reverse configuration, the time complexity is O (N/2 ).
(2) reverse configuration of one-way linked list
(1) Algorithm
// Define struct
Struct t_node
{
Int data;
Struct t_node * next;
};
// Define the alias
Typedef struct t_node Node;
// Define linked list Variables
Node * example_link = NULL;
/*
* Function: reverse the linked list
* Input: x = original linked list
* Output: None
* Return value: the reversed new linked list.
*/
Node reverse (Node * x)
{
If (NULL = x)
Return NULL;
Link t = NULL;
Link r = NULL, y = x; // (0)
While (y! = NULL)
{
T = y-> next; // (1)
Y-> next = r; // (2)
R = y; // (3)
Y = t; // (4)
}
Return r; // return the reverse linked list
}
/*
* Function: Initialize the linked list.
* Input: None
* Output: None
* Return value: None
*/
Void Init_Link (void)
{
Node * pNext = NULL; // cursor
// Header Node
Example_link = (Node *) malloc (sizeof (Node ));
If (NULL = example_link)
Return;
Example_link-& gt; data = 100;
Example_link-> next = NULL;
// Intermediate node
PNext = example_link; // assign a value to the cursor
For (I = 1, I <N, I ++)
{
PNext-> next = (Node *) malloc (sizeof (Node ));
If (NULL = pNext-> next)
Return;
PNext-> next-> data = 100-I * 10;
PNext-> next = NULL;
PNext = pNext-> next; // move the cursor
}
}
Main ()
{
Int I, n;
Node * pNext = NULL;
Node * reverse_link = NULL;
// Initialize the linked list
Init_Link ();
If (NULL = example_link)
Return;
// Display the original linked list
Printf ("original Linked List Value: \ n ");
PNext = example_link;
While (pNext! = NULL)
{
Printf ("% d,", pNext-> data );
PNext = pNext-> next;
}
Printf ("\ n ");
// Reverse linked list
Reverse_link = reverse (example_link );
Printf ("reverse Linked List Value: \ n ");
PNext = reverse_link;
While (pNext! = NULL)
{
Printf ("% d,", pNext-> data );
PNext = pNext-> next;
}
Getch ();
} // Endmain ()
(2) time complexity
Since the reverse function must traverse the entire linked list, the time complexity is O (N ).
This article is from the "C ++" blog, please be sure to keep this source http://ljqy27.blog.51cto.com/1054/40153