Skip to content

Instantly share code, notes, and snippets.

@juliosandino
Created October 17, 2018 07:08
Show Gist options
  • Save juliosandino/7fcef83c3532df3fc98544ab64ba9e58 to your computer and use it in GitHub Desktop.
Save juliosandino/7fcef83c3532df3fc98544ab64ba9e58 to your computer and use it in GitHub Desktop.
pa2 so pichael can see and judge my code. he already took the class so its cool.
#include <stdio.h>
#include <stdlib.h>
#include "List.h"
// structs --------------------------------------------------------------------
// private NodeObj type
typedef struct NodeObj {
int data;
struct NodeObj* next;
struct NodeObj* prev;
} NodeObj;
// private Node type
typedef NodeObj* Node;
// private ListObj type
typedef struct ListObj {
Node front;
Node back;
Node cursor;
int length;
int index;
} ListObj;
// Constructors-Destructors ---------------------------------------------------
// newNode()
// Returns a reference to new Node object. Initializes next and data fiels
// Private.
Node newNode (int data) {
Node N = malloc(sizeof(NodeObj));
N->data = data;
N->next = NULL;
N->prev = NULL;
return(N);
}
// freeNode()
// Frees heap memory pointed to by *pN, sets *pN to NULL
// Private.
void freeNode(Node* pN) {
if (pN != NULL && *pN != NULL) {
free(*pN);
*pN = NULL;
}
}
// newList()
// Returns reference to the new empty List object.
List newList(void) {
List L;
L = malloc(sizeof(ListObj));
L->front = L->back = L->cursor = NULL;
L->length = 0;
L->index = -1;
return(L);
}
void freeList(List* pL) {
if (pL != NULL && *pL !=NULL) {
while (length(*pL) != 0) {
deleteFront(*pL);
}
free(*pL);
*pL = NULL;
}
}
// Access functions -----------------------------------------------------------
// length()
// Returns the length of L
int length(List L) {
if ( L==NULL ) {
printf("List Error: calling length() on NULL List reference\n");
exit(1);
}
return(L->length);
}
// index()
// Returns the cursor position. -1 if undefined
int index(List L) {
if ( L==NULL ) {
printf("List Error: calling index() on NULL List reference\n");
exit(1);
}
if (L->cursor == NULL) {
return -1;
}
return(L->index);
}
// front()
// Returns the data of the front node
int front(List L) {
if ( L == NULL ) {
printf("List Error: calling front() on NULL List reference\n");
exit(1);
}
if (length(L) == 0) {
printf("List Error: calling front() on empty List\n");
exit(1);
}
return(L->front->data);
}
// back()
// Returns the data of the back node
int back(List L) {
if ( L == NULL ) {
printf("List Error: calling back() on NULL List reference\n");
exit(1);
}
if (length(L) == 0) {
printf("List Error: calling back() on empty List\n");
exit(1);
}
return(L->back->data);
}
// get()
// Returns the data of the cursor node
int get(List L) {
if ( L == NULL ) {
printf("List Error: calling get() on NULL List reference\n");
exit(1);
}
if (L->cursor == NULL) {
return -1;
}
return(L->cursor->data);
}
// equals
// Returns true if two lists are the same. false otherwise
int equals(List A, List B) {
if ( A == NULL || B == NULL ) {
printf("List Error: calling equals() on NULL List reference\n");
exit(1);
}
if (length(A) != length(B)) {
return 0;
}
Node a = A->front;
Node b = B->front;
while (a && b) {
if (a->data != b->data) {
return 0;
}
a = a->next;
b = b->next;
}
return 1;
}
// Manipulation procedures ----------------------------------------------------
void clear(List L) {
if (L == NULL) {
printf("List Error: calling clear() on Null List reference\n");
exit(1);
}
if (length(L) == 0) {
return;
}
Node ptr = L->front;
Node temp = NULL;
while(ptr) {
temp = ptr->next;
freeNode(&ptr);
ptr = temp;
}
L->front = L->back = L->cursor = NULL;
L->index = -1;
freeNode(&temp);
freeNode(&ptr);
}
void moveFront(List L) {
if ( L == NULL ) {
printf("List Error: calling moveFront() on NULL List reference\n");
exit(1);
}
if (length(L) == 0) {
printf("List Error: calling moveFront() on empty List\n");
exit(1);
}
L->cursor = L->front;
L->index = 0;
}
void moveBack(List L) {
if ( L == NULL ) {
printf("List Error: calling moveBack() on NULL List reference\n");
exit(1);
}
if (length(L) == 0) {
printf("List Error: calling moveBack() on empty List\n");
exit(1);
}
L->cursor = L->back;
L->index = length(L) - 1;
}
void movePrev(List L) {
if ( L == NULL ) {
printf("List Error: calling movePrev() on NULL List reference\n");
exit(1);
}
if (L->cursor == NULL) {
printf("List Error: calling movePrev() on empty List\n");
exit(1);
}
L->cursor = L->cursor->prev;
L->index -= 1;
}
void moveNext(List L) {
if ( L == NULL ) {
printf("List Error: calling moveNext() on NULL List reference\n");
exit(1);
}
if (L->cursor == NULL) {
printf("List Error: calling moveNext() on empty List\n");
exit(1);
}
L->cursor = L->cursor->next;
L->index += 1;
}
void prepend(List L, int data) {
if ( L == NULL ) {
printf("List Error: calling prepend() on NULL List reference\n");
exit(1);
}
Node N = newNode(data);
if (length(L) == 0) {
L->front = L->back = N;
} else {
N->next = L->front;
L->front->prev = N;
L->front = N;
}
L->index += 1;
L->length += 1;
}
void append(List L, int data) {
if ( L == NULL ) {
printf("List Error: calling append() on NULL List reference\n");
exit(1);
}
Node N = newNode(data);
if (length(L) == 0) {
L->front = L->back = N;
} else {
N->prev = L->back;
L->back->next = N;
L->back = N;
}
L->length += 1;
}
void insertBefore(List L, int data) {
if ( L == NULL ) {
printf("List Error: calling insertBefore() on NULL List reference\n");
exit(1);
}
if (L->cursor == NULL) {
printf("List Error: calling insertBefore() with undefined cursor");
}
Node N = newNode(data);
N->next = L->cursor;
N->prev = L->cursor->prev;
if (L->cursor != L->front) {
L->cursor->prev = N;
}
L->cursor->prev = N;
if (L->cursor == L->front) {
L->front = N;
}
L->length += 1;
L->index++;
}
void insertAfter(List L, int data) {
if ( L == NULL ) {
printf("List Error: calling insertAfter() on NULL List reference\n");
exit(1);
}
if (L->cursor == NULL) {
printf("List Error: calling insertAfter() with undefined cursor");
}
Node N = newNode(data);
N->prev = L->cursor;
N->next = L->cursor->next;
if (L->cursor != L->back) {
L->cursor->next->prev = N;
}
L->cursor->next = N;
if (L->cursor == L->back) {
L->back = N;
}
L->length += 1;
}
void deleteFront(List L) {
if ( L == NULL ) {
printf("List Error: calling deleteFront() on NULL List reference\n");
exit(1);
}
if (length(L) == 0) {
return ;
}
Node temp = L->front;
L->front = L->front->next;
freeNode(&temp);
L->index--;
L->length--;
}
void deleteBack(List L) {
if ( L == NULL ) {
printf("List Error: calling deleteBack() on NULL List reference\n");
exit(1);
}
if (length(L) == 0) {
return ;
}
if (L->length != 1) {
L->back->prev->next = NULL;
}
if (L->back == L->cursor) {
L->cursor = NULL;
}
Node temp = L->back;
L->back = L->back->prev;
L->length--;
freeNode(&temp);
}
void delete(List L) {
if ( L == NULL ) {
printf("List Error: calling delete() on NULL List reference\n");
exit(1);
}
if ( L->cursor == NULL ) {
printf("List Error: calling delete() with undefined cursor\n");
exit(1);
}
if (L->cursor == L->front) {
deleteFront(L);
} else if (L->cursor == L->back) {
deleteBack(L);
} else {
L->cursor->prev->next = L->cursor->next;
L->cursor->next->prev = L->cursor->prev;
freeNode(&L->cursor);
L->cursor = NULL;
L->length -= 1;
}
}
void printList(List L) {
Node N = NULL;
if ( L == NULL ) {
printf("List Error: calling printList() on NULL List reference\n");
exit(1);
}
for(N = L->front; N != NULL; N = N->next) {
printf("%d ", N->data);
}
printf("\n");
}
List copyList(List L) {
List list = newList();
Node current = L->front;
while (current != NULL) {
append(list, current->data);
current = current->next;
}
freeNode(&current);
return list;
}
//-----------------------------------------------------------------------------
// List.h
// Header file for List ADT
//-----------------------------------------------------------------------------
#ifndef _LIST_H_INCLUDE_
#define _LIST_H_INCLUDE_
// Exported type --------------------------------------------------------------
typedef struct ListObj* List;
// Constructors-Destructors ---------------------------------------------------
List newList(void);
void freeList(List* pL);
// Access functions -----------------------------------------------------------
int length(List L);
int index(List L);
int front(List L);
int back(List L);
int get(List L);
int equals(List A, List B);
// Manipulation procedures ----------------------------------------------------
void clear(List L);
void moveFront(List L);
void moveBack(List L);
void movePrev(List L);
void moveNext(List L);
void prepend(List L, int data);
void append(List L, int data);
void insertBefore(List L, int data);
void insertAfter(List L, int data);
void deleteFront(List L);
void deleteBack(List L);
void delete(List L);
// Other operations -----------------------------------------------------------
//void printList(FILE* out, List L);
void printList(List L);
List copyList(List L);
#endif
#include <stdio.h>
#include <stdlib.h>
#include "List.h"
int main(int argc, char* argv[]) {
List A = newList();
for (int i = 5; i >= 0; i--) {
prepend(A, i);
}
deleteFront(A);
deleteBack(A);
moveFront(A);
moveNext(A);
delete(A);
List B = copyList(A);
printList(A);
printList(B);
printf("A == B: %d\n", equals(A, B));
printf("Front: %d\n", front(A));
printf("Back: %d\n", back(A));
printf("Cursor: %d\n", get(A));
printf("length: %d\n", length(A));
freeList(&A);
freeList(&B);
return(0);
}
#------------------------------------------------------------------------------
# Makefile for List ADT
#
# make makes ListTest
# make clean removes object files
#
#------------------------------------------------------------------------------
ListTest : ListTest.o List.o
gcc -o ListTest ListTest.o List.o
ListTest.o : List.h ListTest.c
gcc -c -std=c99 -Wall ListTest.c
List.o : List.h List.c
gcc -c -std=c99 -Wall List.c
clean :
rm -f ListTest ListTest.o List.o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment