Last active
January 10, 2020 06:32
-
-
Save frankie-yanfeng/37d98cbc1829c427addef2d349e99e81 to your computer and use it in GitHub Desktop.
Double List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "doubleList.h" | |
/* linkedlist.c */ | |
#include <stdlib.h> | |
#include "doubleList.h" | |
//static link head = NULL; | |
static List ListHead; | |
link make_node(unsigned char item) | |
{ | |
link p = malloc(sizeof *p); | |
if (!p) | |
exit(1); | |
p->item = item; | |
p->prev = p->next = NULL; | |
return p; | |
} | |
void free_node(link p) | |
{ | |
free(p); | |
} | |
link search(unsigned char key) | |
{ | |
link p; | |
//for (p = head; p; p = p->next) | |
for (p = ListHead.head; p; p = p->next) | |
if (p->item == key) | |
return p; | |
return NULL; | |
} | |
void insert(link p) | |
{ | |
//p->next = head; | |
//p->next = ListHead.head; | |
//ListHead.head = p; | |
p->next = ListHead.head; | |
if (ListHead.head) | |
ListHead.head->prev = p; | |
ListHead.head = p; | |
p->prev = NULL; | |
} | |
link delete(link p) | |
{ | |
// link prev; | |
// if (p == ListHead.head) { | |
// ListHead.head = p->next; | |
// return p; | |
// } | |
// for (prev = ListHead.head; prev; prev = prev->next) | |
// if (prev->next == p) { | |
// prev->next = p->next; | |
// return p; | |
// } | |
// return NULL; | |
if (p->prev) | |
p->prev->next = p->next; | |
else | |
ListHead.head = p->next; | |
if(p->next) | |
p->next->prev = p->prev; | |
return p; | |
} | |
link deleteUniformed(link p) | |
{ | |
link prev; | |
if (p == ListHead.head) { | |
ListHead.head = p->next; | |
return p; | |
} | |
for (prev = ListHead.head; prev; prev = prev->next) | |
if (prev->next == p) { | |
prev->next = p->next; | |
return p; | |
} | |
return NULL; | |
} | |
void traverse(void (*visit)(link)) | |
{ | |
link p; | |
for (p = ListHead.head; p; p = p->next) | |
visit(p); | |
} | |
void destroy(void) | |
{ | |
link q, p = ListHead.head; | |
ListHead.head = NULL; | |
while (p) { | |
q = p; | |
p = p->next; | |
free(q); | |
} | |
} | |
void push(link p) | |
{ | |
insert(p); | |
} | |
link pop(void) | |
{ | |
if (ListHead.head == NULL) | |
return NULL; | |
else | |
return delete(ListHead.head); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DOUBLE_LIST_DOUBLELIST_H | |
#define DOUBLE_LIST_DOUBLELIST_H | |
typedef struct node *link; | |
typedef struct list List; | |
struct node { | |
unsigned char item; | |
link prev, next; | |
}; | |
struct list { | |
link head; | |
link tail; | |
}; | |
link make_node(unsigned char item); | |
void free_node(link p); | |
link search(unsigned char key); | |
void insert(link p); | |
link delete(link p); | |
link deleteUniformed(link p); | |
void traverse(void (*visit)(link)); | |
void destroy(void); | |
void push(link p); | |
link pop(void); | |
#endif //DOUBLE_LIST_DOUBLELIST_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* main.c */ | |
#include <stdio.h> | |
#include "doubleList.h" | |
void print_item(link p) | |
{ | |
printf("%d\n", p->item); | |
} | |
int main(void) | |
{ | |
link p = make_node(10); | |
insert(p); | |
p = make_node(5); | |
insert(p); | |
p = make_node(90); | |
insert(p); | |
p = search(5); | |
delete(p); | |
free_node(p); | |
p = search(90); | |
deleteUniformed(p); | |
free_node(p); | |
traverse(print_item); | |
destroy(); | |
p = make_node(100); | |
push(p); | |
p = make_node(200); | |
push(p); | |
p = make_node(250); | |
push(p); | |
while ((p = pop())) { | |
print_item(p); | |
free_node(p); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment