Reverse placement of ordered tables such as arrays and linked lists

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.