线性表链式实现

81 阅读2分钟

C++编译器

#include <stdio.h>
#include <stdlib.h>
#define NAN -999999

typedef struct Node {
	int data;
	Node *next;
} Node;

typedef struct LinkList {
	int lenth;
	Node *head;
	Node *trail;

	/*
	初始化链表
	*/
	void initLinkList() {
		this->lenth = 0;
		Node *p = (Node *)malloc(sizeof(Node));
		p->next = NULL;
		this->head = p;
		this->trail = p;
	}

	/*
	在指定节点后添加元素
	arg1:Node *p 指定节点
	arg2:int value 元素值
	return true:操作成功 false:操作失败
	*/
	bool add(Node *p, int value) {
		Node *s = (Node *)malloc(sizeof(Node));
		s->data = value;
		s->next = p->next;
		p->next = s;
		return true;
	}

	/*
	在链表尾部添加元素
	arg1:int value 元素值
	*/
	void append(int value) {
		add(this->trail, value);
		this->trail = this->trail->next;
		this->lenth++;
	}
	
	/*
	在链表头部插入元素
	arg1:int value 元素值
	*/
	void headInsert(int value){
		Node *p = (Node*) malloc(sizeof(Node));
		if(this->lenth==0){
			this->trail = p;
		}
		p->data = value;
		p->next = this->head->next;
		
		this->head->next=p;
		
		this->lenth++;
	}
	
	/*
	获取指定位置的节点
	arg1:int index 节点下标
	return Node* 指定位置节点
	*/
	Node *indexOf(int index) {
		if (index < 0 || index >= this->lenth) {
			printf("fail:index out of this LinkList\n");
		}
		Node *p = this->head;
		int i = -1;
		while (i < index) {
			i++;
			p = p->next;
		}

		return p;
	}
	/*
	指定下标位置添加元素
	arg1:int index 下标位置
	arg2:int value 元素值
	return:true:操作成功 false:操作失败
	*/
	bool insert(int index, int value) {
		if (index < 0 || index > this->lenth) {
			printf("fail:index out of this LinkList\n");
			return false;
		}
		Node *p = indexOf(index - 1);
		if (add(p, value)) {
			//判断插入节点是否在尾节点后一位
			if(index==this->lenth){
				this->trail = this->trail->next;
			}
			
			this->lenth++;
		}
		return true;
	}
	
	/*
	删除指定下标节点
	arg1:int index 目标节点下标
	*/
	bool remove(int index){
		if (index < 0 || index > this->lenth) {
			printf("fail:index out of this LinkList\n");
			return false;
		}
		//获取第index节点的前驱节点
		Node *p = indexOf(index-1);
		
		Node *s = p->next;
		
		p->next = s->next;
		this->lenth--;
		
		free(s);
		return true;	
	}
	/*
	修改指定节点元素值
	arg1:int index 目标节点下标 int 修改后的值
	*/
	bool update(int index,int value){
		if (index < 0 || index > this->lenth) {
			printf("fail:index out of this LinkList\n");
			return false;
		}
		//获取第index节点
		Node *p = indexOf(index);
		
		p->data = value;
		
		return true;
		
	}
	
	/*
	获取指定下标节点的元素值
	arg1:int index 目标节点下标
	return: 目标节点元素值
	*/
	int get(int index){
		if (index < 0 || index > this->lenth) {
			printf("fail:index out of this LinkList\n");
			return NAN;
		}
		//获取第index节点
		Node *p = indexOf(index);
		
		return p->data;
	}
	/*
	判断链表是否为空
	*/
	bool isEmpty(){
		return this->lenth==0||this->head->next==NULL;
	}

	/*
	打印链表
	*/
	void print() {
		Node *p = this->head->next;
		while (p != NULL) {
			printf("%d ", p->data);
			p = p->next;
		}
		printf("lenth=%d\n", this->lenth);
	}
} LinkList;

int main() {

	LinkList linkList;

	linkList.initLinkList();
	
	printf("LinkList isEmpty:%s\n",linkList.isEmpty()?"true":"false");

	for (int i = 0; i < 10; i++) {
		linkList.append(i);
	}

	linkList.print();

	int index = 3;

	printf("index=%d value=%d",index,linkList.indexOf(index)->data);

	linkList.insert(1, 10);

	linkList.remove(3);

	printf("index=%d value=%d\n",index,linkList.get(index));

	linkList.insert(linkList.lenth,10);
	
	linkList.append(11);
	
	printf("LinkList isEmpty:%s\n",linkList.isEmpty()?"true":"false");

	linkList.print();

	return 0;
}