链表创建 然后逆序 然后输出 上机测试题
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; }
相关文章推荐
- 简单链表添加,然后顺序或逆序输出
- 创建单项链表,然后实现单项链表逆序
- 【1139】数据结构上机测试2-2:单链表操作B (逆序建表+重复元素删除)
- 【1139】数据结构上机测试2-2:单链表操作B (逆序建表+重复元素删除)
- 两种方法实现单向链表的创建、遍历、删除、查找、逆序输出(循环法和递归法)
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- 链表的创建、取长、输出、插入、删除、逆序
- 链表的创建、逆序等操作
- 设计一个学生类,其中包含学号、姓名、成绩等数据成员,创建学生对象并且倒入到文件file.txt,然后由文件读取到另一个学生对象并输出,试编程实现。
- new的链表,创建链表,输出链表
- Problem Description 求n个数的最小公倍数。 Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。 Output 为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。 Sample
- 单链表逆序输出
- 实现链表的逆序输出和删除某节点
- 将一个链表逆序并输出
- 单链表创建及反向输出(接受0~9及#)
- 利用指针实现将5个整数输入到数组a中,然后将a逆序复制到数组b中,并输出b中各单元的值。
- 链表的创建,插入,删除,逆序
- 用单链表来实现逆序输出
- 链表的创建及逆序
- list 链表的 创建、输出(打印)、删除、插入 2011.07.26