码迷,mamicode.com
首页 > 其他好文 > 详细

单链表

时间:2015-12-03 17:15:55      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:单链表

#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;

}


本文出自 “水仙花” 博客,请务必保留此出处http://10704527.blog.51cto.com/10694527/1719274

单链表

标签:单链表

原文地址:http://10704527.blog.51cto.com/10694527/1719274

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!