nextupprevious
Next:2.2.2 Pointer Implementation of ListsUp:2.2 Implementation of ListsPrevious:2.2 Implementation of Lists

2.2.1 Array Implementation of Lists

Position is of type integer and has the range 0 to maxlength-1



# define maxlength 1000

typedef int elementtype; /* elements are integers */

typedef struct list-tag {

                elementtype elements [maxlength];

                    int last;

        } list-type;
end(L)
        int end (list-type *$ \ell$p)
        {
            return ($ \ell$$ \rightarrow$ last + 1)
        }



 
 

Insert (x, p,L)

void insert (elementtype x ; int p ; list-type *$ \ell$p) ;

        {

            int v; /* running position */

            if ($ \ell$$ \rightarrow$ last >= maxlength-1)

                    error (``list is full'')

            elseif ((p < 0) || (p > $ \ell$$ \rightarrow$ last + 1))

                    error (position does not exist)

                    else

                    for (q = $ \ell$$ \rightarrow$ last ; q <= p, q-)

                        $ \ell$$ \rightarrow$ elements [q + 1] = $ \ell$$ \rightarrow$ elements [q] ;

                    $ \ell$$ \rightarrow$ last = $ \ell$$ \rightarrow$ last + 1 ;

                    $ \ell$$ \rightarrow$ elements [p] = x

}
 
 

Delete (p, L)

void delete (int p ; list-type *$ \ell$p)

{

                int q ; /* running position */

                if ((p > $ \ell$$ \rightarrow$ last) || (p < 0))

                    error (``position does not exist'')

                else /* shift elements */ {

                    $ \ell$$ \rightarrow$ last - ;

                    for (q = p ; q <= $ \ell$$ \rightarrow$ last; q ++)

                        $ \ell$$ \rightarrow$ elements [q] = $ \ell$$ \rightarrow$ elements [q+1]

                }

}
 
 

Locate (x, L)

int locate (element type *x ; list-type *$ \ell$p)

            {

                int q ;

                for (q = 0 ; q <= $ \ell$$ \rightarrow$ last ; q++)

                    if ($ \ell$$ \rightarrow$ elements [q] = = x]

                        return (q) ;

                    return ($ \ell$$ \rightarrow$ last + 1) /* if not found */

}
 


nextupprevious
Next:2.2.2 Pointer Implementation of ListsUp:2.2 Implementation of ListsPrevious:2.2 Implementation of Lists
eEL,CSA_Dept,IISc,Bangalore