next up previous contents
Next: Stacks: ADT Up: Lists: ADT Previous: Array Lists: list   Contents

Pointer Lists: Introduction [14]

typedef struct node *node_ptr;
struct node {                           struct list_record {
  element_type element;                   node_ptr head;
  node_ptr next;                          node_ptr tail;
  node_ptr prev;                        }; 
}; 
typedef node_ptr position;              typedef struct list_record *LIST;




\begin{picture}(756,340)(20,480)
\thicklines\put( 40,500){\vector( 0, 1){ 15}}
\...
...0,545){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm header}}}
\end{picture}



Pointer Lists: Introduction [15]




\begin{picture}(711,299)(5,520)
\thicklines\put(100,780){\framebox (40,20){}}
\p...
...,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm Ex8: I(ntersection L,L1}}}
\end{picture}



Pointer Lists: list1 and list2 [16]

Finish Coding: list1.c [``Good'' executable is list2]

% gcc -o list1 list1.c                                 > tcc list1.c

Same Data File with Test Cases: list.dat

% list2 list.dat
h(elp; q(uit; H(ead x; D(elete x; d(isplay; t(ail x; T(ail x; 
o(rdered x; b(ackward; a(nother; I(ntersect
> c b a        > e d c a

New Data File with Test Cases: list1.dat

o o  o c  o r  o r  o x  D x  t e  T c  T t  d q

% list2 list1.dat
> c o r r e c t

New Data File with Test Cases: list2.dat

o i  o h  o t  o l  d  b  a  o h  o i  o a  d I q

% list2 list2.dat
> h i l t     > t l i h
> a h i 
> h i



Pointer Lists: list1.c [17]

int is_empty(LIST L) { return(L->head->next == NULL); }
int is_last(position p) { return(p->next == NULL); }
position find(element_type x, LIST L) {
position p = NULL;
  if (L != NULL) {
    p = L->head->next;
    while ( (p != NULL) && (p->element !=x) )
      p = p->next;
  }
  return p;
}
/* Ex2 : insert x at the head of the list */
void head_insert(element_type x, LIST L) { }
/* Ex3 : print all the elements except the header */
 void display_list(LIST L) { }
/* Ex4 : scan from the head and insert x at the end of the list */
void tail_insert(element_type x, LIST L) { }
/* Ex5 : insert x directly at the tail of the list using tail pointer */
void Tail_insert(element_type x, LIST L) { }
/* Ex6 : insert x in order (alpabetically) into the list */
void ordered_insert(element_type x, LIST L) { }
/* Ex8 : print out the intersection of L and L1 */
void intersect(LIST L, LIST L1) { }
/* Ex9 : using the prev pointers, print the list backwards */
void backward_list(LIST L) { }


Arrays: list1.c [18]
position find_previous(element_type x, LIST L) {
position p;
  p = L->head;
  while ( (p->next != NULL) && (p->next->element != x) )
    p = p->next;
  return p;
}

/* Ex1 : create both a pointer to a list and a header node */
/* Ex5 : the tail should also point to the header          */
/* Ex9 : initialize prev pointer                           */
LIST create_list() { return(NULL); }

/* Ex5 : what if the tail is deleted? */
/* Ex9 : take care of prev pointer    */
void delete(element_type x, LIST L) {
position p, tmp_cell;
  if (L != NULL) {
    p = find_previous(x,L);
    if (p->next != NULL) { 
      tmp_cell = p->next;
      p->next = tmp_cell->next; /* bypass */ 
      free(tmp_cell);
    }
} }


Arrays: list1.c [19]
/* Ex5 : what if a new element is inserted after the tail? */
/* Ex9 : take care of prev pointer */
void insert(element_type x, LIST L, position p) {
position tmp_cell;
  if (L != NULL) {
    tmp_cell = (position) malloc(sizeof(struct node));
    if (tmp_cell == NULL) 
      printf("Out of space!\n");
    else {
      tmp_cell->element = x;
      tmp_cell->next = p->next;
      p->next = tmp_cell;
} } }

/* Ex5 : tail should be reinitialized as in create_list */
void delete_list(LIST L) {
position p, tmp;
  if (L != NULL) {
    p = L->head->next;   
    L->head->next = NULL;
    while (p != NULL) {
      tmp = p->next;
      free(p);
      p = tmp;
} } }



Ted Billard 2001-10-25