顺序表的链式实现-单链表

一、线性链表
  逻辑上相邻的两个元素在物理存储单元上不一定是不相邻的,这就是链表。
二、单链表具体实现
节点结构体:
1.一般采用形式

typedef struct LNode{
	ElemType data;
	LNode    *next;
}LNode,*LinkList;

2.实际运用形式

//带头结点的线性链表类型
typedef struct LNode{
	ElemType data;
	struct LNode *next;
}LNode,*LinkList;
typedef struct{
	LinkList head,tail;//指向表中头结点和最后一个节点
	int len;
};

3.具体实现
头文件:

#include
#include
#include
typedef int ElemType;

typedef struct LNode{
	ElemType data;
	LNode    *next;
}LNode,*LinkList;

bool initList(LinkList &list);

bool destoryList(LinkList &list);

int listLength(LinkList list);

bool listEmpty(LinkList list);

bool getElem(LinkList list,int i,ElemType &e);

bool localElem(LinkList list,ElemType e);

bool priorElem(LinkList list,ElemType cur_e,ElemType &pre_e);

bool nextElem(LinkList list,ElemType cur_e,ElemType &next_e);

bool listInsert(LinkList &list,int i, ElemType e);

bool listInsert_heart(LinkList &list,int i, ElemType e);

bool listDelete(LinkList &list,int i,ElemType &e);

bool listTraverse(LinkList list);

cpp文件:


#include"singleLinkList.h"

//初始化一个带有头结点的链表list
bool initList(LinkList &list){
	list = (LinkList)malloc(sizeof(LNode));
	if(list == NULL)
		return false;

	list->data = 0; // 头结点用于保存节点数
	list->next = NULL;
	return true;
}
//逐个删除
bool destoryList(LinkList &list){

	if(list == NULL) return false;
	LinkList p = list;
	LinkList q ;
	while(p->next){
		q = p->next;
		p->next = q->next;
		free(q);
	}
	list->data = 0;
	return true;
}

int listLength(LinkList list){
	if(list != NULL) return list->data;
	return 0;
}

bool listEmpty(LinkList list){
	if(list != NULL){
		if(list->data == 0) true;
		else false;
	}
	return true;
}

//头结点算是第0个元素
bool getElem(LinkList list,int i,ElemType &e){
	if(list == NULL) return false;
	LinkList p = list->next;
	int j = 1 ;
	while(p && j < i){
		p = p->next;
		j++;
	}
	if(!p || (j > i)) false ;

	e = p->data;
	return true;
}

bool localElem(LinkList list,ElemType e){
	if(list == NULL) return false;
	LinkList p = list->next;
	while(p){
		if(p->data == e) return true;
		p = p->next;
	}
	return false;
}

bool priorElem(LinkList list,ElemType cur_e,ElemType &pre_e){
	if(list == NULL) return false;
	LinkList p = list->next;
	LinkList q = p;
	while(p){
		if(p->data == cur_e) break;
		q = p;
		p = p->next;
	}
	if(!p) return false;
	if(q == list) return false;

	pre_e = q->data;
	return true;
}

bool nextElem(LinkList list,ElemType cur_e,ElemType &next_e){

	if(list == NULL) return false;
	LinkList p = list->next;
	while(p){
		if(p->data == cur_e) break;
		p = p->next;
	}
	if(!p) return false;
	if(p->next == NULL) return false;
	next_e = p->next->data;
	return true;
}

//头插法插入节点
bool listInsert_heart(LinkList &list,int i, ElemType e){

	if(list == NULL) return false;

	LinkList node = (LinkList)malloc(sizeof(LNode));
	if(node == NULL) return false;
	
	node->data = e;
	node->next = list->next;

	list->next = node;
	list->data++;
	return true;
}

//第i个节点处插入节点
bool listInsert(LinkList &list,int i, ElemType e){

	LinkList p  = list;
	int j = 0;
	while(p && j < i - 1){ p = p->next;j++;}
	if(!p || (j > i - 1)) return false;

	LinkList node = (LinkList)malloc(sizeof(LNode));
	if(node == NULL) return false;

	node->data = e;
	node->next = NULL;

	node->next = p->next;
	p->next = node;
	
	list->data++;
	return true;
}

bool listDelete(LinkList &list,int i,ElemType &e){
	if(list == NULL) return false;
	LinkList p = list->next;
	LinkList q = p;
	int j = 1;
	while(p && j < i){
		q = p;
		p = p->next;
		j++;
	}
	if(!p || (j > i)) return false;

	q->next = p->next;
	e = p->data;
	free(p);
	return true;
}

bool listTraverse(LinkList list){
	if(list == NULL) return false;
	LinkList p = list->next;
	while(p){
		printf("%d ",p->data);
		p = p->next;
	}
	printf("\n");
	return true;
}
int main(){

	//初始化
	LinkList list;
	initList(list);

	//插入节点
	for(int i = 1 ;i <= 10 ;i++){
		listInsert(list,i,i);
	}

	listTraverse(list);

	//length
	printf("length = %d\n",listLength(list));

	//isEmpty
	if(listEmpty(list))
		printf("The table is not empty!\n");
	else
		printf("The talbe is  empty\n");
	//第3个元素是
	ElemType num;
	if(getElem(list,3,num)) printf("The %dth element is: %d\n",3,num);
	//e的前一个节点
	ElemType pre_e;
	if(priorElem(list,4,pre_e)){
		printf("The previous element of 4 is: %d\n",pre_e);
	}else{
		printf("The 4 has no previous\n");
	}

	//e的后一个节点
	ElemType next_e;
	if(nextElem(list,4,next_e)){
		printf("The 4 element of e is: %d\n",next_e);
	}else{
		printf("The 4 has no nextElem\n");
	}

	//delete 5
	ElemType del_e;
	listDelete(list,5,del_e);
	printf("Eelete the %dth element %d\n",5,del_e);
	listTraverse(list);

	//destroy list
	destoryList(list);
	printf("destory list:\n");
	//isEmpty
	if(listEmpty(list))
		printf("The table is not empty!\n");
	else
		printf("The talbe is  empty\n");

	getchar();
	return 0;
}

你可能感兴趣的:(数据结构和算法)