【数据结构】线性表|单链表|(全)测试代码|用C语言/C++实现单链表的定义、插入、删除、查找、打印输出等基本操作

代码:

#include<stdio.h>
#include<stdlib.h>
typedef struct LNode {
    
    
	int data;
	struct LNode *next;
}LNode,*LinkList;

//基础操作:单链表的按位查找
LNode *GetElem(LinkList L, int i) {
    
    
	int j = 1;
	LNode *p = L->next;
	if (i == 0)  //是第一个结点
		return L;
	if (i < 1)   //非法输入
		return NULL;
	while (p != NULL && j < i) {
    
    
		p = p->next;       
		j++;
	}
	return p;
}
//初始化一个空的单链表(带头结点)
bool InitList(LinkList &L) {
    
    
	L = (LNode *)malloc(sizeof(LNode));//分配一个头结点
	if (L == NULL) {
    
       //分配失败返回false
		return false;
	}
	L->next = NULL; //头结点之后暂时还没有结点
	return true;
}

//判断单链表是否为空(带头节点)
bool Empty(LinkList L) {
    
    
	if (L->next == NULL) {
    
    
		return true;
	}
	else {
    
    
		return false;
	}
}
//基础操作:后插操作
bool InsertNextNode(LNode *p, int e) {
    
    
	if (p == NULL) {
    
    
		return false;
	}
	/*********************2.在第i-1个结点后面插入e**********************/
	LNode *s = (LNode *)malloc(sizeof(LNode));
	if (s == NULL) {
    
      //分配内存失败
		return false;
	}
	s->data = e;
	s->next = p->next;
	p->next = s;
	return true;
}

//基础操作:按位序插入:在第i个位置插入元素e
bool ListInsert(LinkList &L, int i, int e) {
    
    
	if (i < 1) {
    
    
		return false;
	}
	LNode *p;
	p = L;//p指向头结点,头结点是第0个结点(不存数据)
	//但单链表中实际存放的是头结点后面的结点,位序从1开始
	/*********************1.找到第i-1个结点*******************************/
	int j = 0;//表示当前p指向的是第几个结点
	while (p!=NULL && j < i-1) {
    
        //j<i-1是因为我们只需要循环到第i-1个结点在后面插入e
		p = p->next;
		j++;
	}
	return InsertNextNode(p, e);
}

//基础操作:前插操作
bool InsertPriorNode(LNode *p,int e) {
    
    
	if (p == NULL) {
    
    
		return false;
	}
	LNode *s = (LNode *)malloc(sizeof(LNode));
	if (s == NULL) {
    
    
		return false;
	}
	s->next = p->next;
	p->next = s;
	s->data = p->data;
	p->data = e;
	return true;

}
基础操作:前插操作【王道书版本】
在p结点之前插入结点s
//bool InsertPriorNode(LNode *p, LNode *s) {
    
    
//	if (p == NULL||s==NULL) {
    
    
//		return false;
//	}
//	s->next = p->next;
//	p->next = s;
//	int temp = p->data;
//	p->data = s->data;
//	s->data = temp;
//	return true;
//
//}

//基础操作:打印单链表数据
void DisplayList(LinkList L) {
    
    
	LNode *p;
	p = L->next;
	int i = 0;
	while (p!= NULL) {
    
    
		printf("%d  ", p->data);
		p = p->next;
		i++;
	}
	printf("一共有%d个元素\n",i);
}

//基础操作:按位序删除
bool NodeDelete(LinkList &L, int i,int &e) {
    
    
	if (i < 1) {
    
    
		return false;
	}
	LNode *p;
	LNode *q;
	int j = 0;
	p = L;

	while (p != NULL && j < i - 1) {
    
    
		p = p->next;
		j++;
	}

	if (p == NULL) {
    
    
		return false;
	}
	if (p->next == NULL) {
    
    //第i-1个结点后面没有其他结点了
		return false;
	}

	q = p->next;
	e = q->data;
	p->next = q->next;
	
	free(q);
	return true;
}
//基础操作:删除指定结点p
bool DeleteNode(LNode *p) {
    
    
	if (p == NULL) {
    
    
		return false;
	}
	LNode *q = p->next;
	if (q == NULL) {
    
    
		return false;
	}
	p->data = q->data;
	p->next = q->next;
	free(q);
	return true;
}
void main() {
    
    
	LinkList L; //声明一个指向单链表的指针
	//初始化一个空表
	InitList(L);
	DisplayList(L);
	ListInsert(L, 1, 1);
	ListInsert(L, 2, 2);
	ListInsert(L, 3, 3);
	ListInsert(L, 4, 4);
	DisplayList(L);
	ListInsert(L, 3, 7);
	DisplayList(L);
	LNode *p;
	p = L;
	int i = 0;
	while (p != NULL && i < 5) {
    
    
		p = p->next;
		i++;
	}
	InsertPriorNode(p, 9);
	DisplayList(L);
	int e = -1;
	NodeDelete(L, 2 ,e);
	DisplayList(L);
	printf("删除的元素值为%d\n", e);
	p = p->next;
	if (!DeleteNode(p)) {
    
     
		printf("删除失败!\n");
	}
	DisplayList(L);
}


结果:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/dxy1128/article/details/107945651