您的位置:首页 > 其它

单链表

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  单链表