void swap(struct student *head, int lIndex, int rIndex){
struct student *lNode = get(lIndex, head);
struct student *rNode = get(rIndex, head);
struct student *tmp = (struct student *)malloc(SIZE);
strcpy(tmp->name, lNode->name);
strcpy(tmp->no, lNode->no);
tmp->score = lNode->score;
strcpy(lNode->name, rNode->name);
strcpy(lNode->no, rNode->no);
lNode->score = rNode->score;
strcpy(rNode->name, tmp->name);
strcpy(rNode->no, tmp->no);
rNode->score = tmp->score;
}
void sort(struct student *head){
for(int i = 0; i < size(head); i++){
for(int j = 0; j < size(head) - i - 1; j++){
if((head+j)->score-(head+j+1)->score>0){
swap(head, j, j+1);
}
}
}
}
#include
#include
#include
#define SIZE sizeof(struct student)
struct student
{
char name[20];
char no[8];
float score;
struct student *next;
};
struct student *create(){
struct student *p1,*p2,*head;
int count = 0;
p1 = p2 = head = (struct student *)malloc(SIZE);
scanf("%s%s%f", &p1->name, &p1->no,&p1->score);
while(strcmp(p1->no, "0")!=0){
if(count!=0){
p2->next=p1;
p2=p1;
}
p1=(struct student *)malloc(SIZE);
scanf("%s%s%f", &p1->name, &p1->no,&p1->score);
++count;
}
p2->next = NULL;
return count == 0?NULL:head;
}
void output(struct student *head){
struct student *p = head;
while(p!=NULL){
printf("%s %s:%3.1f\n", p->name, p->no, p->score);
p=p->next;
}
}
int search(char *no, struct student *head){
struct student *p = head;
int count = 0;
while(p!=NULL){
if(strcmp(p->no, no)==0){
break;
}
p=p->next;
++count;
}
return count;
}
void removeAtIndex(int index, struct student *head){
struct student *p = head;
for(int i = 0; i < index-1; i++){
p = p->next;
}
p->next = p->next->next;
}
struct student *get(int index, struct student *head){
struct student *p = head;
return p+index;
}
int menu(){
printf("\t\t\t*********************************\n");
printf("\t\t\t* 1.Input *\n");
printf("\t\t\t* 2.Output *\n");
printf("\t\t\t* 3.Sort *\n");
printf("\t\t\t* 4.Insert *\n");
printf("\t\t\t* 5.Delete *\n");
printf("\t\t\t* 6.Modify *\n");
printf("\t\t\t* 7.Quit *\n");
printf("\t\t\t*********************************\n");
int choice;
scanf("%d",&choice);
return choice;
}
int size(struct student *head){
struct student *p = head;
int count = 0;
while(p!=NULL){
++count;
p = p->next;
}
return count;
}
void swap(struct student *head, int lIndex, int rIndex){
struct student *lNode = get(lIndex, head);
struct student *rNode = get(rIndex, head);
struct student *tmp = (struct student *)malloc(SIZE);
strcpy(tmp->name, lNode->name);
strcpy(tmp->no, lNode->no);
tmp->score = lNode->score;
strcpy(lNode->name, rNode->name);
strcpy(lNode->no, rNode->no);
lNode->score = rNode->score;
strcpy(rNode->name, tmp->name);
strcpy(rNode->no, tmp->no);
rNode->score = tmp->score;
}
void sort(struct student *head){
for(int i = 0; i < size(head); i++){
for(int j = 0; j < size(head) - i - 1; j++){
if((head+j)->score-(head+j+1)->score>0){
swap(head, j, j+1);
}
}
}
}
void main()
{
struct student *head;
int choice;
while(true){
switch(choice=menu()){
case 1:
head = create();
break;
case 2:
output(head);
break;
case 3:
sort(head);
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
return;
break;
default:
printf("invilid input!\n");
break;
}
}
}
#include
#include
#include
#define SIZE sizeof(struct student)
struct student
{
char name[20];
char no[8];
float score;
struct student *next;
};
struct student *create(void){
struct student *p1,*p2,*head;
int count = 0;
p1 = p2 = head = (struct student *)malloc(SIZE);
// scanf("%s%s%f", &p1->name, &p1->no,&p1->score);
scanf("%s%s%f", p1->name, p1->no,&p1->score);
while(strcmp(p1->no, "0")!=0){
if(count!=0){
p2->next=p1;
p2=p1;
}
p1=(struct student *)malloc(SIZE);
// scanf("%s%s%f", &p1->name, &p1->no,&p1->score);
scanf("%s%s%f", p1->name, p1->no,&p1->score);
++count;
}
p2->next = NULL;
return count == 0?NULL:head;
}
void output(struct student *head){
struct student *p = head;
while(p!=NULL){
printf("%s %s:%3.1f\n", p->name, p->no, p->score);
p=p->next;
}
}
int search(char *no, struct student *head){
struct student *p = head;
int count = 0;
while(p!=NULL){
if(strcmp(p->no, no)==0){
break;
}
p=p->next;
++count;
}
return count;
}
void removeAtIndex(int index, struct student *head){
struct student *p = head;
for(int i = 0; i < index-1; i++){
p = p->next;
}
p->next = p->next->next;
}
struct student *get(int index, struct student *head){
struct student *p = head;
// return p+index;
while (index--)
p = p->next;
return p;
}
int menu(void){
printf("\t\t\t*********************************\n");
printf("\t\t\t* 1.Input *\n");
printf("\t\t\t* 2.Output *\n");
printf("\t\t\t* 3.Sort *\n");
printf("\t\t\t* 4.Insert *\n");
printf("\t\t\t* 5.Delete *\n");
printf("\t\t\t* 6.Modify *\n");
printf("\t\t\t* 7.Quit *\n");
printf("\t\t\t*********************************\n");
int choice;
scanf("%d",&choice);
return choice;
}
int size(struct student *head){
struct student *p = head;
int count = 0;
while(p!=NULL){
++count;
p = p->next;
}
return count;
}
void swap(struct student *head, int lIndex, int rIndex){
struct student *lNode = get(lIndex, head);
struct student *rNode = get(rIndex, head);
struct student *tmp = (struct student *)malloc(SIZE);
strcpy(tmp->name, lNode->name);
strcpy(tmp->no, lNode->no);
tmp->score = lNode->score;
strcpy(lNode->name, rNode->name);
strcpy(lNode->no, rNode->no);
lNode->score = rNode->score;
strcpy(rNode->name, tmp->name);
strcpy(rNode->no, tmp->no);
rNode->score = tmp->score;
}
void sort(struct student *head){
for(int i = 0; i < size(head); i++){
for(int j = 0; j < size(head) - i - 1; j++){
// if((head+j)->score-(head+j+1)->score>0){
if(get(j, head)->score-get(j+1, head)->score>0){
swap(head, j, j+1);
}
}
}
}
int main(void)
{
struct student *head;
int choice;
while(1){
switch(choice=menu()){
case 1:
head = create();
break;
case 2:
output(head);
break;
case 3:
sort(head);
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
return 0;
break;
default:
printf("invilid input!\n");
break;
}
}
return 0;
}
// scanf("%s%s%f", &p1->name, &p1->no,&p1->score);
scanf("%s%s%f", p1->name, p1->no,&p1->score);