#include<stdlib.h>
#include <stdio.h>
typedef struct node{
int ClassID;
int StuID;
int Grade;
struct node *next;
}StudentLinkedListNode;
/* 列印單個節點 */
void printLinkedListNode(StudentLinkedListNode * node){
printf("{Class:%d, ID:%d, Grade:%d}",node->ClassID, node->StuID, node->Grade);
if(node->next!=NULL){
printf("->");
}else
{
printf("\n");
}
}
/* 輸出該表的成績情況 */
void outputStudentLinkedList(StudentLinkedListNode* head){
StudentLinkedListNode *p;/*臨時指標變數*/
p=head;/*賦值指向頭結點*/
/*回圈遍歷節點直到最后一個節點*/
while(p)
{
printLinkedListNode(p);
p=p->next;
}
}
/** 新建一個鏈表node并回傳 */
StudentLinkedListNode* studentLinkedListCreate( int class_id,int student_id,int grade) {
StudentLinkedListNode *p = NULL;
p = (StudentLinkedListNode*)malloc(sizeof(StudentLinkedListNode));
p->ClassID = class_id;
p->Grade = grade;
p->StuID = student_id;
p->next = NULL;
return p;
}
/** copy一個已有的node */
StudentLinkedListNode* studentLinkedListCopy(StudentLinkedListNode* src)
{
StudentLinkedListNode* tempNode = studentLinkedListCreate(src->ClassID,src->StuID,src->Grade);
return tempNode;
}
/** 按照降序插入學生的成績情況,并回傳鏈表頭指標 */
StudentLinkedListNode* studentLinkedListAdd(StudentLinkedListNode* head, StudentLinkedListNode* node) {
StudentLinkedListNode *p;/*臨時指標變數*/
StudentLinkedListNode *temp=NULL;/*中轉指標變數*/
p=head;/*賦值指向頭結點*/
if(!p) return node;//當表為空時
if(p->Grade<node->Grade)/*插在表頭*/
{
node->next=head;
return node;
}
while(p&&node->Grade<p->Grade)/*尋找目標節點*/
{
temp=p;
p=p->next;
}
temp->next=node;
node->next=p;
return head;
}
/** 根據學號搜索某個學生的成績情況 如果沒有,請printf("this class has no such student as id=%d",id); */
void searchByID(StudentLinkedListNode*head ,int id){
StudentLinkedListNode *p;
p=head;
int find=0;
while(p)
{
if(p->StuID==id)
{
printLinkedListNode(p);
find=1;
break;
}
p=p->next;
}
if(!find)
{
printf("this class has no such student as id=%d",id);
}
}
/** 根據學號洗掉某個學生的成績情況 如果沒有,請printf("this class has no such student as id=%d",id);*/
StudentLinkedListNode* deleteByID(StudentLinkedListNode* head,int id){
StudentLinkedListNode *p = head;
StudentLinkedListNode *q=NULL;
int find=0;
while(p)
{
if(id==p->StuID)
{
if(!q)/*考慮洗掉頭結點的情況*/
{
q=p;
p = p->next;
free(q);
find=1;
printf("Delete successfully\n");
return p;
}
else/*找到指定學號的學生資訊后洗掉之*/
{
q->next = p->next;
free(p);
find=1;
printf("Delete successfully\n");
return head;
}
}
q=p;
p=p->next;/*若在查的學生學號與指定學號不同,則右移指標p*/
}
if(!find)/*如果沒有,printf("this class has no such student as id=%d",id);*/
{
printf("this class has no such student as id=%d",id);
}
}
/** 合并兩個班級的成績情況 */
StudentLinkedListNode* mergeLinkedList(StudentLinkedListNode* heads[]){
StudentLinkedListNode *p1,*p2,*q1,*q2;
p1=p2=heads[0];
q1=q2=heads[1];
do
{
/*尋找目標節點*/
while(((q1->Grade)<(p1->Grade))&&(p1->next!=NULL))
{
p2=studentLinkedListCopy(p1);
p1=studentLinkedListCopy(p1->next);
}
if((q1->Grade)>=(p1->Grade))
{
if(heads[0]==p1)/*插在表頭*/
{
q1->next=studentLinkedListCopy(heads[0]);
p1=studentLinkedListCopy(q1->next);
p2=studentLinkedListCopy(q1);
heads[0]=studentLinkedListCopy(q1);
q2=studentLinkedListCopy(q2->next);
q1=studentLinkedListCopy(q2);
heads[1]=studentLinkedListCopy(q1);
}
else
{
p2->next=studentLinkedListCopy(q1);
q1=studentLinkedListCopy(q1->next);
q2->next=studentLinkedListCopy(p1);
p2=studentLinkedListCopy(q2);
q2=studentLinkedListCopy(q1);
}/*插在表中*/
}
}while((p1->next!=NULL)||((p1==NULL)&&(q1!=NULL)));
if((q1!=NULL)&&((q1->Grade)<(p1->Grade))&&(p1->next==NULL))
{
p1->next=studentLinkedListCopy(q1);
}/*考慮剩余元素*/
return heads[0];
//tips:因為傳入的都是指標,為了防止拆分程序中影響了原來總鏈表的存盤,
//在鏈表node賦值的時候可以用上面提供的studentLinkedListCopy函式
//如 node->next = temp_node 改為 node->next = studentLinkedListCopy(temp_node)
//TODO
}
StudentLinkedListNode* reverseLinkedList(StudentLinkedListNode*head){
//TODO
}
int main(){
StudentLinkedListNode* Class[2]={NULL};
StudentLinkedListNode* All=NULL;
StudentLinkedListNode* reAll=NULL;
printf("1.insert 2.search by StuID 3.delete by StuID 4.merge 5.reverse 6.output \n");
int op;
int i;
int tempClass;
int tempStuId;
int tempGrade;
StudentLinkedListNode *tempNode;
while (scanf("%d", &op) != EOF) {
switch (op) {
case 1:
printf("How many rows of data do you need to input?\n");
scanf("%d",&i);
printf("input the i row data format as:class_id,student_id,grade\n");
while (i!=0){
scanf("%d,%d,%d", &tempClass,&tempStuId, &tempGrade);
tempNode = studentLinkedListCreate(tempClass, tempStuId, tempGrade);
Class[tempClass]=studentLinkedListAdd(Class[tempClass], tempNode);
i--;
}
break;
case 2:
printf("input thedata format as: ClassID, StuID\n");
scanf("%d,%d",&tempClass,&tempStuId);
searchByID(Class[tempClass], tempStuId);
break;
case 3:
printf("input the data format as: ClassID, StuID\n");
scanf("%d,%d",&tempClass,&tempStuId);
Class[tempClass]=deleteByID(Class[tempClass], tempStuId);
break;
case 4:
All=mergeLinkedList(Class);
outputStudentLinkedList(All);
break;
case 5:
reAll=reverseLinkedList(All);
outputStudentLinkedList(reAll);
break;
case 6:
printf("Class 0:\n");
outputStudentLinkedList(Class[0]);
printf("\nClass 1:\n");
outputStudentLinkedList(Class[1]);
break;
}
printf("\n###################################\n");
printf("1.insert 2.search by studentID 3.delete by studentID 4.merge 5. reverse 6.output \n");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/91638.html
標籤:C語言
