【C++】 ——单链表的模拟实现(纯完整代码)

单链表我也就不多介绍了(网上的太多了),直接上实现代码,我分了三个模块,SList.h,Slist.cpp,main.cpp

SList.h

#pragma once
#include <iostream>
#include <stdlib.h>
#include <assert.h>
using namespace std;
typedef int Elemtype;

typedef struct Slist
{
	Elemtype data;  //数据域
	struct Slist* _next;  //指针域
}Node;

typedef struct Head_Slist
{
	Node* _head;
}node;

void SListInit(node* sl); //单链表的初始化
void SlistDestroy(node* sl);  //单链表的销毁
void SlistPrintf(node* sl);  //单链表的打印
Node* BuySlistNode(Elemtype x);  //给新节点开辟一个空间
void SListPushFront(node* sl, Elemtype val); //头插
void SListPopFront(node* s1);//头删
void SListPushBack(node* sl, Elemtype val);//尾插
void SlistPopBack(node* s1); //尾删
void SListFind(node* sl, Elemtype val); //查找
void SListInsert(node *sl, Node* pos, Elemtype val); //插入
void SListErase(node *sl, Node* pos); //删除

Slist.cpp

#include "SList.h"
void SListInit(node* sl) //单链表的初始化
{
	assert(sl);
	sl->_head = NULL;
}
void SlistDestroy(node* sl)  //单链表的销毁
{
	assert(sl);
	Node* cur = sl->_head;
	while (cur)
	{
		Node* next = cur->_next;
		free(cur);
		cur = next;
	}
}
void SlistPrintf(node* sl)  //单链表的打印
{
	assert(sl);
	Node* cur = sl->_head;
	while (cur)
	{
		cout << cur->data << " ";
		cur = cur->_next;
	}
		cout << endl;
}
void SListPushFront(node* sl, Elemtype val) //头插
{
	Node* newnode = BuySlistNode(val);
	newnode->_next = sl->_head;
	sl->_head = newnode;
}
Node* BuySlistNode(Elemtype x)  //给新节点开辟一个空间
{
	Node* pnode = (Node*)malloc(sizeof(Node));
	pnode->data = x;
	pnode->_next = NULL;
	return pnode; 
}
void SListPopFront(node* s1)//头删
{
	assert(s1);
	Node* next = s1->_head;
	Node* newnext = next->_next;
	free(s1->_head);
	s1->_head = newnext;;
}
void SListPushBack(node* sl, Elemtype val)//尾插
{
	Node* cur = sl->_head;
	if (sl->_head == NULL)
	{
		cur = BuySlistNode(val);
		cur->_next = NULL;
		sl->_head = cur;
	}
	else
	{
		Node* prev = cur;
		while (cur)
		{
			prev = cur;
			cur = cur->_next;
		}
		cur = BuySlistNode(val);
		prev->_next = cur;
	}
	
}
void SlistPopBack(node* s1) //尾删
{
	assert(s1);
	Node* cur = s1->_head;
	if (cur->_next == NULL)
	{
		free(cur);
		s1->_head = NULL;
	}
	else
	{
		while (cur->_next->_next)
		{
			cur = cur->_next;
		}
		free(cur->_next);
		cur->_next = NULL;
	}
}
 void SListFind(node* sl, Elemtype val) //查找
{
	Node* cur = sl->_head;
	while (cur)
	{
		if (cur->data == val)
		{
			cout << "Yes" << endl;
			break;
		}
		else
		{
			cur = cur->_next;
		}
	}
	if (cur == NULL)
	{
		cout << "No" << endl;
	}
}
 void SListInsert(node *sl, Node* pos, Elemtype val) //插入
{
	 assert(sl);
	 Node* newnode = BuySlistNode(val);
	 Node* cur = sl->_head;
	 if (pos==sl->_head)
	 {
		 SListPushFront(sl,val);
		 return;
	 }
	  while (cur ->_next!= pos)
	 {
		  cur = cur->_next;
	  }
	  newnode->_next = cur->_next;
	  cur->_next = newnode;
}
 void SListErase(node *sl, Node* pos) //删除
 {
	 assert(sl);
	 Node* cur = sl->_head;
	 if (pos == sl->_head)
	 {
		 SListPopFront(sl);
		 return;
	 }
	 while (cur->_next != pos)
	 {
		 cur = cur->_next;
	 }
	 cur->_next = pos->_next;
	 free(pos);
	 pos->_next = NULL;
}

main.cpp

#include "SList.h"
int main()
{
	node s;
	SListInit(&s);
	SListPushFront(&s, 1);
	SListPushFront(&s, 2);
	SListPushFront(&s, 3);
	SListPushFront(&s, 4);
	SListPushFront(&s, 5);
	cout << "头插:" << endl;
	SlistPrintf(&s);
	//SlistDestroy(&s); //单链表的销毁
	cout << "头删:" << endl;
	SListPopFront(&s);//头删
	SlistPrintf(&s);
	cout << "尾插:" << endl;
	SListPushBack(&s, 8);//尾插
	SlistPrintf(&s);
	cout<< "空链表尾插:"<<endl;
	SListPushBack(&s, 1);
	SListPushBack(&s, 2);
	SListPushBack(&s, 3);
	SListPushBack(&s, 4);
	SListPushBack(&s, 5);
	SlistPrintf(&s);
	cout << "尾删:"<<endl;
	SlistPopBack(&s);
	SlistPrintf(&s);
	cout << "查找:"<<endl;
	SListFind(&s, 8);
	cout << "位置插入:" << endl;
	SListInsert(&s, NULL, 8);
	SlistPrintf(&s);
	cout << "某一位置的删除:" << endl;
	SListErase(&s, NULL);
	SlistPrintf(&s);
	system("pause");
	return 0;
}


发布了33 篇原创文章 · 获赞 13 · 访问量 1039

猜你喜欢

转载自blog.csdn.net/Vicky_Cr/article/details/105079705