您的位置:首页 > 其它

链表创建 然后逆序 然后输出 上机测试题

2014-08-07 21:55 253 查看
//============================================================================
// Name        : mm.cpp
// Author      : sfe
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef struct tagListNode
{
int value;
struct tagListNode *next;
}ListNode;
ListNode *CreatList(const int *NumBuffer,const int &iNodeNum)
{
ListNode *pListHead ;
ListNode *pListPP ;
for(int i = 0 ;i < iNodeNum; i++)
{
ListNode * ListNodeTemp = new ListNode();//(ListNode *)malloc(sizeof(ListNode));
ListNodeTemp ->value = NumBuffer[i];
ListNodeTemp->next = NULL;
if(i == 0){
pListHead =  ListNodeTemp;
}
else
pListPP->next = ListNodeTemp;
pListPP = ListNodeTemp;
}
// std::cout<<"After CreatList"<<std::endl;
//   while(NULL != pListHead)
//   {
//   std::cout<<pListHead->value<<std::endl;
//   pListHead = pListHead->next;
//   }

return pListHead;
}
ListNode * ReversetList(ListNode *pListNodeHead)
{
ListNode *pListHead ;
ListNode * ListNodeTemp = NULL, *ListNodeFormer = pListNodeHead,
*ListNodeAfter = pListNodeHead->next;
while(ListNodeAfter != NULL)
{
ListNodeTemp = ListNodeAfter->next;
ListNodeAfter->next = ListNodeFormer;
ListNodeFormer =  ListNodeAfter;
ListNodeAfter = ListNodeTemp;
}
pListNodeHead->next = NULL;
pListNodeHead = ListNodeFormer;
pListHead = pListNodeHead;

// std::cout<<"After ReversetList"<<std::endl;
// while(NULL != pListNodeHead)
// {
// std::cout<<pListNodeHead->value<<std::endl;
// pListNodeHead = pListNodeHead->next;
// }
return pListHead;
}
int main()
{
string strInput ;
string strTemp("");
std::cin>>strInput;
int iNum = 0;
int j = 0;
int iLength = strInput.length();
for(int i =0 ; i < iLength ; i++)
{
if(strInput[i] == ',')
iNum++;
}
iNum++;
int *NumBuffer = new int[iNum];
for(int i = 0 ;i < iLength ;i++)
{
if(strInput[i] == ',' )
{
NumBuffer[j++] = atoi(strTemp.c_str());
strTemp = "";
//std::cout<<NumBuffer[j-1]<<std::endl;
}
else
strTemp += strInput[i];
if((i+1) == iLength)
{
NumBuffer[j++] = atoi(strTemp.c_str());
}
}
ListNode * pListNodeHead = CreatList(NumBuffer,iNum);
pListNodeHead = ReversetList(pListNodeHead);
//ListNode * ListNodeTemp  = pListNodeHead;
while(NULL != pListNodeHead)
{
if(pListNodeHead->next != NULL)
std::cout<<pListNodeHead->value<<",";
else
std::cout<<pListNodeHead->value;
delete pListNodeHead;
pListNodeHead = pListNodeHead->next;
}
delete[]NumBuffer;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐