aboutsummaryrefslogtreecommitdiff
path: root/DSAA/chap3_lists_stacks_queues/list.c
blob: 23848bcd04f9a7ab3b18dc3f084789ce650d2b36 (plain)
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
#include <stdio.h>
#include <stdlib.h>

#include "list.h"

/* Return true if L is empty */

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

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

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

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

Position
Find( ElementType X, List L )
{
        Position P;

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

        return P;
}

/* Delete first occurrence of X from a list.
 * Assume use of a header node */

void Delete( ElementType X, List L )
{
        Position P, TmpCell;

        P = FindPrevious(X, L);

        if ( !IsLast(P, L) )
        {
                TmpCell = P->Next;
                P->Next = TmpCell->Next;
                free( TmpCell );
        }
}

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

Position
FindPrevious( ElementType X, List 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.
 * Return the position of the inserted node */

Position
Insert( ElementType X, List L, Position P )
{
        Position TmpCell;

        TmpCell = malloc( sizeof( struct Node ) );
        if( TmpCell == NULL )
                printf( "Out of space!" );

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

        return TmpCell;
}

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

        P = L->Next;
        L->Next = NULL;

        while( P != NULL )
        {
                Tmp = P->Next;
                free( P );
                P = Tmp;
        }
}

void ConstructList( List L, int Elements[], int Num )
{
        int i;

        for( i = 0; i < Num; i++ )
        {
                Insert(Elements[i], L, L);
        }
}
void
PrintList( List L )
{
        Position P;
        P = L->Next;

        while( P != NULL )
        {
                printf( "%d->", P->Element );
                P = P->Next;
        }

        printf( "\n" );
}

/*int*/
/*main()*/
/*{*/
        /*Position P;*/
        /*List L;*/

        /*L = malloc( sizeof (struct Node) );*/
        /*Insert(3,L, L);*/
        /*Insert(5,L, L);*/
        /*Insert(8, L, L);*/
        /*P = FindPrevious(3, L);*/
        /*PrintList(L);*/
/*}*/