微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【数据结构】单向有序链表---最水的代码

代码,自己模拟

#include<iostream>
using namespace std;
struct node
{
	int data;
	node *next;
} ;
void search(node *,node *);
void insert(node *,node *,node *);
void print(node *);
int main()
{
	node *head,*p,*s;
	s=new node;
	head=new node;
	head->next=s;
	cin>>s->data;
	p=s;
	s->next=NULL;
	while(s->data!=0)
	{
		s=new node;
		cin>>s->data;
		search(head,s);
	}
	print(head);
}
void search(node *head,node *s)
{
	node *p;
	bool swi;
	swi=false;
	for(p=head->next;p->next!=NULL;p=p->next)
	{
		if(s->data>=p->data&&s->data<p->next->data)
		{
				insert(p,p->next,s);
				swi=1;
				goto abc;
		}
	}
	if(swi==false)
		insert(p,s);
abc: ;
}
void insert(node *p,node *s)
{
	p->next=s;
	s->next=NULL;
}
void insert(node *p,node *q,node *s)
{
	s->next=q;
	p->next=s;
}
void print(node *head)
{
	node *p;
	for(p=head->next;p->next!=NULL;p=p->next)
		cout<<" "<<p->data;
}
写程序的时候纠结了一个小时,不出结果,还以为链表建错了,最后发现没输出,汗!!!!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐