记录·链表逆序

记录·链表逆序

#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct Node{
	int data;
	struct Node* next;
}node; 

node* CreatList(){
	node* head=new node;
	head->next=NULL;
	node* last=head;
	int n;
	cin>>n;
	int num;
	for(int i=0;i<n;i++){
		cin>>num;
		node* p=new node;
		p->data=num;
		p->next=NULL;
		last->next=p;
		last=p;
	}
	return head;
}

void NiXu_List(node* head){
	node *p,*prev,*latter;
	p=head->next;
	prev=NULL;
	latter=p->next;
	while(p!=NULL){
		p->next=prev;
		prev=p;
		p=latter;
		if(p!=NULL)
			latter=p->next;
	}
	head->next=prev;
}

void PrintList(node* head){
	for(node* p=head->next;p;p=p->next){
		cout<<p->data<<" ";
	}
}
int main(){
	node* head=CreatList();
	NiXu_List(head);
	PrintList(head);
	return 0;
}

 

上一篇:Java基础之LinkedList


下一篇:js中reduce的方法和去重