/* This file implements a singly linked list. Copyright (C) 2004 The University of Texas at Austin This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Email: usman@cs.njit.edu Mail: Usman Roshan, Department of Computer Sciences, NJIT-CCS, Computer Science Department, GITC 4400, University Heights, Newark, NJ 07102 */ #include #include #include #include int size_list(list l){ assert(l); return l->size; } list new_list(){ list l; l = (list) malloc(sizeof(struct list_struct)); l->head = 0; l->tail = 0; l->size = 0; return l; } list_node new_list_node(void* v){ list_node n; n = (list_node) malloc(sizeof(struct list_node_struct)); n->next=0; n->prev=0; n->data=v; return n; } void del_list_node(list_node n){ assert(n); /* MUST free data from main code by casting appropriately*/ assert(n->data == NULL); free(n); } void del_list(list l){ list_node n, next; assert(l); for(n=l->head;n;){ next = n->next; del_list_node(n); n=next; } free(l); } list_node append_list(list l, void* v){ list_node n; assert(l); n = new_list_node(v); if(!l->head){ l->head = n; l->tail = n; } else { l->tail->next = n; n->prev = l->tail; l->tail = n; } l->size++; return n; } list_node insert_list(list l, void* v, list_node n, int where){ list_node nn = new_list_node(v); assert(l); assert(n); /*where=0 means insert before n and where=1 means after n*/ if(where == 0){ if(l->head == n){ nn->next = n; nn->prev = n->prev; n->prev = nn; l->head=nn; } else{ nn->next=n; nn->prev=n->prev; n->prev->next=nn; n->prev = nn; } } else if(where == 1){ if(l->tail == n){ nn->prev=n; nn->next=n->next; n->next=nn; l->tail=nn; } else { nn->prev=n; nn->next=n->next; n->next->prev = nn; n->next=nn; } } l->size++; return nn; } void* pop_list(list l){ void* d; list_node n; assert(l); assert(l->head != 0); d = l->head->data; n = l->head->next; del_list_node(l->head); l->head = n; if(l->head) l->head->prev = 0; l->size--; return d; } void push_list(list l, void* d){ list_node n; assert(l); n = new_list_node(d); if(!l->head){ l->head=n; l->tail=n; } else{ n->next=l->head; l->head->prev=n; l->head=n; } l->size++; }