Problem A: 最小节点(线性表)
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 220 Solved: 185
[ Submit][ Status][ Web Board]
Description
(线性表)设有一个由正整数组成的无序(向后)单链表,编写完成下列功能的算法:找出最小值结点,且打印该数值。
Input
输入长度:6
输入数据:3 2 1 4 6 8
Output
1
Sample Input
6
11 14 5 6 8 9
Sample Output
5
HINT
#include <iostream>
using namespace std;
struct Node
{
int data; //结点的数据
struct Node *next; //指向下一结点
};
Node *head=NULL; //将链表头定义为全局变量,以便于后面操作
void make_list(); //建立链表
void out_list(); //输出链表
int main( )
{
make_list();
out_list();
return 0;
}
void make_list()
{
int n,t;
Node *p,*q;
cin>>t;
while(t--) //输入若干正数建立链表,输入非正数时,建立过程结束
{
cin>>n;
p=new Node;
p->data=n;
p->next=NULL;
if(head==NULL)
head=p;
else
q->next=p;
q=p;
}
return;
}
void out_list()
{
Node *p=head;
int min=p->data;
while(p!=NULL)
{
if(p->data<min) //查找是否有值为x的结点
min=p->data;
p=p->next;
}
cout<<min<<endl;
return;
}