关于对无头的单向不循环链表的头删,头插,尾删,尾插,打印,查找和插入的方法;
原代码如下
#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;
}
}
}
}