How to Implement static linked list using C language

Source: Internet
Author: User

Before I started, I thought there was no difference between static and dynamic linked lists. I thought about it carefully and found that memory management was a very important topic to be discussed in the original static linked list.

The "static" character of the static linked list refers to the Static Memory Source (usually using a global array ). Unlike the Dynamic Linked List, the application and release of node memory in the static linked list must be maintained by yourself. Because it is a linked list, it is easy to think of connecting spare nodes to form a free_list, each time you need to retrieve a node from the free_list header, and then add the node to the header when you release the node. This makes it easy to implement other operations on the linked list.

Copy codeThe Code is as follows: // static Linked List Implementation
# Include <stdio. h>

# Define MAXN 16 // capacity of list.
Typedef int element; // element type.

// Define boolean type:
Typedef int bool;
# Define true-1
# Define false 0

# Define NPTR-1 // null pointer definition. can not between 0 to MAXN-1.
Typedef int pointer;

# Define DEBUGVAL (x) printf ("% s: % d \ n", # x, (x); // a macro for debug.

Struct _ node
{
Element data;
Pointer next;
} SLList [MAXN];
Pointer ifree, idata;

# Define nextof (p) SLList [p]. next
# Define dataof (p) SLList [p]. data

# Define _ alloc (d) ifree; dataof (ifree) = (d); ifree! = NPTR? Ifree = nextof (ifree): NPTR
# Define _ free (p) nextof (p) = ifree; ifree = p

Void init ()
{
Int I;
Ifree = 0;
Idata = NPTR;
For (I = 0; I <MAXN-1; I ++)
Nextof (I) = I + 1;
Nextof (I) = NPTR;
}

// Clear all nodes.
Void clear () {init ();}

// Push val to front.
Bool push_front (element val)
{
Pointer tmp, np;
If (ifree! = NPTR ){
Np = _ alloc (val );
Nextof (np) = idata;
Idata = np;
Return true;
}
Return false;
}

// Push val to end of list.
Bool push_back (element val)
{
If (idata = NPTR) {// empty table, write directly
Idata = _ alloc (val );
Nextof (idata) = NPTR;
Return true;
}
If (ifree! = NPTR) {// non-null. First, locate the last node.
Pointer last = idata, np;
While (nextof (last )! = NPTR) last = nextof (last );
Np = _ alloc (val );
Nextof (np) = NPTR;
Nextof (last) = np;
Return true;
}
Return false;
}

// Insert val to after p pointed node.
Bool insert_after (pointer p, element val)
{
If (ifree! = NPTR & p! = NPTR ){
Pointer pn = _ alloc (val );
Nextof (pn) = nextof (p );
Nextof (p) = pn;
Return true;
}
Return false;
}

// Insert to the position in front of p.
Bool insert (pointer ptr, element val)
{
If (ifree = NPTR) return false; // No node, directly return
If (ptr = idata) {// There is a node
Pointer np = _ alloc (val );
Nextof (np) = idata;
Idata = np;
Return true;
}
Else {// in other cases, first find the ptr precursor and then insert
Pointer p = idata;
While (p! = NPTR ){
If (nextof (p) = ptr) {// find p -- the prev node of ptr.
Return insert_after (p, val); // insert val after p.
}
P = nextof (p );
}
}
Return false;
}

// Find element, return the prev node pointer.
Pointer find_prev (element val)
{
Pointer p = idata;
While (p! = NPTR ){
If (dataof (nextof (p) = val)
Return p;
P = nextof (p );
}
Return NPTR;
}

// Find element, return the node pointer.
Pointer find (element val)
{
Pointer p = idata;
While (p! = NPTR ){
If (dataof (p) = val) return p;
P = nextof (p );
}
Return NPTR;
}

// Pop front element.
Void pop_front ()
{
If (idata! = NPTR) {// move the node at the top of the data list to the free list
# If 0
Pointer p = idata;
Idata = nextof (idata); // idata = nextof (idata );
Nextof (p) = ifree; // SLList [p]. next = ifree;
Ifree = p;
# Else
Pointer p = idata;
Idata = nextof (idata );
_ Free (p );
# Endif
}
}

// Pop back element.
Void pop_back ()
{
If (idata = NPTR) return;
If (nextof (idata) = NPTR) {// only 1 node.
Nextof (idata) = ifree;
Ifree = idata;
Idata = NPTR;
}
Else {// locate the last node p and its precursor q.
// TODO: find the last node p, and it's perv node q.
Pointer p = idata, q;
While (nextof (p )! = NPTR ){
Q = p;
P = nextof (p );
}
// Remove * p to free list, update nextof (q) to NPTR.
Nextof (p) = ifree;
Ifree = p;
Nextof (q) = NPTR;
}
}

Void show ()
{
Pointer p = idata;
For (; p! = NPTR; p = nextof (p )){
Printf ("% 3d", dataof (p ));
}
Printf ("\ n ");
}

# Define INFOSHOW
Void info ()
{
# Ifdef INFOSHOW
Int I;
DEBUGVAL (ifree );
DEBUGVAL (idata );
Puts ("=================================\ n"
"Index \ tdata \ tnext \ n"
"--------------------");
For (I = 0; I <MAXN; I ++ ){
Printf ("% d \ t % d \ n", I, SLList [I]. data, SLList [I]. next );
}
Puts ("=================================\ n ");
# Endif
}

/*
Test procedure:
*/
Int main ()
{
Int I;
Init ();

# If 1 // push_front test:
Puts ("push_front test :");
For (I = 0; I <MAXN + 2; I ++ ){
Push_front (2 * I + 1 );
Show ();
}

Puts ("pop_front test :");
For (I = 0; I <MAXN + 2; I ++ ){
Pop_front ();
Show ();
}
# Endif

# If 1 // push_back test:
Puts ("push_back test :");
For (I = 0; I <MAXN + 2; I ++ ){
Push_back (I + 1) * 10 );
Show ();
}

Puts ("pop_back test :");
For (I = 0; I <MAXN + 1; I ++)
{
Pop_back ();
Show ();
}
# Endif

# If 1 // insert test:
Puts ("insert test :");
For (I = 0; I <MAXN + 2; I ++)
{
Insert (idata, (I + 1) * 10 );
Show ();
}
Puts ("clear... \ n ");
Clear ();
# Endif

# If 1 // insert_after test:
Puts ("insert_after test :");
Push_back (-99 );
For (I = 0; I <MAXN + 1; I ++ ){
Insert_after (idata, I + 1 );
Show ();
}
Puts ("clear... \ n ");
Clear ();
# Endif

# If 1 // find test:
Puts ("find test :");
For (I = 0; I <MAXN/2; I ++ ){
Push_front (MAXN-I );
Push_back (MAXN/2-i );
// Show ();
}
Show ();
Info ();
For (I = 0; I <MAXN; I ++ ){
Int val = rand () % (2 * MAXN );
Pointer p = find (val );
If (p! = NPTR)
Printf ("% 3d % 3d found at % d \ n", val, dataof (p), p );
Else
Printf ("% 3d not found \ n", val );
}
# Endif

# If 1
Puts ("\ nfind_prev test :");
For (I = 0; I <MAXN; I ++ ){
Int val = rand () % (2 * MAXN );
Pointer p = find_prev (val );
If (p! = NPTR)
Printf ("% 3d % 3d found at % d's next. \ n", val, dataof (nextof (p), p );
Else
Printf ("% 3d not found \ n", val );
}
# Endif

# If 1 // find_prev and insert_after test:
Clear ();
Puts ("\ nfind_prev and insert_after test :");
For (I = 0; I <MAXN/2; I ++ ){
Push_front (MAXN/2-i );
}
Show ();
For (I = 0; I <MAXN/2; I ++ ){
Int val = rand () % (2 * MAXN), n =-(I + 1 );
Pointer p = find_prev (val );
If (p! = NPTR ){
Printf ("insert % d to front of % d:", n, val );
Insert_after (p, n );
Show ();
}
}
# Endif

# If 1 // find and insert test:
Clear ();
Puts ("\ nfind and insert test :");
For (I = 0; I <MAXN/2; I ++ ){
Push_front (MAXN/2-i );
}
Show ();
For (I = 0; I <MAXN/2; I ++ ){
Int val = rand () % MAXN, n =-(I + 1 );
Pointer p = find (val );
If (p! = NPTR ){
Printf ("insert % d to after of % d:", n, val );
Insert_after (p, n );
Show ();
}
}
# Endif

Puts ("end of main ().");
Return 0;
}

//

The test results are as follows:

Copy codeThe Code is as follows: push_front test:
1
3 1
5 3 1
7 5 3 1
9 7 5 3 1
11 9 7 5 3 1
13 11 9 7 5 3 1
15 13 11 9 7 5 3 1
17 15 13 11 9 7 5 3 1
19 17 15 13 11 9 7 5 3 1
21 19 17 15 13 11 9 7 5 3 1
23 21 19 17 15 13 11 9 7 5 3 1
25 23 21 19 17 15 13 11 9 7 5 3 1
27 25 23 21 19 17 15 13 11 9 7 5 3 1
29 27 25 23 21 19 17 15 13 11 9 7 5 3 1
29 27 25 23 21 19 17 15 13 11 9 7 5 3 1
29 27 25 23 21 19 17 15 13 11 9 7 5 3 1
Pop_front test:
27 25 23 21 19 17 15 13 11 9 7 5 3 1
25 23 21 19 17 15 13 11 9 7 5 3 1
23 21 19 17 15 13 11 9 7 5 3 1
21 19 17 15 13 11 9 7 5 3 1
19 17 15 13 11 9 7 5 3 1
17 15 13 11 9 7 5 3 1
15 13 11 9 7 5 3 1
13 11 9 7 5 3 1
11 9 7 5 3 1
9 7 5 3 1
7 5 3 1
5 3 1
3 1
1

Push_back test:

20
20 30
20 30 40
20 30 40 50
20 30 40 50 60
20 30 40 50 60 70
20 30 40 50 60 70 80
20 30 40 50 60 70 80 90
20 30 40 50 60 70 80 90 100
20 30 40 50 60 70 80 90 100 110
20 30 40 50 60 70 80 90 100 110
20 30 40 50 60 70 80 100 110 120
20 30 40 50 60 70 80 100 110 120 130
20 30 40 50 60 70 80 100 110 120 130 140
20 30 40 50 60 70 80 100 110 120 130 140 150
20 30 40 50 60 70 80 100 110 120 130 140 150
20 30 40 50 60 70 80 100 110 120 130 140 150
Pop_back test:
20 30 40 50 60 70 80 100 110 120 130 140
20 30 40 50 60 70 80 100 110 120 130
20 30 40 50 60 70 80 100 110 120
20 30 40 50 60 70 80 90 100 110
20 30 40 50 60 70 80 90 100 110
20 30 40 50 60 70 80 90 100
20 30 40 50 60 70 80 90
20 30 40 50 60 70 80
20 30 40 50 60 70
20 30 40 50 60
20 30 40 50
20 30 40
20 30
20

Insert test:

10
20 10
30 20 10
40 30 20 10
50 40 30 20 10
60 50 40 30 20 10
70 60 50 40 30 20 10
80 70 60 50 40 30 20 10
90 80 70 60 50 40 30 20 10
100 90 80 70 60 50 40 30 20 10
110 100 90 80 70 60 50 40 30 20 10
120 110 100 90 80 70 60 50 40 30 20 10
130 120 110 100 90 80 70 60 50 40 30 20 10
140 130 120 110 100 90 80 70 60 50 40 30 20 10
150 140 130 120 110 90 80 70 60 50 40 30 20 10
150 140 130 120 110 90 80 70 60 50 40 30 20 10
150 140 130 120 110 90 80 70 60 50 40 30 20 10
Clear...

Insert_after test:
-99 1
-99 2 1
-99 3 2 1
-99 4 3 2 1
-99 5 4 3 2 1
-99 6 5 4 3 2 1
-99 7 6 5 4 3 2 1
-99 8 7 6 5 4 3 2 1
-99 9 8 7 6 5 4 3 2 1
-99 10 9 8 7 6 5 4 3 2 1
-99 11 10 9 8 7 6 5 4 3 2 1
-99 12 11 10 9 8 7 6 5 4 3 2 1
-99 13 12 11 10 9 8 7 6 5 4 3 2 1
-99 14 13 12 11 10 9 8 7 6 5 4 3 2 1
-99 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
-99 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
-99 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Clear...

Find test:
10 11 12 13 14 15 16 8 7 6 5 4 3 2 1
Ifree:-1
Idata: 14
================================
Index data next
--------------------
16 1
8 3
15 0
7 5
14 2
6 7
13 4
5 9
12 6
4 11
11 8
3 13
10 10
2 15
9 12
1-1
================================
9 found at 14
3 found at 11
Not found
4 found at 9
1 found at 15
12 found at 8
Not found
14 found at 4
Not found
16 found at 0
9 found at 14
Not found
Not found
Not found
9 found at 14
11 found at 10

Find_prev test:
Not found
6 found at 3's next.
Not found
Not found
7 found at 1's next.
12 found at 10's next.
Not found
Not found
4 found at 7's next.
Not found
13 found at 8's next.
Not found
6 found at 3's next.
Not found
7 found at 1's next.
Not found

Find_prev and insert_after test:
2 3 4 5 6 7 8
Insert-4 to front of 8: 1 2 3 4 5 6 7-4 8
Insert-5 to front of 3: 1 2-5 3 4 5 6 7-4 8
Insert-8 to front of 6: 1 2-5 3 4 5-8 6 7-4 8

Find and insert test:
2 3 4 5 6 7 8
Insert-2 to after of 3: 1 2 3-2 4 5 6 7 8
Insert-6 to after of 8: 1 2 3-2 4 5 6 7 8-6
Insert-7 to after of 5: 1 2 3-2 4 5-7 6 7 8-6
End of main ().

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.