单链表
2015-12-03 16:07
148 查看
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
//实现单链表:
typedef int DataType;
typedef struct ListNode
{
struct ListNode *_pNext;
DataType _data;
}ListNode;
ListNode* NewNode(DataType x)
{
ListNode *tem = (ListNode *)malloc(sizeof(ListNode));
tem->_data = x;
tem->_pNext = NULL;
return tem;
}
void Display(const ListNode* pHead)
{
while (pHead)
{
cout << pHead->_data <<"-";
pHead = pHead->_pNext;
}
cout << endl;
}
void PushFront(ListNode*& pHead,DataType x)
{
if (pHead == NULL)
{
pHead = NewNode(x);
}
else
{
ListNode *cur = NewNode(x);
cur->_pNext = pHead;
pHead = cur;
}
}
void PopFront(ListNode*& pHead)
{
if (pHead != NULL)
{
ListNode *del = pHead;
pHead = pHead->_pNext;
free(del);
}
}
void PushBack(ListNode*& pHead, DataType x)
{
if (pHead == NULL)
{
pHead = NewNode(x);
}
else
{
ListNode *tem = pHead;
while (tem->_pNext != NULL)
{
tem = tem->_pNext;
}//>>>>>>>>>>>>>>>循环到末尾
ListNode *cur = NewNode(x);
tem->_pNext = cur;
cur->_pNext = NULL;
}
}
void PopBack(ListNode*& pHead)
{
ListNode *tem = pHead;
while (tem->_pNext->_pNext != NULL)
{
tem = tem->_pNext;
}
free(tem->_pNext);
tem->_pNext = NULL;
}
ListNode* Find(ListNode*pHead, DataType x)
{
ListNode *tem = pHead;
while (tem->_pNext != NULL)
{
if (tem->_data == x)
{
return tem;
}
tem = tem->_pNext;
}
}
void Insert(ListNode*& pos, DataType x)
{
assert(pos);
ListNode* cur = NewNode(x);
cur->_pNext = pos->_pNext;
pos->_pNext = cur;
DataType tem = pos->_data;
pos->_data = cur->_data;
cur->_data = tem;
}
void Erase(ListNode*&pHead, ListNode*& pos)//>>>>>>>
{
assert(pHead);
assert(pos);
ListNode *cur = pHead;
while (cur->_pNext->_data != pos->_data)
{
cur = cur->_pNext;
}
if (cur->_pNext == NULL)
{
cout << "找不到此节点!" << endl;
}
else
{
cur->_pNext = pos->_pNext;
free(pos);
}
}
void Reverse(ListNode*& pHead)
{
ListNode* cur = pHead->_pNext;
pHead->_pNext = NULL;
while (cur != NULL)
{
PushFront(pHead, cur->_data);
ListNode* tem = cur;
cur = cur->_pNext;
free(tem);
}
}
ListNode* FindMiddle(ListNode*& pHead)
{
ListNode* first = pHead;
ListNode* slow = pHead;
while (first->_pNext != NULL)
{
first = first->_pNext;
if (first->_pNext != NULL)
{
first = first->_pNext->_pNext;
}
slow = slow->_pNext;
}
return slow;
}
void EraseLastNode(ListNode*& pos)
{
ListNode *cur = pos->_pNext;
DataType tem = pos->_data;
pos->_data = cur->_data;
cur->_data = tem;
pos->_pNext = cur->_pNext;
free(cur);
}
//test PushFront and PopFront
void test1()
{
ListNode *pHead = NULL;
PushFront(pHead, 1);
PushFront(pHead, 2);
PushFront(pHead, 3);
Display(pHead);
PopFront(pHead);
Display(pHead);
}
//test PushBack PopBack
void test2()
{
ListNode *pHead = NULL;
PushBack(pHead, 1);
PushBack(pHead, 2);
PushBack(pHead, 3);
Display(pHead);
PopBack(pHead);
Display(pHead);
}
//test Insert Erase Reverse
void test3()
{
ListNode *pHead = NULL;
PushBack(pHead, 1);
PushBack(pHead, 2);
PushBack(pHead, 3);
ListNode *pos = Find(pHead, 2);
Display(pHead);
Insert(pos, 10);
Display(pHead);
//Erase(pHead, pos);
//Display(pHead);
Reverse(pHead);
Display(pHead);
}
//test FindMiddle EraselastNode
void test4()
{
ListNode *pHead = NULL;
PushBack(pHead, 1);
PushBack(pHead, 2);
PushBack(pHead, 3);
PushBack(pHead, 4);
PushBack(pHead, 5);
Display(pHead);
//ListNode *cur = FindMiddle(pHead);
//cout << cur->_data << endl;
ListNode *pos = Find(pHead, 2);
EraseLastNode(pos);
Display(pHead);
}
int main()
{
//test1();
//test2();
//test3();
test4();
system("pause");
return 0;
}
#include<iostream>
#include<assert.h>
using namespace std;
//实现单链表:
typedef int DataType;
typedef struct ListNode
{
struct ListNode *_pNext;
DataType _data;
}ListNode;
ListNode* NewNode(DataType x)
{
ListNode *tem = (ListNode *)malloc(sizeof(ListNode));
tem->_data = x;
tem->_pNext = NULL;
return tem;
}
void Display(const ListNode* pHead)
{
while (pHead)
{
cout << pHead->_data <<"-";
pHead = pHead->_pNext;
}
cout << endl;
}
void PushFront(ListNode*& pHead,DataType x)
{
if (pHead == NULL)
{
pHead = NewNode(x);
}
else
{
ListNode *cur = NewNode(x);
cur->_pNext = pHead;
pHead = cur;
}
}
void PopFront(ListNode*& pHead)
{
if (pHead != NULL)
{
ListNode *del = pHead;
pHead = pHead->_pNext;
free(del);
}
}
void PushBack(ListNode*& pHead, DataType x)
{
if (pHead == NULL)
{
pHead = NewNode(x);
}
else
{
ListNode *tem = pHead;
while (tem->_pNext != NULL)
{
tem = tem->_pNext;
}//>>>>>>>>>>>>>>>循环到末尾
ListNode *cur = NewNode(x);
tem->_pNext = cur;
cur->_pNext = NULL;
}
}
void PopBack(ListNode*& pHead)
{
ListNode *tem = pHead;
while (tem->_pNext->_pNext != NULL)
{
tem = tem->_pNext;
}
free(tem->_pNext);
tem->_pNext = NULL;
}
ListNode* Find(ListNode*pHead, DataType x)
{
ListNode *tem = pHead;
while (tem->_pNext != NULL)
{
if (tem->_data == x)
{
return tem;
}
tem = tem->_pNext;
}
}
void Insert(ListNode*& pos, DataType x)
{
assert(pos);
ListNode* cur = NewNode(x);
cur->_pNext = pos->_pNext;
pos->_pNext = cur;
DataType tem = pos->_data;
pos->_data = cur->_data;
cur->_data = tem;
}
void Erase(ListNode*&pHead, ListNode*& pos)//>>>>>>>
{
assert(pHead);
assert(pos);
ListNode *cur = pHead;
while (cur->_pNext->_data != pos->_data)
{
cur = cur->_pNext;
}
if (cur->_pNext == NULL)
{
cout << "找不到此节点!" << endl;
}
else
{
cur->_pNext = pos->_pNext;
free(pos);
}
}
void Reverse(ListNode*& pHead)
{
ListNode* cur = pHead->_pNext;
pHead->_pNext = NULL;
while (cur != NULL)
{
PushFront(pHead, cur->_data);
ListNode* tem = cur;
cur = cur->_pNext;
free(tem);
}
}
ListNode* FindMiddle(ListNode*& pHead)
{
ListNode* first = pHead;
ListNode* slow = pHead;
while (first->_pNext != NULL)
{
first = first->_pNext;
if (first->_pNext != NULL)
{
first = first->_pNext->_pNext;
}
slow = slow->_pNext;
}
return slow;
}
void EraseLastNode(ListNode*& pos)
{
ListNode *cur = pos->_pNext;
DataType tem = pos->_data;
pos->_data = cur->_data;
cur->_data = tem;
pos->_pNext = cur->_pNext;
free(cur);
}
//test PushFront and PopFront
void test1()
{
ListNode *pHead = NULL;
PushFront(pHead, 1);
PushFront(pHead, 2);
PushFront(pHead, 3);
Display(pHead);
PopFront(pHead);
Display(pHead);
}
//test PushBack PopBack
void test2()
{
ListNode *pHead = NULL;
PushBack(pHead, 1);
PushBack(pHead, 2);
PushBack(pHead, 3);
Display(pHead);
PopBack(pHead);
Display(pHead);
}
//test Insert Erase Reverse
void test3()
{
ListNode *pHead = NULL;
PushBack(pHead, 1);
PushBack(pHead, 2);
PushBack(pHead, 3);
ListNode *pos = Find(pHead, 2);
Display(pHead);
Insert(pos, 10);
Display(pHead);
//Erase(pHead, pos);
//Display(pHead);
Reverse(pHead);
Display(pHead);
}
//test FindMiddle EraselastNode
void test4()
{
ListNode *pHead = NULL;
PushBack(pHead, 1);
PushBack(pHead, 2);
PushBack(pHead, 3);
PushBack(pHead, 4);
PushBack(pHead, 5);
Display(pHead);
//ListNode *cur = FindMiddle(pHead);
//cout << cur->_data << endl;
ListNode *pos = Find(pHead, 2);
EraseLastNode(pos);
Display(pHead);
}
int main()
{
//test1();
//test2();
//test3();
test4();
system("pause");
return 0;
}
相关文章推荐
- C#定义并实现单链表实例解析
- C#数据结构之单链表(LinkList)实例详解
- C语言实现单链表逆序与逆序输出实例
- C语言单链表常见操作汇总
- C数据结构之单链表详细示例分析
- C++中单链表的建立与基本操作
- 深入单链表的快速排序详解
- java实现单链表中是否有环的方法详解
- Python单链表的简单实现方法
- Go语言单链表实现方法
- 结构之美:在单链表指定位置插入数据
- 《编程之美》3.4:没有头结点的单链表如何删除结点
- 结构之美:使用尾插法创建单链表
- 单链表实现
- 单链表的实现及其操作
- 判断单链表是否有环
- 单链表建环,无环链表变有环
- 用头插法实现单链表整表创建
- 单链表的删除某个元素的操作
- 单链表的插入与遍历操作