[ create a new paste ] login | about

Link: http://codepad.org/k6gh7JbK    [ raw code | output | fork ]

C++, pasted on Jan 6:
#include <cstdlib>
#include <iostream>

using namespace std;

 class node 
 {
 	public:
   	int  data;
   	node *next;
 };
 
 void fo(node *list) //輸出 
 {
 	while(list!=NULL)
	{
		cout<<list->data<<endl;
		list=list->next;
	}
 }
 
node* invertList (node *oldList)
 {
	node *newList,*temp;
        newList=NULL;
	
	while(oldList!=NULL)
	{	
		temp=oldList->next;     //暫存,以便於之後更新oldList
		oldList->next=newList;  //將切斷的oldnode接到新的List
		newList=oldList;        //更新newList
		oldList=temp;           //更新oldList
	}
	
	return newList;
	 
 }

void invertListByAdress (node **oldList)
 {
	node *newList,*temp;
        newList=NULL;
	
	while(*oldList!=NULL)
	{	
		temp=(*oldList)->next;
		(*oldList)->next=newList;
		newList=*oldList;
		*oldList=temp;
	}
	
	*oldList=newList;
	 
 }
 

int main(int argc, char *argv[])
{ 
	node *a=new node,*b = new node, *c=new node;
	node *first=a;
	
	a->data=10;
	b->data=20; 
	c->data=30;
	a->next=b;
	b->next=c;
	c->next=NULL;
	
	fo(first);
	
	//first=invertList(first);
        invertListByAdress(&first);
	
	fo(first);
	

    system("PAUSE");
    return EXIT_SUCCESS;
}


Output:
1
2
3
4
5
6
7
8
10
20
30
30
20
10

Disallowed system call: SYS_fork


Create a new paste based on this one


Comments: