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

代码,自己模拟

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

相关文章

【啊哈!算法】算法3:最常用的排序——快速排序       ...
匿名组 这里可能用到几个不同的分组构造。通过括号内围绕的正...
选择排序:从数组的起始位置处开始,把第一个元素与数组中其...
public struct Pqitem { public int priority; ...
在编写正则表达式的时候,经常会向要向正则表达式添加数量型...
来自:http://blog.csdn.net/morewindows/article/details/6...