탑승 시험(01회)

2815 단어

코드

#include 
#include 
#include 

typedef struct stuInfo {
  char stuName[10]; /*    */
  int Age;          /*  */
} ElemType;

typedef struct node {
  ElemType data;
  struct node *next;
} ListNode, *ListPtr;

ListPtr List_Init();
void List_Insert(ListPtr h, ElemType x, char ch);
void List_Destroy(ListPtr h);
ListPtr List_Merge(ListPtr La, ListPtr Lb);
void OutPut(ListPtr La);

ListPtr List_Init() {
  ListPtr p;
  p = (ListPtr)malloc(sizeof(ListNode));
  if (p == NULL) {
    exit(0);
  };
  p->next = NULL;
  return p;
}

void List_Destroy(ListPtr h) {
  ListPtr cur = h->next;
  for (; cur != NULL; h = cur, cur = cur->next) {
    free(h);
  }
  free(h);
  h = NULL;
}

void List_Insert(ListPtr h, ElemType x, char ch) {//ch ''   
  ListPtr newNode, pre, cur;

  newNode = (ListPtr)malloc(sizeof(ListNode));
  if (newNode == NULL) {
    exit(0);
    ;
  }
  newNode->data.Age = x.Age;
  strcpy(newNode->data.stuName, x.stuName);
  pre = h;
  cur = h->next;

  if (ch == '>') {
    for (; cur != NULL && newNode->data.Age < cur->data.Age;
         pre = cur, cur = cur->next)
      ;
  } else if (ch == 'data.Age > cur->data.Age;
         pre = cur, cur = cur->next)
      ;
  }
  else{
    return;
  }

  if (cur == NULL) {
    pre->next = newNode;
    newNode->next = cur;
    return;
  }
  if (newNode->data.Age == cur->data.Age) {//        
    if (strcmp(newNode->data.stuName, cur->data.stuName) == 0) {
      return;
    }
  }
  pre->next = newNode;
  newNode->next = cur;
}

ListPtr List_Merge(ListPtr La, ListPtr Lb) {
  ListPtr Lc, p;
  Lc = List_Init();
  for (p = La->next; p != NULL; p = p->next) {
    List_Insert(Lc, p->data, '>');
  }
  for (p = Lb->next; p != NULL; p = p->next) {
    List_Insert(Lc, p->data, '>');
  }
  return Lc;
}

void OutPut(ListPtr La) {
  ListPtr p;
  p = La->next;
  printf("Name\tAge
"); while (p != NULL) { printf("%s\t%d
", p->data.stuName, p->data.Age); p = p->next; } printf("
"); } int main(void) { ListPtr pHeadA, pHeadB, pHeadC; FILE *inFile; ElemType stu; pHeadA = List_Init(); pHeadB = List_Init(); inFile = fopen("StuInfoA.txt", "r"); if (inFile == NULL) { return 1; } while (!feof(inFile)) { fscanf(inFile, "%s\t%d", stu.stuName, &stu.Age); List_Insert(pHeadA, stu, '

텍스트

  • StuInfoA.txt
  • fghfg  45
    bhkjh  98
    dsfjgh  12
    dfg  5
    dfgrdg 77
    fdgsdf 9
    
  • StuInfoB.txt
  • ftujh 87
    wouero 9
    dfg  5
    wtwu  8
    vbvxcv 66
    dsfghk  18
    

    좋은 웹페이지 즐겨찾기