2个回答
展开全部
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define __TEST_VRSER__
struct SUer{
int sNumber;
char sName[20];
float fMath;
};
typedef SUer DATA;
struct SNode{
DATA data;
SNode *pNext;
};
SNode *g_pHead = NULL;
void AddHead(DATA _data){
SNode *p = (SNode*)malloc(sizeof(SNode));
p->data = _data;
p->pNext = g_pHead;
g_pHead = p;
}
void AddTalie(DATA _data){
SNode *pNew = (SNode*)malloc(sizeof(SNode));
pNew->data = _data;
pNew->pNext = NULL;
if (!g_pHead) {
g_pHead = pNew;
}
SNode *p = g_pHead;
while(p->pNext != NULL)
p = p->pNext;
p->pNext = pNew;
}
void Print(){
SNode *p = g_pHead;
while (p) {
printf("%d\t%s\t%0.2f\n",p->data.sNumber,p->data.sName,p->data.fMath);
#ifdef __TEST_VRSER__
printf("p=0x%p\n",p);
#endif
p = p->pNext;
}
printf("\n");
}
void Modinfy(){
int number;
printf("请输入你要查询的学号:");
scanf("%d",&number);
SNode *p = g_pHead;
while(p->pNext){
if (p->data.sNumber == number) {
break;
}
p = p->pNext;
}
if (!p) {
puts("你输入的学号不存在:");
return;
}
printf("%d\t\%s\t\%0.1f\n\n",p->data.sNumber,p->data.sName,p->data.fMath);
printf("请输入姓名和成绩");
scanf("%s",p->data.sName,sizeof(p->data.sName));
scanf("%f",&p->data.fMath);
Print();
}
void Input(const int &_dt){
int m;
printf("请输入学号: \n");
scanf("%d",&m);
DATA gData;
gData.sNumber = m;
printf("请输入名字: \n");
scanf("%s",gData.sName,sizeof(gData.sName));
printf("请输入成绩: \n");
scanf("%f",&gData.fMath);
if (1 == _dt){
AddHead(gData);
}else if(2 == _dt){
AddTalie(gData);
}
}
int Find(const int &_data){
SNode *p = g_pHead;
int i = 0;
while (p) {
if (p->data.sNumber == _data) {
return i;
}
i++;
p = p->pNext;
}
return -1;
}
int Delete(const int &nNumber){
SNode *p = g_pHead;
SNode *p1 = NULL;
if (!p)
return 0;
if (p->data.sNumber == nNumber) {
g_pHead = p->pNext;
free(p);
return 1;
}
while (p) {
if (p->data.sNumber == nNumber) {
p1->pNext = p->pNext;
free(p);
return 1;
}
p1 = p;
p = p->pNext;
}
return NULL;
}
int Judge(SNode *q,SNode *m,const int &nIndx){
switch (nIndx) {
case 1:
return q->data.sNumber < m->data.sNumber;
case 2:
return strcmp(q->data.sName, m->data.sName) <0;
case 3:
//return q->data.fMath < m->data.fMath ? q->data.fMath : m->data.fMath;
return q->data.fMath < m->data.fMath;
break;
}
return 0;
}
void Sort(const int &Indx){
SNode *p = g_pHead;
if (!g_pHead) {
printf("你当前的数据为空");
return;
}
while (p->pNext) {
SNode *q = p->pNext;
SNode *m = p;
while (q) {
//if (q->data.fMath < m->data.fMath) {
if(Judge(q, m, Indx)){
m = q;
q = q->pNext;
}
}
if (m != p) {
DATA t = p->data;
p->data = m->data;
m->data = t;
}
p = p->pNext;
}
}
void PrintS(SNode *ps[]){
puts("学号\t姓名\t成绩");
int i = 0;
while (ps[i]) {
//SNode *p = ps[i];
//printf("打印外排链表:%d\t%s\t%0.2f\n",p->data.sNumber,
// p->data.sName,p->data.fMath);
printf("打印外排链表:%d\t%s\t%0.2f\n",ps[i]->data.sNumber,
ps[i]->data.sName,ps[i]->data.fMath);
i++;
}
}
//表外排序
void SortWai(const int &Indx){
if (!g_pHead) {
return;
}
SNode *p = g_pHead;
// SNode *ps[100] ={0};
int sum = 0;
int i = 0;
while (p) { //求链表个数
p = p->pNext;
++sum;
}
SNode* *ps = (SNode**)malloc(sizeof(SNode*)*(sum+1));//sum+1 malloc 动态申请数组的时候对申请一个作为空节点判断结束的时候用,也可以在插入的时候添加一个GetSize()函数去记录插入的个数;
sum = 0;
p = g_pHead;
while (p) {
ps[sum] = p;
p = p->pNext;
++sum;
}
ps[sum] = NULL;
while (i < sum - 1) {
int j = i + 1;
int m = i;
while (j < sum) {
if (ps[j]->data.sNumber < ps[m]->data.sNumber) //新手注意这个里的ps[xx]是一个二级指针 也就是SNode -> pa[xxx].xx
m = j;
++j;
}
if (m != i) {
SNode *t = ps[i];
ps[i] = ps[m];
ps[m] = t;
}
++i;
}
// ps[i] = NULL;
PrintS(ps);
free(ps);
}
void PT(){
printf("****************************** \n");
printf("****** c测试 ******* \n");
printf("****************************** \n");
}
int main(int argc, const char * argv[]){
#ifdef __TEST_VRSER__
puts("当前版本是测试版");
#else
PT();
#endif
Input(1);
Input(2);
Input(1);
// Input(2);
Print();
SortWai(1);
return 0;
}
#include <malloc.h>
#include <string.h>
#define __TEST_VRSER__
struct SUer{
int sNumber;
char sName[20];
float fMath;
};
typedef SUer DATA;
struct SNode{
DATA data;
SNode *pNext;
};
SNode *g_pHead = NULL;
void AddHead(DATA _data){
SNode *p = (SNode*)malloc(sizeof(SNode));
p->data = _data;
p->pNext = g_pHead;
g_pHead = p;
}
void AddTalie(DATA _data){
SNode *pNew = (SNode*)malloc(sizeof(SNode));
pNew->data = _data;
pNew->pNext = NULL;
if (!g_pHead) {
g_pHead = pNew;
}
SNode *p = g_pHead;
while(p->pNext != NULL)
p = p->pNext;
p->pNext = pNew;
}
void Print(){
SNode *p = g_pHead;
while (p) {
printf("%d\t%s\t%0.2f\n",p->data.sNumber,p->data.sName,p->data.fMath);
#ifdef __TEST_VRSER__
printf("p=0x%p\n",p);
#endif
p = p->pNext;
}
printf("\n");
}
void Modinfy(){
int number;
printf("请输入你要查询的学号:");
scanf("%d",&number);
SNode *p = g_pHead;
while(p->pNext){
if (p->data.sNumber == number) {
break;
}
p = p->pNext;
}
if (!p) {
puts("你输入的学号不存在:");
return;
}
printf("%d\t\%s\t\%0.1f\n\n",p->data.sNumber,p->data.sName,p->data.fMath);
printf("请输入姓名和成绩");
scanf("%s",p->data.sName,sizeof(p->data.sName));
scanf("%f",&p->data.fMath);
Print();
}
void Input(const int &_dt){
int m;
printf("请输入学号: \n");
scanf("%d",&m);
DATA gData;
gData.sNumber = m;
printf("请输入名字: \n");
scanf("%s",gData.sName,sizeof(gData.sName));
printf("请输入成绩: \n");
scanf("%f",&gData.fMath);
if (1 == _dt){
AddHead(gData);
}else if(2 == _dt){
AddTalie(gData);
}
}
int Find(const int &_data){
SNode *p = g_pHead;
int i = 0;
while (p) {
if (p->data.sNumber == _data) {
return i;
}
i++;
p = p->pNext;
}
return -1;
}
int Delete(const int &nNumber){
SNode *p = g_pHead;
SNode *p1 = NULL;
if (!p)
return 0;
if (p->data.sNumber == nNumber) {
g_pHead = p->pNext;
free(p);
return 1;
}
while (p) {
if (p->data.sNumber == nNumber) {
p1->pNext = p->pNext;
free(p);
return 1;
}
p1 = p;
p = p->pNext;
}
return NULL;
}
int Judge(SNode *q,SNode *m,const int &nIndx){
switch (nIndx) {
case 1:
return q->data.sNumber < m->data.sNumber;
case 2:
return strcmp(q->data.sName, m->data.sName) <0;
case 3:
//return q->data.fMath < m->data.fMath ? q->data.fMath : m->data.fMath;
return q->data.fMath < m->data.fMath;
break;
}
return 0;
}
void Sort(const int &Indx){
SNode *p = g_pHead;
if (!g_pHead) {
printf("你当前的数据为空");
return;
}
while (p->pNext) {
SNode *q = p->pNext;
SNode *m = p;
while (q) {
//if (q->data.fMath < m->data.fMath) {
if(Judge(q, m, Indx)){
m = q;
q = q->pNext;
}
}
if (m != p) {
DATA t = p->data;
p->data = m->data;
m->data = t;
}
p = p->pNext;
}
}
void PrintS(SNode *ps[]){
puts("学号\t姓名\t成绩");
int i = 0;
while (ps[i]) {
//SNode *p = ps[i];
//printf("打印外排链表:%d\t%s\t%0.2f\n",p->data.sNumber,
// p->data.sName,p->data.fMath);
printf("打印外排链表:%d\t%s\t%0.2f\n",ps[i]->data.sNumber,
ps[i]->data.sName,ps[i]->data.fMath);
i++;
}
}
//表外排序
void SortWai(const int &Indx){
if (!g_pHead) {
return;
}
SNode *p = g_pHead;
// SNode *ps[100] ={0};
int sum = 0;
int i = 0;
while (p) { //求链表个数
p = p->pNext;
++sum;
}
SNode* *ps = (SNode**)malloc(sizeof(SNode*)*(sum+1));//sum+1 malloc 动态申请数组的时候对申请一个作为空节点判断结束的时候用,也可以在插入的时候添加一个GetSize()函数去记录插入的个数;
sum = 0;
p = g_pHead;
while (p) {
ps[sum] = p;
p = p->pNext;
++sum;
}
ps[sum] = NULL;
while (i < sum - 1) {
int j = i + 1;
int m = i;
while (j < sum) {
if (ps[j]->data.sNumber < ps[m]->data.sNumber) //新手注意这个里的ps[xx]是一个二级指针 也就是SNode -> pa[xxx].xx
m = j;
++j;
}
if (m != i) {
SNode *t = ps[i];
ps[i] = ps[m];
ps[m] = t;
}
++i;
}
// ps[i] = NULL;
PrintS(ps);
free(ps);
}
void PT(){
printf("****************************** \n");
printf("****** c测试 ******* \n");
printf("****************************** \n");
}
int main(int argc, const char * argv[]){
#ifdef __TEST_VRSER__
puts("当前版本是测试版");
#else
PT();
#endif
Input(1);
Input(2);
Input(1);
// Input(2);
Print();
SortWai(1);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询