int main(int argc, char* argv[]) { int returned = -1; struct linkedList* q = createLinkedList(3); printf("Checking if the list is empty: %d\n", isEmptyList(q)); printf("Attempting to add to front \n"); addFrontList(q, 1); removeList(q, 1); printf("Checking if the list is empty: %d\n", isEmptyList(q)); addFrontList(q, 2); addFrontList(q, 3); _printList(q); printf("Attempting to add to back \n"); addBackList(q, 4); addBackList(q, 5); addBackList(q, 6); _printList(q); printf("Attempting to print front \n"); printf("%d\n", frontList(q)); printf("Attempting to print back \n"); printf("%d\n", backList(q)); printf("Attempting to remove front \n"); removeFrontList(q); _printList(q); printf("Attempting to remove back \n"); removeBackList(q); _printList(q); printf("Attempting to delete 4 \n"); removeList(q, 4); _printList(q); printf("Attempting to print list \n"); _printList(q); printf("Attempting to confirm contains 5 \n"); returned = containsList(q, 5); if (returned == 1) printf("true \n"); else printf("false \n"); return 0; }
int removeList(List* l, int index, ItemType* e) { if (index > l->size || index < 0) { return 0; } Node *node, *removed; node = createNode(); removed = createNode(); node = l->first; int i = 0; while (i < index) { i++; node = node->next; } if (index == 0) { removed = l->first; l->first = removed->next; } else if (index == sizeList(l)) { removed = node->next; node->next = NULL; l->last = node; } else { removed = node->next; node->next = removed->next; } if (isEmptyList(l)) { l->first = NULL; l->last = NULL; } *e = removed->data; free(node); free(removed); l->size--; return 1; }
int addList(List* l, ItemType e, int index) { if (index > sizeList(l) || index < 0) { return 0; } Node* node = createNode(); node->data = e; node->next = NULL; if (isEmptyList(l)) { l->first = node; l->last = node; } else { Node* iterador = createNode(); iterador = l->first; int i = 0; while (i < index) { i++; iterador = iterador->next; } if (index == 0) { node->next = iterador; l->first = node; } else if (index == sizeList(l)) { iterador->next = node; l->last = node; } else { node->next = iterador->next; iterador->next = node; } free(iterador); } l->size++; return 1; }
struct Lists *createLevelLinkedList(struct Tree *tree) { struct Lists *lists = (struct Lists *)malloc(sizeof(struct Lists)); if (lists == NULL) { fprintf(stderr, "createLevelLinkedList():malloc lists error.\n"); return NULL; } lists->first = NULL; lists->last = NULL; if (tree->root == NULL) { return lists; } struct List *current = createList(); addListNode(current, tree->root); while (!isEmptyList(current)) { addLists(lists, current); struct List *parent = current; current = createList(); struct ListNode *node = NULL; while ((node = getNode(parent, 1)) != NULL) { addListNode(current, node->value->left); addListNode(current, node->value->right); } } return lists; }
/* removeBackList param: lst the linkedList pre: lst is not null pre:lst is not empty post: size reduced by 1 */ void removeBackList(struct linkedList *lst) { /* FIXME: you must write this */ assert(lst!=NULL); assert(!isEmptyList(lst)); _removeLink(lst,lst->lastLink->prev); }
TYPE frontList(struct linkedList *lst) { //ensure the pointer parameters contain valid addresses assert(lst != 0); assert(!isEmptyList(lst)); return lst->firstLink->next->value; //get value data from the first complete link in the list }
void Delete (LIST * lp,int n) { NODE *p=lp->first,*q=NULL; if (isEmptyList(*lp)==0) return; if (p->info==n) DeleteFromFront(lp); else { while (p!=NULL) { q=p; p=p->next; if (p==NULL) { printf ("\nThe node with integer %d was not found.\n",n); return; } if (p->info==n) { printf ("\nThe node with integer %d was found and deleted\n",n); q->next=p->next; free (p); return; } } } return; }
TYPE backList(struct linkedList *lst) { /* FIXME: you must write this */ assert(lst != NULL); assert(!isEmptyList(lst)); return lst->lastLink->prev->value; }
void removeAllNodes(struct List *list){ while(isEmptyList(list)!=1){ popFront(list);// remove the first element until the end of the lsit } }
/* Function to print list Pre: lst is not null */ void _printList(struct linkedList* lst) { struct DLink *temp = malloc(sizeof(struct DLink)); if(isEmptyList(lst)) { printf("The list is empty.\n"); } else{ for(temp = lst->firstLink->next; temp != lst->lastLink; temp = temp->next) { printf("%i\n", temp->value); } /* while(p < n) { printf("%i\n", frontList(lst)); addBackList(lst, frontList(lst)); removeFrontList(lst); p++; }*/ /* FIXME: you must write this */ } }
void _removeLink(struct linkedList *lst, struct DLink *l) { if(isEmptyList(lst)){ printf("The list is empty\n"); } else{ if(l == lst->firstLink->next) { l = l->next; l->prev = lst->firstLink;; lst->firstLink->next = l; lst->size = lst->size - 1; } else if(l == lst->lastLink->prev) { l = l->prev; l->next = lst->lastLink; lst->lastLink->prev = l; lst->size = lst->size - 1; } } /* FIXME: you must write this */ }
/* removeFrontList param: lst the linkedList pre:lst is not null pre: lst is not empty post: size is reduced by 1 */ void removeFrontList(struct linkedList *lst) { /* FIXME: you must write this */ // printf("list size: %d", lst->size); assert(!isEmptyList(lst)); _removeLink(lst,lst->firstLink->next); }
/* Function for .... */ State dequeue(Queue *qp) { State s = firstItem(qp->list); qp->list = removeFirstNode(qp->list); if ( isEmptyList(qp->list) ) { qp->lastNode = NULL; } return s; }
void removeFrontList(struct linkedList *lst) { /* FIXME: you must write this */ assert(lst != NULL); assert(!isEmptyList(lst)); _removeLink(lst, lst->firstLink->next); }
/** Remove o elemento do início da lista. Retorna o elemento removido e nulo, caso a lista esteja vazia. */ void* removeFirstList(struct List * list) { if (isEmptyList(list))exit(1); ListNode *p = list->first; void* q = list->first->elem; list->first = list->first->next; free(p); return q; }
void removeBackList(struct linkedList *lst) { assert(lst != 0); //make sure lst contains a valid address if (!isEmptyList(lst)) { _removeLink(lst, lst->lastLink->prev); //remove link from the back of the list } }
void removeFrontList(struct linkedList *lst) { assert(lst != 0); //make sure lst contains a valid address if (!isEmptyList(lst)) { _removeLink(lst, lst->firstLink->next); //remove link from the front of the list } }
/* Function for .... */ void enqueue (State s, Queue *qp) { if ( isEmptyList(qp->list) ) { qp->list = addItem(s,NULL); qp->lastNode = qp->list; } else { (qp->lastNode)->next = addItem(s,NULL); qp->lastNode = (qp->lastNode->next); } }
void insertNodeToEndList(List *pList, ListNode *newNode) { if (isEmptyList(pList)) { pList->head = pList->tail = newNode; } else { pList->tail->next = newNode; pList->tail = newNode; } }
void PrintList (LIST l) { NODE *p; if (isEmptyList(l)==0) return; printf ("\n\nList:\n"); for (p=l.first; p!=NULL; p=p->next) printf ("[%d]\n",p->info); return; }
void _removeLink(struct linkedList *lst, struct DLink *l) { assert(lst != NULL); assert (l != NULL ); assert ( !isEmptyList(lst) ); // extra check (l->prev)->next = l->next; (l->next)->prev = l->prev; free(l); lst->size--; }
/** * Remove item da lista * @param l elemento do tipo t_lista */ void removeList(t_lista *l) { t_apont_no aux; if (! isEmptyList(l)) { aux = l->prim; l->prim = aux->prox; free(aux); } }
void printList (link ls) { link curr = ls; while (! isEmptyList (curr)) { printf ("->["); printItem (getItem (curr)); printf ("]"); curr = getNext (curr); } printf ("->[X]\n"); }
int main(int argc, char* argv[]) { printf("implement testLinkedList.c!\n"); struct linkedList *lst; lst = createLinkedList(); printf("isEmptyList?%i\n", isEmptyList(lst)); printf("\nTesting add links (3),5,6,9,8\n"); addList(lst, 5); printf("addList 5:%i\n", frontList(lst)); addBackList(lst, 6); printf("backList 6:%i\n", backList(lst)); addBackList(lst, 9); printf("backList 9:%i\n", backList(lst)); addFrontList(lst,3); printf("frontList 3:%i\n", frontList(lst)); addBackList(lst, 8); printf("backList 8:%i\n\n", backList(lst)); printf("frontList 3:%i\n", frontList(lst)); printf("backList 8:%i\n\n", backList(lst)); printf("remove the front(3) and back(8)...\n"); removeFrontList(lst); printf("frontList should now be 5:%i\n", frontList(lst)); removeBackList(lst); printf("backList should now be 9:%i\n", backList(lst)); printf("contains 5?%i\n",containsList(lst,5)); printf("removing 5..."); removeFrontList(lst); printf("contains 5?%i\n",containsList(lst,5)); printf("removing 6..."); removeList(lst,6); printf("frontList? 9:%i", frontList(lst)); printf("isEmptyList?%i\n", isEmptyList(lst)); return 0; }
/* De-allocate all links of the list param: lst pointer to the linked list pre: none post: All links (including the two sentinels) are de-allocated */ void freeLinkedList(struct linkedList *lst) { while(!isEmptyList(lst)) { /* remove the link right after the first sentinel */ _removeLink(lst, lst->firstLink->next); } /* remove the first and last sentinels */ free(lst->firstLink); free(lst->lastLink); }
void copyList(Lista lis, Lista *plist) { Lista laux; int h; laux = lis; emptyList(plist); while (!isEmptyList(laux)) { head(laux, &h); tail(&laux); consAtras(plist, h); } }
/** Insere um elemento no final da lista. */ void insertLastList(struct List *list, void *elem) { ListNode *p = (ListNode*) malloc(sizeof (ListNode)); initListNode(p, elem); if (isEmptyList(list)) { list->first = p; list->last = p; } else { list->last->next = p; list->last = p; } }
void DeleteFromFront (LIST *lp) { NODE *p; if (isEmptyList(*lp)==0) return; p=lp->first; lp->first=p->next; printf ("\nThe node with integer %d was deleted.\n",p->info); free (p); return; }
/** * returns the pointer to the unit on the given coordinates * returns NULL if there is no unit there */ static UnitsList findUnit(int x, int y) { UnitsList uniList = globalUnitsList; while(!isEmptyList(uniList)) { if(uniList->x == x && uniList->y == y) return uniList; uniList = uniList->next; } return NULL; }
void _addLinkBefore(struct linkedList *lst, struct DLink *l, TYPE v) { assert ( !isEmptyList(lst) && l != NULL); struct DLink *temp = (struct DLink *) malloc (sizeof(struct DLink)); assert (temp != 0); // ensure it was created temp->value = v; temp->next = l; temp->prev = l->prev; l->prev = temp; (temp->prev)->next = temp; lst->size++; }