运行图及代码如下,如有帮助,请帮忙采纳一下,谢谢:
题1:
代码:
#include <iostream>
using namespace std;
struct StNode
{
int data;
struct StNode* next;
};
//创建节点
StNode* CreateNode(int d)
{
StNode* node = new StNode;
node->data = d;
node->next = 0;
return node;
}
//创建链表
StNode* CreateList()
{
StNode* head,*p,*t;
head = 0;
p = head;
t = head;
int data;
cout << "请输入链表中各节点的值(在一行中输入多个数字,以空格分隔,以回车结束):" <<endl;
while(1)
{
cin >> data;
t = CreateNode(data);
if(head ==0)
{
head = t;
p = head;
}
else
{
p->next = t;
p = t;
}
if (cin.get() == '\n')
break;
}
return head;
}
//打印链表
void Display(StNode* head)
{
cout << "打印链表:" << endl;
while(head)
{
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
//查找表中最大的值
void FindMax(StNode* head, StNode* &max,StNode* &min)
{
StNode* p;
p = head;
max = head;
min = head;
if(p == 0) return ;
p= p->next;
while(p)
{
if(p->data > max->data)
{
max = p;
}
if (p->data < min->data)
{
min = p;
}
p = p->next;
}
}
void Free(StNode* head)
{
StNode* p = head;
while(head)
{
p = head->next;
delete head; head = p;
}
}
int main()
{
StNode* A=0,*max = 0,*min = 0;
A = CreateList();
Display(A);
FindMax(A,max,min);
if(max != 0 && min != 0)
cout << "最大值:" << max->data << ",最小值"<<min->data << endl;
Free(A);
return 0;
}
题2:
代码:
#include <iostream>
#include <string>
using namespace std;
struct StNode
{
string name;
int data;
struct StNode* next;
};
//创建节点
StNode* CreateNode(string name,int d)
{
StNode* node = new StNode;
node->name = name;
node->data = d;
node->next = 0;
return node;
}
//创建链表
StNode* CreateList()
{
StNode* head,*p,*t;
string name;
head = 0;
p = head;
t = head;
int data;
cout << "请输入链表中各节点的值(在一行中输入以回车结束):" <<endl;
while(1)
{
cin >> name >> data;
t = CreateNode(name,data);
if(head ==0)
{
head = t;
p = head;
}
else
{
p->next = t;
p = t;
}
if (cin.get() == '\n')
break;
}
return head;
}
//打印链表
void Display(StNode* head)
{
cout << "打印链表:" << endl;
while(head)
{
cout << head->name <<":" << head->data << endl;
head = head->next;
}
cout << endl;
}
//查找表中最大的值
void FindMax(StNode* head, StNode* &max,StNode* &min)
{
StNode* p;
p = head;
max = head;
min = head;
if(p == 0) return ;
p= p->next;
while(p)
{
if(p->data > max->data)
{
max = p;
}
if (p->data < min->data)
{
min = p;
}
p = p->next;
}
}
void Free(StNode* head)
{
StNode* p = head;
while(head)
{
p = head->next;
delete head; head = p;
}
}
int main()
{
StNode* A=0,*max = 0,*min = 0;
A = CreateList();
Display(A);
FindMax(A,max,min);
if(max != 0 && min != 0)
{
cout << "最大成绩:" <<max->name << ":"<< max->data << endl;
cout << "最小成绩:" <<min->name << ":"<< min->data << endl;
}
Free(A);
return 0;
}