aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/kernel/slist.c
blob: 392f08a9aee1d293a569e3f98c68c8ad8199576f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
        #include "slist.h"
        #include "threads/malloc.h"
#include <stdio.h>
//        #include <stdlib.h>

        /* List structure */
        struct Node
        {
            ListElement Element;
            Position    Next;
        };

		/* make empty list */

        SList
        MakeEmpty( SList L )
        {
            if( L != NULL )
                DeleteList( L );
            L = malloc( sizeof( struct Node ) );
            if( L == NULL ) {
                printf( "Out of memory!\n" );
				return NULL;
			}
            L->Next = NULL;
            return L;
        }

        /* Return true if L is empty */

        int
        IsEmpty( SList L )
        {
            return L->Next == NULL;
        }

        /* Return true if P is the last position in SList L */
        /* Parameter L is unused in this implementation */

        int IsLast( Position P, UNUSED SList L)
        {
            return P->Next == NULL;
        }

        /* Return Position of X in L; NULL if not found */

        Position
        Find( ListElement X, SList L )
        {
            Position P;

	        P = L->Next;
            while( P != NULL && P->Element != X )
               P = P->Next;

            return P;
        }

        /* Delete from a SList */
        /* Cell pointed to by P->Next is wiped out */
        /* Assume that the position is legal */
        /* Assume use of a header node */

        void
        Delete( ListElement X, SList L )
        {
            Position P, TmpCell;

            P = FindPrevious( X, L );

            if( !IsLast( P, L ) )  /* Assumption of header use */
            {                      /* X is found; delete it */
                TmpCell = P->Next;
                P->Next = TmpCell->Next;  /* Bypass deleted cell */
                free( TmpCell );
            }
        }

        /* If X is not found, then Next field of returned value is NULL */
        /* Assumes a header */

        Position
        FindPrevious( ListElement X, SList L )
        {
            Position P;

            P = L;
            while( P->Next != NULL && P->Next->Element != X )
                P = P->Next;

            return P;
        }

        /* Insert (after legal position P) */
        /* Header implementation assumed */
        /* Parameter L is unused in this implementation */

        void
        Insert( ListElement X, UNUSED SList L, Position P )
        {
            Position TmpCell;

            TmpCell = malloc( sizeof( struct Node ) );
            if( TmpCell == NULL ) {
                printf( "Out of space!!!\n" );
				return;
			}

            TmpCell->Element = X;
            TmpCell->Next = P->Next;
            P->Next = TmpCell;
        }

        /*  DeleteSList algorithm */

        void
        DeleteList( SList L )
        {
            Position P, Tmp;

            P = L->Next;  /* Header assumed */
            L->Next = NULL;
            while( P != NULL )
            {
                Tmp = P->Next;
                free( P );
                P = Tmp;
            }
        }

        Position
        Header( SList L )
        {
            return L;
        }

        Position
        First( SList L )
        {
            return L->Next;
        }

        Position
        Advance( Position P )
        {
            return P->Next;
        }

        ListElement
        Retrieve( Position P )
        {
            return P->Element;
        }