0
点赞
收藏
分享

微信扫一扫

【数据结构】图书管理系统

kiliwalk 2022-03-13 阅读 54
#include<iostream>
using namespace std;
#define MAXSIZE 100

struct Book{    
	char ISBN[20];
	char name[50];
	int price;
};


typedef struct{   //顺序表 
	Book *elem;
	int length;
}SqList;

typedef struct LNode{   //链表 
	Book data;
	struct LNode *next;
}LNode,*LinkList;

//创建顺序表 
void Create_Sq(SqList &L,int n){
	L.elem=new Book[MAXSIZE];
	L.length=0;
	for(int i=0;i<n;i++){
		cin>>L.elem[i].ISBN>>L.elem[i].name>>L.elem[i].price;   //L.elem[i]数组元素用.引用即可 
		L.length++;
	}
} 
//创建链表(尾插法)
void Create_Link(LinkList &L,int n){
	L=new LNode;
	L->next=NULL;
	LNode *r=L;  //设置尾指针 
	for(int i=1;i<=n;i++){
		LNode *p=new LNode;
		cin>>p->data.ISBN>>p->data.name>>p->data.price;    //指针p用->引用即可 
		p->next=NULL;
		
		r->next=p;
		r=p;
	} 
}

//显示顺序表
void Show_Sq(SqList L){
	int i=0;
	while(i<L.length){
		cout<<L.elem[i].ISBN<<' '<<L.elem[i].name<<' '<<L.elem[i].price<<endl;
		i++;
	}
	cout<<endl;
} 
//显示链表
void Show_Link(LinkList L){
	while(L->next){
		cout<<L->next->data.ISBN<<' '<<L->next->data.name<<' '<<L->next->data.price<<endl;
		L=L->next;
	}
} 

int main(){
	SqList L1;
	LinkList L2;
	Create_Sq(L1,3);
	cout<<"顺序表:"<<endl; 
	Show_Sq(L1);
	Create_Link(L2,3);
	cout<<"链表:"<<endl; 
	Show_Link(L2);
	return 1;
}

运行结果

 

举报

相关推荐

0 条评论