c语言实现带头结点和尾节点的双向链表

c语言实现带头结点和尾节点的双向链表

  • 接口参考严蔚敏老师的数据结构

  • 编译环境:linux

数据结构 :
typedef struct LNode {
	Item data;
	struct LNode *prior;
	struct LNode *next;
}Link;

typedef struct {
	Link *head, *tail;
	int len;
}LinkList;

list.h

#ifndef LIST_H_
#define LIST_H_
#include <stdbool.h>

#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

list.c

#include "list.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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;
	}
}

main.c

#include <stdio.h>
#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;
}

makefile

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


  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值