#ifndef _SLIST_H
#define _SLIST_H
#ifdef __cplusplus
extern "C" {
#endif
/*******1. 不带头结点的单链表*****/
/*****
*@链表结点结构定义
*@ m_data:数据
*@m_pNext:指向下一结点的指针
***/
struct listNode
{
int m_data;
listNode* m_pNext;
};
/*******
*@ 用数组array初始化链表,数组元素个数为n
*@ 返回链表头
*@auther:spf
*@version 1.0
*@data:2015.7.18
*/
listNode* buildList(int* array, int n);
/***************
*@ 链表末尾加入一个数据
*@pHead:指向链表头的指针
*@value:要加入的数据
*/
void AddDataToTrial(listNode** pHead, int value);
/*****************
*单链表插入操作
*pHead :指向表头的指针
*@ 要插入元素的位置
*@ 要出入的元素
*/
bool insertList(listNode** pHead, int index, int value);
/*****************
*单链表删除操作
*pHead :指向表头的指针
*@ 要要删除元素的索引
*/
bool deleteList(listNode** pHead, int index);
/******************
* 打印单链表
@ pHead 表头指针
*/
bool printList(listNode* pHead);
/*******2. 带头结点的单链表*****/
/******
*链表的初始化
* &pHead 链表头指针的引用
**/
bool initList(listNode* &pHead);
/******
*用数组建立链表
* &pHead 链表头指针的引用
**/
bool BuildList(listNode* &pHead,int *array,int n);
/******
*清空链表
* &pHead 链表头指针的引用
**/
bool clearList(listNode* &pHead);
/******
*计算表的长度
* &pHead 链表头指针的引用
**/
int lengthOfList(listNode* &pHead);
/******
*推断链表是否为空,空返回1。否则返回0
* &pHead 链表头指针的引用
**/
bool isEmptyOfList(listNode* &pHead);
/******
*查找指定元素,查找成功返回元素所在结点指针,否则返回NULL
* &pHead 链表头指针的引用
**/
listNode* searchElement(listNode* &pHead,int x);
/******
*在单链表中对第i(i>=0)个结点定位。函数返回第i个结点的地址,
* 若i小于0 或i 超出结点个数,则返回NULL
* &pHead 链表头指针的引用
**/
listNode* Locate(listNode* &pHead, int i);
/******
*将元素value插入到链表中第i(i>=1)个结点的位置。若i不符合要求。则返回0 ,否则返回1
* 若i小于0 或i 超出结点个数,则返回NULL
* &pHead 链表头指针的引用
**/
bool Insert(listNode* &pHead,int i, int value);
/******
*将删除链表中第i(i>=1)个结点的元素,若i不符合要求,则返回0 。否则返回1,其删除的元素置于value返回
* 若i小于0 或i 超出结点个数,则返回NULL
* &pHead 链表头指针的引用
**/
bool Remove(listNode* &pHead, int i, int &value);
/******
*按顺序打印打印链表
* &pHead 链表头指针的引用
**/
bool Print(listNode* &pHead);
/******
*递归打印链表
* pHead 链表头指针的引用
**/
bool recursivePrint(listNode* first);
/******
*反向打印链表(基于栈实现)
* &pHead 链表头指针的引用
**/
bool PrintListReverse(listNode* first);
/******
*反向打印链表(基于递归)
* &pHead 链表头指针的引用
**/
bool PrintListReverseRecursive(listNode* first);
/******
*反转单链表(用三个辅助指针反转)
* &pHead 链表头指针的引用
**/
bool ReverseSlist(listNode*& pHead);
/******
*反转单链表(递归)
* &pHead 链表头指针的引用
**/
listNode* ReverseSlistRecursive(listNode* pHead);
#ifdef __cplusplus
}
#endif
#endif
#include "stdafx.h"
#include "slist.h"
#include <stack>
#include <stdlib.h>
#include <iostream>
listNode* buildList(int* array, int n){
listNode*t, *pHead= new listNode();
pHead->m_data = array[0];
pHead->m_pNext = NULL;
t = pHead;
for (size_t i = 1; i < n;++i)
{
listNode* pNode = new listNode();
t->m_pNext = pNode;
pNode->m_data = array[i];
pNode->m_pNext=NULL;
t = pNode;
}
return pHead;
}
void AddDataToTrial(listNode** pHead, int value)
{
listNode* pNew = new listNode();
pNew->m_data = value;
pNew->m_pNext = NULL;
if (*pHead == NULL)
*pHead = pNew;
else
{
listNode* pNode = *pHead;
while (pNode->m_pNext!=NULL)
{
pNode = pNode->m_pNext;
}
pNode->m_pNext = pNew;
}
}
bool insertList(listNode** pHead, int index, int value)
{
if (index < 1)
{
std::cerr << "索引无效" << std::endl;
return 0;
}
else if (index == 1)
{
listNode* pNew = new listNode();
if (pNew == NULL)
{
std::cerr << "分配内存失败" << std::endl;
return 0;
}
pNew->m_data = value;
pNew->m_pNext =*pHead;
*pHead = pNew;
}
else
{
listNode *pNode = *pHead;
while (pNode != NULL && 0 < index - 2)
{
pNode = pNode->m_pNext;
--index;
}
if (pNode==NULL&&(*pHead)!=NULL)
{
std::cerr << "链表长度不够,无法插入" << std::endl;
return 0;
}
else
{
listNode* pNew = new listNode();
if (pNew == NULL)
{
std::cerr << "分配内存失败" << std::endl;
return 0;
}
pNew->m_data = value;
pNew->m_pNext = pNode->m_pNext;
pNode->m_pNext = pNew;
}
}
return 1;
}
bool deleteList(listNode** pHead, int index)
{
if (index < 1)
{
std::cerr << "元素索引小于1。无法进行删除操作!" << std::endl;
return 0;
}
if (index == 1)
{
listNode* pNode = *pHead;
*pHead = (*pHead)->m_pNext;
delete pNode;
pNode = NULL;
}
else
{
listNode* pNode = *pHead;
while (pNode != NULL && 0 < index - 2)
{
pNode = pNode->m_pNext;
--index;
}
if (pNode==NULL||pNode->m_pNext==NULL)
{
std::cerr << "空表或链表较短,索引无效"<<std::endl;
return 0;
}
else
{
listNode *q = pNode->m_pNext;
pNode->m_pNext = q->m_pNext;
delete q;
q = NULL;
}
}
return 1;
}
bool printList(listNode* pHead)
{
listNode* pNode = pHead;
int i = 0;
while (pNode != NULL)
{
++i;
std::cout << "第" << i << "个元素为:" << pNode->m_data << std::endl;
pNode = pNode->m_pNext;
}
std::cout << "总计" << i << "个元素打印完成" << std::endl;
return 1;
}
bool ReverseSlist(listNode*& pHead)
{
if(pHead==nullptr&&pHead->m_pNext==nullptr)
return 0;
listNode* pre, *temp, *head;
pre = temp = nullptr;
head = pHead;
while (head!=nullptr)
{
temp = head;
head = head->m_pNext;
temp->m_pNext = pre;
pre = temp;
}
pHead = temp;
if (temp == nullptr)
return 0;
return 1;
}
listNode* ReverseSlistRecursive(listNode* pHead)
{
if (pHead == nullptr || pHead->m_pNext == nullptr)
return pHead;
else
{
listNode* newNode = ReverseSlistRecursive(pHead->m_pNext);
pHead->m_pNext->m_pNext = pHead;
pHead->m_pNext = nullptr;
return newNode;
}
}
bool initList(listNode* &pHead)
{
pHead = new listNode;
if (!pHead)
{
std::cerr << "内存分配失败" << std::endl;
return 0;
}
pHead->m_pNext = NULL;
return 1;
}
bool BuildList(listNode* &pHead, int *array, int n)
{
listNode* p = pHead;
if (p == NULL)
{
std::cerr << "链表头不存在" << std::endl;
return 0;
}
for (int i = 0; i < n;i++)
{
listNode* pNew = new listNode;
pNew->m_data = array[i];
pNew->m_pNext = NULL;
p->m_pNext = pNew;
p = p->m_pNext;
}
return 1;
}
bool clearList(listNode* &pHead)
{
listNode* p;
while (pHead->m_pNext!=NULL)
{
p = pHead->m_pNext;
pHead->m_pNext = p->m_pNext;
delete p;
}
return 1;
}
int lengthOfList(listNode* &pHead)
{
int cout = 0;
listNode* p = pHead->m_pNext;
while (p!=NULL)
{
++cout;
p = p->m_pNext;
}
return cout;
}
bool isEmptyOfList(listNode* &pHead)
{
return (pHead->m_pNext==NULL);
}
listNode* searchElement(listNode* &pHead, int x)
{
listNode* p = pHead->m_pNext;
while (p != NULL&&p->m_data != x)
{
p = p->m_pNext;
}
return p;
}
listNode* Locate(listNode* &pHead, int i)
{
if (i < 0)
return NULL;
listNode* p = pHead;
int cout = 0;
while (p!=NULL&&cout<i)
{
p = p->m_pNext;
cout++;
}
return p;
}
bool Insert(listNode* &pHead,int i, int value)
{
listNode* p = pHead;
int cout = 0;
while (p!=NULL&&cout<i-1)
{
cout++;
p = p->m_pNext;
}
if (p == NULL||i<1)
{
std::cerr << "链表为空或者索引无效" << std::endl;
return 0;
}
listNode* pNew = new listNode();
pNew->m_data = value;
pNew->m_pNext = p->m_pNext;
p->m_pNext = pNew;
return 1;
}
bool Remove(listNode* &pHead, int i, int &value)
{
listNode* p = pHead;
int cout = 0;
while (p!=NULL&&cout<i-1)
{
p = p->m_pNext;
cout++;
}
if (p == NULL || p->m_pNext==NULL||i < 1)
{
std::cerr << "链表为空或者索引无效" << std::endl;
return 0;
}
listNode* d = p->m_pNext;
p->m_pNext = d->m_pNext;
value = d->m_data;
delete d;
d = NULL;
return 1;
}
bool Print(listNode* &pHead)
{
if (pHead == NULL||pHead->m_pNext==NULL)
{
std::cerr << "链表头不存在或链表为空" << std::endl;
return 0;
}
listNode* p = pHead->m_pNext;
int cout = 0;
while (p!=NULL)
{
cout++;
std::cout << "第" << cout << "个元素为" << p->m_data << std::endl;
p = p->m_pNext;
}
return 1;
}
bool recursivePrint(listNode* first)
{
if (first == NULL)
return 0;
std::cout << first->m_data << std::endl;
recursivePrint(first->m_pNext);
}
bool PrintListReverse(listNode* first)
{
listNode* p = first;
std::stack<listNode*> nodes;
while (p != NULL)
{
nodes.push(p);
p = p->m_pNext;
}
while (!nodes.empty())
{
p = nodes.top();
std::cout << p->m_data << std::endl;
nodes.pop();
}
return 1;
}
bool PrintListReverseRecursive(listNode* first)
{
listNode* p = first;
if (p != NULL)
{
if (p->m_pNext != NULL)
{
PrintListReverseRecursive(p->m_pNext);
}
std::cout << p->m_data << std::endl;
}
return 1;
}
#include "stdafx.h"
#include "slist.h"
#include <cstdlib>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int array[] = { 1, 2, 3,4 };
listNode* list = buildList(array,4);
listNode* list2 = ReverseSlistRecursive(list);
printList(list2);
system("pause");
return 0;
}