Review common operations on a single-chain table

Source: Internet
Author: User

Review common operations on a single-chain table, including creating, inserting, deleting, sorting, reverse setting, and printing and outputting a single-chain table.

# Include <IOSTREAM> using namespace std; typedef struct Single_link {int data; struct Single_link * next;} node; // Create node * Create () {node * head for a single-chain table, * p, * s; int x, cycle = 1; head = (node *) malloc (sizeof (node); p = head; cout <"Input the elements of the link:" <endl; while (cycle) {cin> x; if (x! = 0) {s = (node *) malloc (sizeof (node); s-> data = x; p-> next = s; p = s ;} else cycle = 0;} head = head-> next; p-> next = NULL; return head;} // the length of a single-chain table, int length (node * head) {node * p; p = head; int count = 0; while (p! = NULL) {p = p-> next; count ++;} return count ;} /// // insert a single-chain table /// /// // node * Insert (node * head, int element) {// p is the traversal pointer. q is used to save the inserted node location node. s is the node to be inserted * p, * q, * s; p = head; // create the inserted new node s = (node *) malloc (sizeof (node); s-> data = element; while (s-> data> p-> data & p-> next! = NULL) {q = p; p = p-> next;} if (s-> data <= p-> data) {if (head = p) {s-> next = p; head = s;} else {q-> next = s; s-> next = p ;}} else {p-> next = s; s-> next = NULL;} return head ;} /// // delete a single-chain table //////// /// // node * del (node * head, int element) {// p is the traversal pointer. q is used to save the deleted node * p, * q; p = head; while (p-> data! = Element & p-> next! = NULL) {q = p; p = p-> next;} if (p-> data = element) {if (head = p) {head = p-> next; free (p);} else {q-> next = p-> next ;}} else {cout <"Not find the delete element. "<endl;} return head ;} /// // reverse sequence of a single-chain table ////// /// // node * Reverse (node * head) {node * p, * q, * s; if (head = NULL & head-> next = NULL) {return head;} p = head; q = p-> next; while (q) {s = q-> next; Q-> next = p; p = q; q = s;} head-> next = NULL; head = p; return head ;} /// // sort a single-chain table ////// /// // node * Sort (node * head) {node * p; int n; int temp; n = length (head); if (head = NULL & head-> next = NULL) {return head ;} p = head; for (int j = 1; j <n; j ++) {p = head; for (int I = 0; I <n-j; I ++) {if (p-> data> p-> next-> data) {temp = p-> data; p-> data = p-> next-> data; p-> next -> Data = temp;} p = p-> next ;}} return head ;} /// // print the single-chain table ///////////// //////////////////////////////////////// ///// void print (node * head) {node * p; p = head; int n = length (head); cout <"A total of" <n <"records in the linked list. "<endl; if (head! = NULL) {cout <"the elements in the linked list are:" <endl; while (p! = NULL) {cout <p-> data <"; p = p-> next;} cout <endl ;}} int main () {node * head; int n; head = Create (); print (head); head = Sort (head); print (head); cout <"Input the insert number:" <endl; cin> n; Insert (head, n); print (head); cout <"Input the delete number:" <endl; cin> n; del (head, n); print (head); head = Reverse (head); print (head); return 0 ;}

 

 

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.