題目:
1、在第一個node之前,新增一個node
2、把第二個node刪除
3、在第二、三個node之間,插入新的node
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
class mylist
{
public:
int data;
mylist *next;
};
void show_data(mylist *front)
{
mylist *tptr;
if(front)
{
cout<<"List's data is ";
for(tptr=front;tptr!=0;tptr=tptr->next)
{
cout<<tptr->data<<" ";
}
cout<<"\n"<<endl;
}
}
int main()
{
mylist *front=0,*rear=0,*newptr=0,*tempptr=0;
int a,value;
int i;
for(i=4;i>0;i--)
{
cout<<"Add node?(1)yes,(0)No."<<endl;
cin>>a;
if(a==1)
{
cout<<"Key in a number value:"<<endl;
cin>>value;
newptr = new mylist;
newptr->data = value;
if(rear==0)
{
rear = newptr;
rear->next = 0;
}
else
{
newptr->next = 0;
rear->next = newptr;
rear = newptr;
}
if(front==0){front = rear;}
}
else if(a==0)
{
if(front==0)cout<<"list empty"<<endl;
else
{
tempptr = front;
front = front->next;
value = tempptr->data;
delete tempptr;
cout<<"the value is "<<value<<endl;
}
}
else{cout<<"Wrong input"<<endl;}
}
/**1**/
cout<<"/**1**/"<<endl;
cout<<"Add a node in front of frontptr:"<<endl;
{
cout<<"Key in a number value:"<<endl;
cin>>value;
newptr = new mylist;
newptr->data = value;
newptr->next = front;
front= newptr;
}
show_data(front);
/**2**/
cout<<"/**2**/"<<endl;
mylist *third_node;
tempptr = front;
tempptr = tempptr->next;
third_node = tempptr->next;
front->next = third_node;
value = tempptr->data;
cout<<"Get out the value "<<value<<endl;
delete tempptr;
show_data(front);
/**3**/
cout<<"/**3**/"<<endl;
mylist *second_node;
tempptr = front;
tempptr = tempptr->next;
second_node = tempptr;
tempptr = second_node->next;
cout<<"Key in a number value:"<<endl;
cin>>value;
newptr = new mylist;
newptr->data = value;
second_node->next = newptr;
newptr->next = tempptr;
show_data(front);
return 0;
}
留言列表