/*the filename: main *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include "list.h" int main(){ LIST *myList; NODE *x; myList = (LIST *) malloc(sizeof(LIST)); creatList(myList); insertLastList(myList, 72); insertLastList(myList, 39); insertLastList(myList, 5); printList(myList); deleteFirstList(myList); printList(myList); x = getFirstList(myList); dumpNode(x); printf("******************\n"); deleteFirstList(myList); printList(myList); x = getFirstList(myList); dumpNode(x); printf("******************\n"); deleteFirstList(myList); printList(myList); x = getFirstList(myList); dumpNode(x); printf("******************\n"); }
/*the filename: list *the filename extension: .h */ typedef struct node { int data; struct node *link; } NODE; typedef struct list { int count; struct node *head; struct node *rear; } LIST; void creatList(LIST *listHead); void insertFirstList(LIST *listHead, int data); void insertLastList(LIST *listHead, int data); void printList(LIST *listHead); void dumpNode(NODE *input); void deleteFirstList(LIST *listHead); NODE *getFirstList(LIST *listHead);
/*the filename: list_implement_bak *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include "list.h" void creatList(LIST *listHead) { listHead->count = 0; listHead->head = NULL; listHead->rear = NULL; } void insertLastList(LIST *listHead, int data){ NODE *pNew; pNew = (NODE *) malloc(sizeof(NODE)); pNew->data = data; pNew->link = NULL; if (listHead->head == NULL){ // case 1: empty list listHead->head = pNew; listHead->rear = pNew; } else { // case 2: begining list listHead->rear->link = pNew; listHead->rear = pNew; } listHead->count++; } void insertFirstList(LIST *listHead, int data){ NODE *pNew; pNew = (NODE *) malloc(sizeof(NODE)); pNew->data = data; pNew->link = NULL; if (listHead->head == NULL){ // case 1: empty list listHead->head = pNew; listHead->rear = pNew; } else { // case 2: begining list pNew->link = listHead->head; listHead->head = pNew; } listHead->count++; } void deleteFirstList(LIST *listHead){ NODE *pLoc; if (listHead->count == 0){ printf("Do nothing \n"); }else if (listHead->count == 1){ pLoc = listHead->head; listHead->head = NULL; listHead->rear = NULL; listHead->count--; free(pLoc); } else { pLoc = listHead->head; listHead->head = pLoc->link; listHead->count--; free(pLoc); } } NODE *getFirstList(LIST *listHead){ NODE *pLoc; if (listHead->count == 0){ printf("Do nothing \n"); }else { pLoc = listHead->head; return pLoc; } } void dumpNode(NODE *input){ if (input != NULL){ printf("the data =%d\n", input->data); } } void printList(LIST *listHead){ NODE *curr; if (listHead->head != NULL){ printf("the total count = %d\n", listHead->count); for (curr = listHead->head; curr != NULL; curr = curr->link){ printf("%d\n", curr->data); } } else{ printf("the list is empty\n"); } }
沒有留言:
張貼留言