求用单向链表做!!!急!!!4小时后上课
查看原帖
求用单向链表做!!!急!!!4小时后上课
1432277
limoxuan0712楼主2025/1/14 09:45

老师没教我们双向链表...... 看看这段酣畅淋漓却错误的代码吧(4小时后上课)

#include<iostream>
#include<list>
using namespace std;

struct node{
	int date;
	node* Next;
};

void Print_list(node* T){
	if(T==NULL) return ;
	while(T != NULL){
		cout<<T->date<<" ";
		T=T->Next;
	}
	cout<<endl;
}

void Head_list(node* &T,int val){
	if(T==NULL){
		node* p=new node;
		p->date=val;
		p->Next=NULL;
		T=p;
		return ;
	}
	node* p=new node;
	p->date=val;
	p->Next=T;
	T=p;
}
int main(){
	int n,x;
	cin>>n;
	node* T=NULL;
	while(cin>>x && x!=0){
		Head_list(T,x);
	}
	Print_list(T);
	delete T;
	return 0;
}
/*
6
35 12 99 26 8 66
*/
2025/1/14 09:45
加载中...