接口参考严蔚敏老师的数据结构
编译环境:linux
typedef struct LNode {
Item data;
struct LNode *prior;
struct LNode *next;
}Link;
typedef struct {
Link *head, *tail;
int len;
}LinkList;
#ifndef LIST_H_
#define LIST_H_
#include
#define OVERFLOW -1
#define ERROR 0
#define OK 1
typedef int Item;
typedef struct LNode {
Item data;
struct LNode *prior;
struct LNode *next;
}Link;
typedef struct {
Link *head, *tail;
int len;
}LinkList;
/*Initialize the linked list using head-insert-method*/
void InitialList(LinkList *L);
/*Initialize the linked list using tail-insert-method*/
void CreateList(LinkList *L);
/*determine if the list is empty*/
bool IsEmpty(LinkList L);
/*return the length of the list*/
unsigned int ListCount(LinkList L);
/*clear the list, make the list empty*/
void ClearList(LinkList *L);
/*Destroy the list*/
void DestroyList(LinkList *L);
/*get the value of the ith item and return it with e*/
bool GetItem(LinkList *L, int i, Item *e);
/*find the first item with its value is e and return its order*/
int LocateList(LinkList *L, Item e);
/*insert e into the head of the list*/
bool InsertHeadList(LinkList *L, Item e);
/*insert e into the tail of the list*/
bool InsertTailList(LinkList *L, Item e);
/*delete the head elem and return it with e*/
bool DeleteHeadList(LinkList *L, Item *e);
/*delete the tail elem and return it with e*/
bool DeleteTailList(LinkList *L, Item *e);
/*return the precurosr of the current elem*/
bool PriorElem(LinkList L, Item cur_e, Item *Pri_e);
/*return the successor of the current elem*/
bool NextElem(LinkList L, Item cur_e, Item *Nex_e);
/*print the list in positive order*/
void PrintList(LinkList L);
/*print the list in negative order*/
void PrintReverseList(LinkList L);
#endif
#include "list.h"
#include
#include
#include
void InitialList(LinkList *L) {
L->head = (Link*)malloc(sizeof(Link));
L->tail = (Link*)malloc(sizeof(Link));
if (L->head == NULL || L->tail == NULL)
exit(OVERFLOW);
L->head->prior = NULL;
L->tail->next = NULL;
L->head->next = L->tail;
L->tail->prior = L->head;
L->len = 0;
}
void CreateList(LinkList *L) {
Link *p;
p = (Link*)malloc(sizeof(Link));
memset(p, 0, sizeof(Link));
p->prior = NULL;
p->next = NULL;
scanf("%d",&(p->data));
Link *q = L->head;
while (p->data != -1) {
L->len++;
q->next = p;
p->prior = q;
p->next = L->tail;
L->tail->prior = p;
q = p;
p = (Link*)malloc(sizeof(Link));
memset(p, 0, sizeof(Link));
p->prior = NULL;
p->next = NULL;
scanf("%d",&(p->data));
}
}
bool IsEmpty(LinkList L) {
return L.len == 0;
}
/*return the length of the list*/
unsigned int ListCount(LinkList L) {
return L.len;
}
/*clear the list, make the list empty*/
void ClearList(LinkList *L) {
Link *p;
p = L->head->next;
while (p != L->tail) {
free(p);
p=p->next;
}
L->head->next = NULL;
L->tail->prior = NULL;
L->len = 0;
}
/*Destroy the list*/
void DestroyList(LinkList *L) {
ClearList(L);
free(L->head);
free(L->tail);
}
/*get the value of the ith item and return it with e*/
bool GetItem(LinkList *L, int i, Item *e) {
if (i > L->len) {
printf("not exist");
return ERROR;
}
int compare = L->len / 2;
int j;
Link *p;
if (i <= compare) {
printf("from the head of the list\n");
for (j = i, p = L->head; j > 0; j--) {
p = p->next;
}
*e = p->data;
}
if (i > compare) {
printf("from the tail of the list\n");
for (j=i, p = L->tail; j > 0; j--) {
p = p->prior;
}
*e = p->data;
}
return OK;
}
/*find the first item with its value is e and return its order*/
int LocateList(LinkList *L, Item e) {
Link *p = L->head->next;
int i;
for (i = 0; i < L->len; i++) {
if (p->data == e) {
return i+1;
}
p = p->next;
}
printf("not exist!\n");
return ERROR;
}
/*insert e into the head of the list*/
bool InsertHeadList(LinkList *L, Item e) {
Link *p = (Link*)malloc(sizeof(Link));
memset(p, 0, sizeof(Link));
p->prior = NULL;
p->next = NULL;
p->data = e;
p->prior = L->head;
p->next = L->head->next;
L->head->next->prior = p;
L->head->next = p;
L->len++;
return OK;
}
/*insert e into the tail of the list*/
bool InsertTailList(LinkList *L, Item e) {
Link *p = (Link*)malloc(sizeof(Link));
memset(p, 0, sizeof(Link));
p->prior = NULL;
p->next = NULL;
p->data = e;
p->next = L->tail;
p->prior = L->tail->prior;
L->tail->prior->next = p;
L->tail->prior = p;
L->len++;
return OK;
}
/*delete the head elem and return it with e*/
bool DeleteHeadList(LinkList *L, Item *e) {
*e = L->head->next->data;
Link *p = L->head->next;
L->head->next = p->next;
p->next->prior = L->head;
free(p);
L->len--;
printf("the first elem deleted is %d\n", *e);
return OK;
}
/*delete the tail elem and return it with e*/
bool DeleteTailList(LinkList *L, Item *e) {
*e = L->tail->prior->data;
Link *p = L->tail->prior;
L->tail->prior = p->prior;
p->prior->next = L->tail;
free(p);
L->len--;
printf("the first elem deleted is %d\n", *e);
return OK;
}
/*print the list in positive order*/
void PrintList(LinkList L) {
Link *p = L.head->next;
while (p != L.tail) {
printf("%d\n", p->data);
p = p->next;
}
}
/*print the list in negative order*/
void PrintReverseList(LinkList L) {
Link *p = L.tail->prior;
while (p != L.head) {
printf("%d\n",p->data);
p = p->prior;
}
}
#include
#include "list.h"
int main () {
Item e;
LinkList L;
InitialList(&L);
CreateList(&L);
if (!IsEmpty(L))
printf("the number of the List is %d\n", ListCount(L));
InsertHeadList(&L, 5);
InsertTailList(&L, 6);
PrintList(L);
GetItem(&L, 2, &e);
printf("the second position elem is %d\n", e);
GetItem(&L, 5, &e);
printf("the fifth position elem is %d\n", e);
int pos = LocateList(&L, 5);
printf("the position of 5 is %d\n", pos);
DeleteHeadList(&L, &e);
printf("the delete head elem of e is %d\n", e);
DeleteTailList(&L, &e);
printf("the delete tail elem of e is %d\n", e);
PrintReverseList(L);
DestroyList(&L);
return 0;
}
object = main.c list.c list.h
test : $(object)
gcc -g -Wall -o test $(object)
main.o:list.h
list.o:list.h
.PHONY : clean
clean:
rm *.o -f