Mon, Mar 27, 2017, 01:56:17 PM
by YX13434

#include<iostream>
using namespace std;

class Node{
public:
int data;
Node *next;
void printNode();
};

void Node::printNode()
{
cout<<data<<" ";
}

class List{
public:
List();
bool ListInsert(int i,Node *pNode);
void ListTraverse();
private:
Node *m_pList;
int m_iLength;
};

List::List()
{
m_pList=new Node;
m_pList->data=0;
m_pList->next=NULL;
m_iLength=0; // 你的链表是有头节点的,m_iLength应该初始化为1
}

bool List::ListInsert(int i,Node *pNode)
{
if(i<0||i>m_iLength) // i<=0
{
return false;
}
Node *currentNode=m_pList;
for(int k=0;k<i;k++) // k < i - 1
{
currentNode=currentNode->next;
}
Node *newNode=new Node;
newNode->data=pNode->data;
newNode->next=currentNode->next;
currentNode->next=newNode;
m_iLength++;
return true;
}

void List::ListTraverse()
{
Node *currentNode=m_pList;
while(currentNode->next!=NULL)
{
currentNode=currentNode->next;
if(currentNode->data!=0&&currentNode->next!=NULL)
currentNode->printNode();
if(currentNode->data!=0&&currentNode->next==NULL)
cout<<currentNode->data;
}
}

int main()
{
int t;
cin>>t;
List *pList=new List();
for(int i=0;i<t;i++)
{
int m;
Node node;
Node node1;
cin>>m;
cin>>node1.data;
if(m>0)
{
node.data=node1.data;
}
if(pList->ListInsert(m,&node)==true&&m>0)
{
pList->ListTraverse();
cout<<endl;
}
else
{
cout<<"error"<<endl;
}
}
return 0;
}

Sat, Mar 25, 2017, 08:46:24 PM
by sina.893

#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
cout << "Try out your own codes now! :-)";
return 0 ;
}

...............