Chapter II: 3. Expression and implementation of static linked list---linear table

Source: Internet
Author: User

Objective:

Because there is no "pointer" type in some high-level programming languages, the single-linked list described in the previous section with pointers cannot be implemented, and we will use another form of linked lists: static linked lists.

Directory:

1. Chain representation and implementation of linear tables

1.1 Linear Linked List

    Single-linked list (pointer-type linear linked list)

    Static linked list

1.2 Circular Link List

1.3 Doubly linked list

Body:

Static single-linked list storage structure for linear tables:

#define MAXSIZE 100; Maximum length of a linked list

typedef struct{

Elemtype data;

int cur;

}component, Slinklist[maxsize];

Where a component of an array represents a node. The cursor (that is, indicator cur) is used in place of the pointer to indicate the relative position of the node in the array.

The No. 0 component of the array looks at the head node. Its pointer field indicates the first node of a linked list.

As shown: This storage structure requires pre-allocation of a large storage space, but in the insertion and deletion of linear tables do not need to move elements, just need to modify pointers, it is still with the chain structure of the main advantages.

    

Assuming that S is a variable of type slinklist, s[0].cur indicates the position of the first node in the array, S[s[0].cur].data is the first Data element, and S[s[0].cur].cur indicates the position of the second data element in the array. If the component I represents the K-node of the linked list, then S[i].cur is the location of the k+1 node.

    The key to the static list implementation of INSERT and delete operations is that we want to know which components are already in use and which ones are not used in the entire allocated area. The solution is to chain all unused and deleted components to an alternate list, making the first node from the alternate list A new node to be inserted whenever the insertion occurs, or, conversely, deleting the node from the linked list to the alternate list when it is deleted.


Code implementation:

Note: The implementation function here is:,//Input the collection element by the terminal, first establish a static linked list s for the collection A, and then enter the elements of set B while looking for the S table.
If the element is the same, the element is removed from s, otherwise the element is inserted into the S-linked list.

Other Listinsert, Listdelete, Getelem ... and other operations, according to the static linked list storage characteristics can be easily implemented.

#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE-1
#define OVERFLOW-2
Maximum length of a linked list
#define MAXSIZE 100

Status is the type of function whose value is the function result status code
typedef int STATUS;
typedef int ELEMTYPE;

Storage structure of static linked list
typedef struct{
Elemtype data;
int cur;
}component, Slinklist[maxsize];


Initialize the array space to a linked list
void Initspace_sl (Slinklist &space) {
for (int i=0;i< (MAXSIZE-1); i++) {
space[i].cur=i+1;
}
Point the cursor pointer of the last element to 0
space[maxsize-1].cur=0;

}


Gets the node from the alternate list, returns the array subscript corresponding to the assigned node, and returns 0 if all the space has been exhausted
int Malloc_sl (slinklist &space) {
if (space[0].cur!=0) {//That is, the standby chain is not empty
int i=space[0].cur; //
Space[0].cur=space[i].cur; The alternate chain head node points to the second node (that is, the first node is deleted).
return i; Returns the array subscript corresponding to the first node of the alternate chain
}
return 0; Returns 0 if no space is available for the backup chain
}

The node labeled K is recycled into the backup chain.
void Free_sl (Slinklist &space,int k) {
That is, insert the first node of the alternate chain, and the array subscript k corresponds to the node.
Space[k].cur=space[0].cur;
Space[0].cur=k;
}

The collection element is entered by the terminal, the static linked list s representing the set A is established, and the s table is then searched while the elements of the set B are entered.
If the element is the same, the element is removed from s, otherwise the element is inserted into the S-linked list.

Test data A has 4 elements: 1,2,3,4
B has 3 elements: 2,6,7 The expected result is: 1,3,4,6,7
void Diffrence (slinklist &space,int &s) {
INITSPACE_SL (space);
S=MALLOC_SL (space); S is the head node.
int r=s; R points to the last node.
int m,n;
Enter the number of elements in a and B collections
printf ("%s", "Please enter the total number of elements of a collection:");
scanf ("%d", &m);
printf ("%s", "Enter the total number of collection elements in B:");
scanf ("%d", &n);
CV set a linked list S
for (int i=0;i<m;i++) {
Entering data from a terminal
int data;
printf ("%s", "Enter a set element value:");
scanf ("%d", &data);
int CUR=MALLOC_SL (space); Take the node from the alternate list and return to the subscript.
Space[cur].data=data; Assign a value to a new node that is returned
Space[r].cur=cur; Add a new node to the end of the S-chain
R=cur; R continues to point to the last node
}
space[r].cur=0;

Return
Input set B and compare with S
for (int j=0;j<n;j++) {
int data;
printf ("%s", "Enter B set element value:");
scanf ("%d", &data);
int f=s; F points to the head node of the S-chain

int equalflag=0; Set the node equality identifier, default not equal, if there is equality change to 1
while (Space[f].cur) {
if (data==space[space[f].cur].data) {
int t=space[f].cur;
if (space[f].cur==r)//If the trailing boundary point is removed, then r points to the new tail node.
R=f;
Space[f].cur=space[space[f].cur].cur; Delete a node.
FREE_SL (space,t); Recycling nodes
equalflag=1; changing identifiers
Break
}
F=space[f].cur; Pointer moves back
}
if (equalflag==0) {
int CUR=MALLOC_SL (space);
Space[cur].data=data;
space[cur].cur=0;
Space[r].cur=cur;
R=cur;
}
}


}//time complexity of O (MXN)


Print all data for a linked list
void Printall (slinklist &space,int &s) {
int r=space[s].cur;
while (R) {
printf ("Subscript: [%d]", R);
printf ("Data:%d", space[r].data);
printf ("Cursor pointer: [%d]\n", space[r].cur);
R=space[r].cur;
}
}


void Main () {
Slinklist SL;
int S;
Diffrence (sl,s);
Printall (sl,s);
}

Operation Result:

   

Summarize:

Static linked list "Static" refers to the static linked list is not like a single linked list every time you add an element to open up memory space, but in advance to open up a large space for it.

Then, depending on whether the new node is added to open up new memory space, and static linked list of the corresponding single-linked list can also be called dynamic linked list.

Chapter II: 3. Expression and implementation of static linked list---linear table

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.