加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 06:33:25 所属栏目:安全 来源:网络整理
导读:上代码,自己模拟 #includeiostreamusing 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;cins-d

上代码,自己模拟

#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;
}
写程序的时候纠结了一个小时,不出结果,还以为链表建错了,最后发现没输出,汗!!!!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读