@【数据结构】(字符串链表相关功能实现)
从键盘输入一行以“,”分隔的一组整数,求数据的个数,数据的最大值,最小值,并输出
【函数将整数字符串分割到一个字符串链表中,然后将字符串链表中存储的整数串转换成整数,存储到一连续的存储空间中,最后输出整数的个数及最大数和最小数】
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#define Max_Size 15
using namespace std;
typedef struct LNode
{
char data;
struct LNode *next;
}LNode, *Linkstring;
Linkstring Create()
{
LNode *p;
Linkstring L;
L = (Linkstring)malloc(sizeof(LNode)); // 申请头结点空间
L->next = NULL;
p = L;
char A[20]; int i = 0;
cout << "请输入以‘,’分隔的整数串,输入‘#'结束:" << endl;
cin >> A[i];
while (A[i] != '#')
{
p->next = (LNode*)malloc(sizeof(LNode));
p->next->data = A[i];
p = p->next;
i++;
cin >> A[i];
}
p->next = NULL;
return L;
}
void Strcreate(Linkstring L,int B[],int &m) //将整数串中的元素存入一维数组中
{
LNode *q = L->next;
int i = 0;
B[i] = 0;
while (q!=NULL)
{
if (q->data >= '0'&&q->data <= '9')
{
B[i] = B[i] * 10 + q->data - 48;
q = q->next;
}
else
{
q = q->next;
i++;
B[i] = 0;
}
}
cout << "输出数组中的元素:" << endl;
for (int j = 0; j <= i; j++)
cout << B[j] << " ";
cout << endl;
m = i;
}
void Max(int B[10], int m)
{
int Max = B[0];
int t;
for (int i = 1; i <= m ; i++)
{
if (Max < B[i])
Max = B[i];
}
cout << "数据中的最大值:" << Max<<endl;
}
void Min(int B[10],int m)
{
int Min = B[0];
int t;
for (int i = 1; i <= m; i++)
{
if (Min > B[i])
Min = B[i];
}
cout << "数据中的最小值:" << Min << endl;
}
void main()
{
LNode *p;
Linkstring L;
L= (Linkstring)malloc(sizeof(LNode));
L->next = NULL; // 初始化,置空表
p = L;
char C[20];
int B[10]; int m;
L = Create();
Strcreate(L,B,m);
Max(B, m);
Min(B, m);
system("pause");
}
测试: