考研复试上机准备

1、图书类数组的定义

typedef struct {string bookId;string bookName;double price;
} bookNode;typedef struct {bookNode* elem;int length;
}SqList;// 初始化
int InitList(SqList& L) {L.elem = new bookNode[MAX];L.length = 0;return 1;
}

2、图书类数组插入排序

void SortList(SqList& L) {double temp; // 暂存价格(比较对象)bookNode tempbook; // 暂存置换数据元素int i;int j;for (i = 1; i < L.length; i++) { // 第一个元素已经排好序,无需再排序temp = L.elem[i].price;tempbook = L.elem[i];for (j = i - 1; j >= 0 && L.elem[j].price < temp; j--)L.elem[j + 1] = L.elem[j];L.elem[j + 1] = tempbook;}
}

3、链式存储图书链表

typedef struct {string bookId;string bookName;double price;
}book;typedef struct node{book data;int length;node* next;
}node, *nodeList;// 初始化链表
int InitList(nodeList& L) {L = (node*)malloc(sizeof(node));if (!L) return 0;L->next = NULL;L->length = 0;return 1;
}

4、冒泡排序链表

void SortList(nodeList& L) {node* p = new node;int length = L->length;for (int i = 0; i < length - 1; i++) {p = L->next;int j = 0;while (p && j < length - 1 - i) {if (p->data.price < p->next->data.price) {book x = p->data;p->data = p->next->data;p->next->data = x;}p = p->next;j++;}}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部