데이터 구조 - 선형 표 순서 저장 표시

16489 단어 Struct
#include 
#include 
#include  
#define MAXSIZE 1005
#define OK 1
#define ERROR 0
typedef int Status;
using namespace std;
struct Book{
	char no[30];
	char name[30];
	double price;
	bool operator == (const Book &w) const {
		if (!strcmp(no, w.no)) return true;
		return false;
	} 
};
struct List {
	Book* elem; 
	int lenth; 
};
//       
Status initList(List &list) {
	list.elem = new Book[MAXSIZE];
	if (!list.elem) exit(0); //    
	list.lenth = 0;
	return OK; 
} 
//       
Status getElem(List &list, int i, Book &elem) {
	if (i < 1 || i > list.lenth) return ERROR;
	elem = list.elem[i - 1];
	return OK;
} 
//                    0 
int locateElem(List &list, Book &elem) {
	for (int i = 0; i < list.lenth; i++) {
		if (elem == list.elem[i]) return i + 1;
	} 
	return 0;
} 
//    
Status insertElem(List &list, Book &elem) {
	if (list.lenth == MAXSIZE) return ERROR;
	list.elem[list.lenth++] = elem;
	return OK;
} 
//        
Status insertElem(List &list, int i, Book &elem) {
	if (i < 1 || i > list.lenth + 1) return ERROR;
	for (int j = list.lenth - 1; j >= i - 1; j--) {
		list.elem[j + 1] = list.elem[j];
	}
	list.elem[i - 1] = elem;
	list.lenth++;
	return OK;
} 
//         
Status deleteElem(List &list, int i) {
	if (i < 1 || i > list.lenth) return ERROR;
	for (int j = i - 1; j < list.lenth - 1; j++) list.elem[j] = list.elem[j + 1];
	list.lenth--;
	return OK;
} 
void output(Book &elem) {
	printf("no:%s name:%s price: %lf
"
, elem.no, elem.name, elem.price); } List list; int main() { initList(list); // Book books[100]; for (int i = 1; i <= 3; i++) { Book book; scanf("%s%s%lf", book.no, book.name, &book.price); insertElem(list, i, book); } for (int i = 0; i < list.lenth; i++) output(list.elem[i]); return 0; }

좋은 웹페이지 즐겨찾기