Single-chain table (implemented in C Language) and single-chain C Language

Source: Internet
Author: User

Single-chain table (implemented in C Language) and single-chain C Language

Linked List structure:

SList. h

# Pragma once typedef int DataType; typedef struct SListNode {DataType data; struct SListNode * next;} SListNode; // If You Want To modify the linked list, you must add the reference SListNode * _ BuyNode (DataType x); // create the node void PrintSlist (SListNode * pHead ); // print the single-chain table void PushBack (SListNode * & pHead, DataType x); // tail plug (reference is used here to indicate the list alias. parameters are transmitted during the call, do not pass the address) (reference in. c file unavailable) // void PushBack (SListNode ** pHead, DataType x ); // The first parameter points to the address of the pointer to the first node of the linked list (passing the parameter when calling) void PopBack (SListNode * & pHead ); // Delete void PushFront (SListNode * & pHead, DataType x) at the end; // insert void PopFront (SListNode * & pHead) in the header ); // header Delete void DestoryList (SListNode * & pHead); // clear the entire linked list int GetSize (SListNode * pHead); // obtain the length of the linked list SListNode * Find (SListNode * pHead, dataType x); // find void Insert (SListNode * pos, DataType x); // Insert the data void Erase (SListNode * & pHead, SListNode * pos) after a certain position ); // Delete the data void DelNonTailNode (SListNode * pos) at a certain position; // Delete the non-tail node void InsertFrontNode (SListNode * pos, DataType x) of a single-chain table without a header ); // insert a node SListNode * FindMidNode (SListNode * pHead) before a non-header node in the single-link table; // find the intermediate node SListNode * FindKNode (SListNode * pHead, int k ); // query the last k nodes (only one traversal is required) void PrintTailToHead (SListNode * pHead); // print the single-chain table backwards (recursive) // SListNode * Reverse _ (SListNode * pHead); // Reverse placement of a single-chain table (the returned value needs to be received), the original linked list will be completely non-void Reverse (SListNode * & pHead ); // reverse the original linked list to SListNode * Merge (SListNode * pHead1, SListNode * pHead2); // Merge two ordered linked lists (after merging, the list is still ordered) (recursion) void Sort (SListNode * pHead); // bubble Sort

SList. cpp

# Include "SList. h "# include <stdio. h> # include <assert. h> # include <malloc. h> SListNode * _ BuyNode (DataType x) // create a node {SListNode * tmp = (SListNode *) malloc (sizeof (SListNode); tmp-> data = x; tmp-> next = NULL; return tmp;} void PrintSlist (SListNode * pHead) // print the single-chain table {SListNode * cur = pHead; while (cur) {printf ("% d->", cur-> data); cur = cur-> next;} printf ("NULL \ n ");} // void PushBack (SListNode ** ppHead, Data Type x) // tail plug // {// assert (ppHead); // 1. null // 2. not empty // if (* ppHead = NULL) // {// * ppHead = _ BuyNode (x ); //} // else // {// locate the end // SListNode * tail = * ppHead; // while (tail-> next! = NULL) // {// tail = tail-> next; //} // tail-> next = _ BuyNode (x ); ///} //} void PushBack (SListNode * & pHead, DataType x) // End Plug {// 1. null // 2. if (pHead = NULL) {pHead = _ BuyNode (x);} else {// locate the end SListNode * tail = pHead; while (tail-> next! = NULL) {tail = tail-> next;} tail-> next = _ BuyNode (x) ;}} void PopBack (SListNode * & pHead) // Delete At the end {// 1. null // 2. one node // 3. multiple nodes // if (pHead = NULL) {return;} else if (pHead-> next = NULL) {free (pHead); pHead = NULL ;} else {SListNode * tail = pHead; SListNode * prev = NULL; while (tail-> next) {prev = tail; tail = tail-> next;} free (tail ); prev-> next = NULL;} void PushFront (SListNode * & pHead, DataType x) // Insert the header {// 1. null // 2. if (pHead = NULL) {pHead = _ BuyNode (x);} else {SListNode * tmp = _ BuyNode (x); tmp-> next = pHead; pHead = tmp ;}} void PopFront (SListNode * & pHead) // Delete headers {/// 1. null // 2. one node // 3. more than one node // if (pHead = NULL) {return;} else if (pHead-> next = NULL) {free (pHead); pHead = NULL ;} else {SListNode * tmp = pHead; pHead = pHead-> next; free (tmp) ;}} void DestoryList (SListNode * & pHead) // clear the entire linked list {SListNode * cur = pHead; while (cur) {SListNode * tmp = cur; cur = cur-> next; free (tmp );} pHead = NULL;} int GetSize (SListNode * pHead) // obtain the length of the linked list {assert (pHead); SListNode * cur = pHead; int count = 0; while (cur) {count ++; cur = cur-> next;} return count;} SListNode * Find (SListNode * pHead, DataType x) // Find the node {SListNode * cur = pHead; while (cur) {if (cur-> data = x) {return cur;} cur = cur-> next;} return NULL;} void Insert (SListNode * pos, dataType x) // Insert the node {assert (pos); SListNode * tmp = _ BuyNode (x); tmp-> next = pos-> next; pos-> next = tmp;} void Erase (SListNode * & pHead, SListNode * pos) // delete a node in a certain position {assert (pos); assert (pHead ); // pos is the header node if (pHead = pos) {pHead = pHead-> next; free (pos); return;} // SListNode * prev = pHead; while (prev) {if (prev-> next = pos) {prev-> next = pos-> next; free (pos); break ;} prev = prev-> next ;}} void DelNonTailNode (SListNode * pos) // delete a non-tail node {assert (pos) of a single-chain table with no headers ); assert (pos-> next); SListNode * del = pos-> next; SListNode * dnext = del-> next; pos-> data = del-> data; pos-> next = dnext; free (del);} void InsertFrontNode (SListNode * pos, DataType x) // insert a node {assert (pos); SListNode * tmp = _ BuyNode (pos-> data) before a non-header node in the single-link table ); tmp-> next = pos-> next; pos-> next = tmp; pos-> data = x;} void Sort (SListNode * pHead) // bubble sort {assert (pHead); int size = GetSize (pHead); for (int I = 0; I <size-1; I ++) {SListNode * left = pHead; SListNode * right = pHead-> next; for (int j = 0; j <size-I-1; j ++) {if (left-> data> right-> data) {int tmp = left-> data; left-> data = right-> data; right-> data = tmp ;} right = right-> next; left = left-> next ;}} SListNode * FindMidNode (SListNode * pHead) // find the intermediate node {SListNode * fast = pHead; SListNode * slow = pHead; while (fast & fast-> next) {slow = slow-> next; fast = fast-> next;} return slow ;} SListNode * FindKNode (SListNode * pHead, int k) // find the last k nodes {SListNode * fast = pHead; SListNode * slow = pHead; while (fast & k --) {fast = fast-> next;} if (k> 0) {return NULL;} while (fast) {slow = slow-> next; fast = fast-> next ;} return slow;} void PrintTailToHead (SListNode * pHead) // print a single-chain table backwards (recursively) {if (pHead) {PrintTailToHead (pHead-> next ); printf ("% d", pHead-> data) ;}// SListNode * Reverse _ (SListNode * pHead) // Reverse single-chain table (return values need to be received) the original linked list will be untitled /// {// SListNode * cur = pHead; // SListNode * newHead = NULL; // while (cur) // {// SListNode * tmp = cur; // cur = cur-> next; // tmp-> next = newHead; // newHead = tmp; //} // return newHead; //} void Reverse (SListNode * & pHead) // Reverse single-chain table {SListNode * cur = pHead; SListNode * newHead = NULL; while (cur) {SListNode * tmp = cur; cur = cur-> next; tmp-> next = newHead; newHead = tmp;} pHead = newHead; // return newHead;} SListNode * Merge (SListNode * pHead1, SListNode * pHead2) // merge two ordered linked lists (still ordered after merging) recursion {if (pHead1 = NULL) return pHead2; else if (pHead2 = NULL) return pHead1; SListNode * pMergedHead = NULL; if (pHead1-> data <pHead2-> data) {pMergedHead = pHead1; pMergedHead-> next = Merge (pHead1-> next, pHead2 );} else {pMergedHead = pHead2; pMergedHead-> next = Merge (pHead1, pHead2-> next);} return pMergedHead ;}

Test. cpp

# Include "SList. h "# include <stdlib. h> void Test1 () {// tail insertion print end deletion header deletion clear chain table SListNode * list = NULL; PushBack (list, 1); PushBack (list, 2 ); pushBack (list, 3); PushBack (list, 4); PrintSlist (list); PopBack (list); PrintSlist (list); PushFront (list, 0 ); printSlist (list); PopFront (list); PrintSlist (list); DestoryList (list); PrintSlist (list);} void Test2 () {// find a node insert a node in a certain position delete a node SListNode * list = NULL; PushBack (list, 1); PushBack (list, 2); PushBack (list, 3); PushBack (list, 4); PrintSlist (list); SListNode * pos = Find (list, 2); Insert (pos, 0); PrintSlist (list ); erase (list, Find (list, 0); PrintSlist (list);} void Test3 () {SListNode * list = NULL; PushBack (list, 1); PushBack (list, 2); PushBack (list, 3); PushBack (list, 4); PushBack (list, 5); PushBack (list, 6); PrintSlist (list ); // delete a non-tail node of a single-chain table with no headers/* SListNode * pos = Find (list, 2); DelNonTailNode (pos); PrintSlist (list ); * /// insert a node before a non-header node in the single-strand table/* SListNode * pos = Find (list, 2); InsertFrontNode (pos, 0 ); printSlist (list); * // search for intermediate nodes // PrintSlist (FindMidNode (list); // search for the k-last node // SListNode * ret = FindKNode (list, 2); // PrintSlist (ret); // print a single-chain table (recursive) upside down // PrintTailToHead (list ); // Reverse single-chain table // SListNode * ret = Reverse (list); // PrintSlist (ret); // PrintSlist (Reverse _ (list);} void Test4 () {// merge two ordered linked lists (still ordered after merging) SListNode * list = NULL; PushBack (list, 4); PushBack (list, 2); PushBack (list, 1 ); pushBack (list, 4); PrintSlist (list); Sort (list); PrintSlist (list);/* SListNode * list1 = NULL; PushBack (list1, 2 ); pushBack (list1, 3); PushBack (list1, 3); PushBack (list1, 0); PrintSlist (list); Sort (list1); PrintSlist (list1 ); SListNode * ret = Merge (list, list1); PrintSlist (ret); PrintSlist (list); PrintSlist (list1); */} int main () {// Test1 (); // Test2 (); // Test3 (); Test4 (); system ("pause"); return 0 ;}

 

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.