无头单向不循环链表(C++)

关于对无头的单向不循环链表的头删,头插,尾删,尾插,打印,查找和插入的方法;

原代码如下

#pragma once
#include<iostream>
using namespace std;
class SlistNode
{
public:	int data;
	SlistNode* next;
};

void SListPushFront(SlistNode*& phead, int x);//头插
void SListprintf(SlistNode*&phead);//打印链表所有内容
void SListPushBack(SlistNode*&phead,int x);//尾插
void SListPopBack(SlistNode*& phead);//尾删
void SListPopFront(SlistNode*& phead);//头删
SlistNode* SListFind(SlistNode*& phead,int x);//查找
void SListInsert(SlistNode*& phead, SlistNode*pos,int x);//在x前插入一个节点
#include<iostream>
#include"SList.h"
using namespace std;

 

void SListprintf(SlistNode*&phead)//打印所有链表
{
	
	SlistNode* cur = phead;
	while (cur != NULL)
	{
		cout << "->" << cur->data ;
		cur = cur->next;
	}
		
}
void SListPushBack(SlistNode* &pead, int x)//链表尾插
{

	SlistNode* newnode = new SlistNode();
	newnode->data = x;
	newnode->next = NULL;
	if (pead == NULL)
	{
		pead = newnode;
	}
	else
	{
		SlistNode* tail = pead;
		while (tail->next != NULL)
		{
			tail = tail->next;

		}
			tail->next = newnode;
	}
	
}

void SListPushFront(SlistNode*& pead, int x)//链表头插
{
	SlistNode* newcode = new SlistNode();
	newcode->data = x;
	if (pead == NULL)
	{
		newcode->next = NULL;
		pead = newcode;
	}
	else
	{
		newcode->next = pead;
		pead = newcode;
	}

}
void SListPopBack(SlistNode*& phead)//尾删
{
	SlistNode* cur = phead;
	if (cur == NULL)
	{
		return;
	}
	else if (cur->next == NULL)
	{
		phead = NULL;
	}
	else
	{
		while (cur->next->next != 0)
		{
			cur = cur->next;
		}
		cur->next = NULL;
	}
	
}
void SListPopFront(SlistNode*& phead)//头删
{
	if (phead == NULL)
	{
		return;
	}
	else if (phead->next == NULL)
	{
		phead = NULL;
	}
	else
	phead = phead->next;
}
SlistNode* SListFind(SlistNode*& phead, int x)
{
	if (phead == NULL)
	{
		return NULL;
	}
	else{
		SlistNode* cur = phead;
		while (cur->next!=NULL)
		{
			if (cur->data == x)
			{
				return cur;
			}
			cur = cur->next;
		}

		if (cur->data == x)
		{
			return cur;
		}
	
	}
		return NULL;
}
void SListInsert(SlistNode*& phead, SlistNode*pos,int x)
{
	SlistNode* cur = phead;
	SlistNode* newcode = new SlistNode();
	newcode->data = pos->data;
	newcode->next = NULL;
	if (cur->data == x)
	{
		newcode->next = phead;
		phead = newcode;
	}
	else {

	while (cur->next!=NULL)
	{

	if (cur->next->data == x)
	{
		newcode->next = cur->next;
		cur->next = newcode;
		return;
	}
	else {
		cur = cur->next;
	}
	}
	}

}

各方法如下:

void SListprintf(SlistNode*&phead)//打印所有链表
 

void SListprintf(SlistNode*&phead)//打印所有链表
{
	
	SlistNode* cur = phead;
	while (cur != NULL)
	{
		cout << "->" << cur->data ;
		cur = cur->next;
	}
		
}

void SListPushBack(SlistNode* &pead, int x)

void SListPushBack(SlistNode* &pead, int x)//链表尾插
{

	SlistNode* newnode = new SlistNode();
	newnode->data = x;
	newnode->next = NULL;
	if (pead == NULL)
	{
		pead = newnode;
	}
	else
	{
		SlistNode* tail = pead;
		while (tail->next != NULL)
		{
			tail = tail->next;

		}
			tail->next = newnode;
	}
	
}

void SListPushFront(SlistNode*& pead, int x)//链表头插

void SListPushFront(SlistNode*& pead, int x)//链表头插
{
	SlistNode* newcode = new SlistNode();
	newcode->data = x;
	if (pead == NULL)
	{
		newcode->next = NULL;
		pead = newcode;
	}
	else
	{
		newcode->next = pead;
		pead = newcode;
	}

}

void SListPopBack(SlistNode*& phead)//尾删
 

void SListPopBack(SlistNode*& phead)//尾删
{
	SlistNode* cur = phead;
	if (cur == NULL)
	{
		return;
	}
	else if (cur->next == NULL)
	{
		phead = NULL;
	}
	else
	{
		while (cur->next->next != 0)
		{
			cur = cur->next;
		}
		cur->next = NULL;
	}
	
}

void SListPopFront(SlistNode*& phead)//头删
 

void SListPopFront(SlistNode*& phead)//头删
{
	if (phead == NULL)
	{
		return;
	}
	else if (phead->next == NULL)
	{
		phead = NULL;
	}
	else
	phead = phead->next;
}

SlistNode* SListFind(SlistNode*& phead, int x)

SlistNode* SListFind(SlistNode*& phead, int x)
{
	if (phead == NULL)
	{
		return NULL;
	}
	else{
		SlistNode* cur = phead;
		while (cur->next!=NULL)
		{
			if (cur->data == x)
			{
				return cur;
			}
			cur = cur->next;
		}

		if (cur->data == x)
		{
			return cur;
		}
	
	}
		return NULL;
}

void SListInsert(SlistNode*& phead, SlistNode*pos,int x)

void SListInsert(SlistNode*& phead, SlistNode*pos,int x)
{
	SlistNode* cur = phead;
	SlistNode* newcode = new SlistNode();
	newcode->data = pos->data;
	newcode->next = NULL;
	if (cur->data == x)
	{
		newcode->next = phead;
		phead = newcode;
	}
	else {

	while (cur->next!=NULL)
	{

	if (cur->next->data == x)
	{
		newcode->next = cur->next;
		cur->next = newcode;
		return;
	}
	else {
		cur = cur->next;
	}
	}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值