Last active
January 10, 2020 05:56
-
-
Save frankie-yanfeng/a5733236fea33ab8f2e3be63f3de7681 to your computer and use it in GitHub Desktop.
Single 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
/* linkedlist.c */ | |
#include <stdlib.h> | |
#include "linkedlist.h" | |
//static link head = NULL; | |
static List ListHead; | |
link make_node(unsigned char item) | |
{ | |
link p = malloc(sizeof *p); | |
p->item = item; | |
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; | |
} | |
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; | |
} | |
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 SINGLE_LIST_LINKEDLIST_H | |
#define SINGLE_LIST_LINKEDLIST_H | |
/* linkedlist.h */ | |
typedef struct node *link; | |
typedef struct list List; | |
struct node { | |
unsigned char item; | |
link 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 //SINGLE_LIST_LINKEDLIST_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 "linkedlist.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