"C + + version of data structure" for the implementation of static linked list

Source: Internet
Author: User

Static linked list to solve the problem is: How to statically simulate the dynamic list of storage space request and release , dynamic linked list can be achieved with the help of malloc and free two functions. In a static linked list, since the operation is an array, there is no node request and release problem like the dynamic list, so we have to do two functions to simulate these two actions.

Workaround:

divide the static list into " Valid linked list, alternate list ", through both the application and release of the simulation node


Static linked list:

1) List of valid links (array elements already used are linked by cursor cur)

2) alternate list (unused array elements are linked by cursor cur)

MALLOC_SL (Application node): Get a node from the backup list

FREE_SL (Release node): Connect the freed node to the alternate linked list


The implemented static linked list the structure is as follows:



Specific implementations refer to the following code:

StaticList.h

Static linked list//array The first element and the last element specialization processing//1:s[0].cur The index of the first node that holds the alternate list (the head node of the alternate list), the non-0 indicates the existence of the alternate list, and 0 indicates that there is no alternate list//2:s[maxsize-1]. Cur the subscript (equivalent to the head node) of the first node with a value of 0 means that there is a valid linked list, 0 indicates that there is no valid linked list #include<iostream> #include <cstdlib> #include < cassert>using namespace std;typedef enum{false,true}status;typedef int elemtype; #define MAXSIZE 10typedef struct Staticnode{elemtype data;int cur;} Staticnode;typedef staticnode staticlist[maxsize];void init_sl (staticlist &sl) {for (int i = 0; i < MAXSIZE-1; ++i ) {sl[i].cur = i + 1;} Sl[maxsize-1].cur = 0;//The static linked list is empty, there is no valid node, so the pointer to the active list's head node is null (0)}//success: Return to the node's subscript//failure: Return 0int MALLOC_SL (staticlist &AMP;SL) {int i = sl[0].cur;if (i! = 0)//exists alternate list {sl[0].cur = sl[i].cur;//alternate list uses a node, uses its next node for standby}return i;} The node that will be freed, the header is inserted into the alternate list of void Free_sl (Staticlist &sl,int k) {sl[k].cur = Sl[0].cur;sl[0].cur = k;} void Show_sl (Staticlist SL) {int i = sl[maxsize-1].cur;//find subscript for first valid node while (i! = 0) {cout << sl[i].data << "-- > "; i = Sl[i].cur;} cout << "Nul." << Endl;} StatusPush_back (staticlist &AMP;SL, elemtype x) {int i = MALLOC_SL (SL), if (i = = 0) {cout << "static list full" <<x<< "cannot be interpolated" << Endl;return FALSE;} Sl[i].data = X;sl[i].cur = 0;int J = sl[maxsize-1].cur;//Look for the last node (cur of the last node is 0), and the tail plug while (sl[j].cur! = 0) {j = Sl[j].cur ;} Sl[j].cur = I;return TRUE;} Status Push_front (staticlist &sl, elemtype x) {int i = MALLOC_SL (SL); if (i = = 0) {cout << "static list full" << x < < "Unable to insert header" << Endl;return FALSE;} Sl[i].data = X;sl[i].cur = Sl[maxsize-1].cur;sl[maxsize-1].cur = I;return TRUE;} Status Pop_front (staticlist &sl) {if (sl[maxsize-1].cur = = 0) {cout << "static linked list is empty, cannot header delete" << Endl;return FALSE; }int i = Sl[maxsize-1].cur;sl[maxsize-1].cur = Sl[i].cur; FREE_SL (SL, i);//sl[maxsize-1].cur = sl[i].cur;//removed from the active node//sl[i].cur = sl[0].cur;//Inserts the deleted node header into the alternate list//sl[0].cur = i; return TRUE;} Status Pop_back (staticlist &sl) {if (sl[maxsize-1].cur = = 0) {cout << "static linked list is empty, cannot header delete" << Endl;return FALSE;} int i = MAXSIZE-1;while (Sl[sl[i].cur].cur! = 0)//search for the last node of the precursor {i = sl[i].cur;} int tmp = sl[i].cur;//The coordinates of the last node of the active list (save first) Sl[i].cur = 0;//Delete the tail node FREE_SL (sl,tmp) in the active list;//sl[tmp].cur = sl[0].cur;//sl[ 0].cur = Tmp;return TRUE;}

main.cpp

#include "StaticList.h" int main () {staticlist SL;INIT_SL (SL); for (int i = 0; i < 5; ++i) {//push_back (SL, i); Push_front (SL, i);} SHOW_SL (SL); for (int i = 0; i < 5; ++i) {//pop_front (SL); Pop_back (SL); SHOW_SL (SL);} System ("pause"); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"C + + version of data structure" for the implementation of static linked list

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.