图书管理系统的单链表存储(链式存储)

题目:图书管理系统的单链表存储(要求必须采用链式存储)

[问题描述]

从book.txt中读入图书数据,已结构体Book作为一行图书记录的数据存储结构,实现带头结点的图书信息单链表的建立、求长度,取元素、修改元素、插入、删除等单链表的基本操作。(程序参考I/O框架已给出)

[基本要求]

(1)依次book.txt文件读入数据,建立带头结点的单链表;

    (2)输出单链表中的数据元素

(3)求单链表的长度;

(4)根据指定条件能够取出元素和修改元素;(取出和修改位置内容任选)

(5)实现在指定位置插入和删除元素的功能。(插入和删除位置和内容任选)

[测试数据]   book.txt

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;

struct Book
{
	string id;
	string name;
	double price;
};
typedef struct LinkNode
{
	Book data;
	struct LinkNode* next;
}LinkNode,*LinkList;

string head_1, head_2, head_3;
int length=0;

Status InitList_L(LinkList& L)
{
	L = new LinkNode;
	L->next = NULL;
	return OK;
		
}

Status CreateList(LinkList& L)
{
	LinkList p,q;
	L = new LinkNode;
	L->next = NULL;
	length = 0;
	fstream file;
	file.open("book.txt");
	if (!file)
	{
		cout << "未找到相关文件,无法打开!" << endl;
		exit(ERROR);
	}
	file >> head_1 >> head_2 >> head_3;
	q = L;
	while (!file.eof())
	{
		p = new LinkNode;
		file >> p->data.id >> p->data.name >> p->data.price;
		q->next = p;
		q = p;
		length++;
	}
	q->next = NULL;
	file.close();
	return OK;
}

LinkNode* GetElem(LinkList L, int i)
{
	LinkList p = L->next; 
	int j = 1;
	while (p && j < i)
	{
		p = p->next;
		++j;
	}
	if (!p || j > i)
		return ERROR;
		cout<< left<<setw(15)<<( * p).data.id<<"\t" <<left<<setw(50)<< (*p).data.name <<left << (*p).data.price << endl; 

}

LinkNode* SetElem(LinkList L, int i,Book e)
{
	LinkList p = L->next; 
	int j = 1;
	while (p && j < i)
	{
		p = p->next;
		++j;
	}
	if (!p || j > i)
		return ERROR;
		(*p).data.id=e.id;
		(*p).data.name=e.name;
		(*p).data.price=e.price;
		cout<< left<<setw(15)<<( * p).data.id<<"\t" <<left<<setw(50)<< (*p).data.name <<left << (*p).data.price << endl; 

}

Status ListInsert(LinkList& L, int i, Book e)
{
	LinkList p = L; 
	int j = 0;
	while (p && (j < i - 1))
	{
		p = p->next;
		++j;
	}
	if (!p || j > i - 1)
		return ERROR;
	LinkList s = new LinkNode;
	s->data = e;
	s->next = p->next;
	p->next = s;
	length++;
	return OK;
	



}

Status ListDelete(LinkList& L, int i)
{
	LinkList p = L;
	int j = 0;
	while ((p->next) && (j < i - 1))
	{
		p = p->next;
		++j;
	}
	if (!(p->next) || (j > i - 1))
		return ERROR;
	LinkList q = p->next;
	p->next = q->next;
	delete q;
	length--;
	return OK;
}

void OutputLinkList(LinkList& L)
{
	LinkList* current = &L;
	while (L!=NULL)
	{
		cout<< left<<setw(15)<<( * current)->data.id<<"\t" <<left<<setw(50)<< (*current)->data.name <<left << (*current)->data.price << endl;  
		
		
		*current = (*current)->next;
	}

}

int ListLength(LinkList& L)
{
	return length;
}

int main()
 {
	     int a, choose;
	     Book e;
	     LinkList L, p;
		 InitList_L(L);
	     cout << "1. 建立\n";
	    cout << "2. 输入\n";
	     cout << "3. 插入\n";
	     cout << "4. 删除\n";
	     cout << "5. 输出\n";
	     cout<<"6.链表长度\n" ;
	     cout<<"7.取出元素\n";
	     cout<<"8.修改元素\n";
	     cout << "0. 退出\n\n";
		int flag=1;
		     choose = -1;
	     while (choose != 0)
		         {
	             cout << "请选择:";
	             cin >> choose;
		             switch (choose)
		                 {
		                 case 1: //建立一个链表
			                     {
								 if (InitList_L(L))
				                         cout << "成功建立链表!\n\n";
			                     break;
			                 	}
		                 case 2: //使用前/后插法创建链表
			                    {
								  CreateList(L);
			                    cout << "输入 book.txt 信息完毕\n\n";
			                     break;
			                 }
		                 case 3: //链表的插入
			                     {
								 cout <<
				                    "请输入两个数分别代表插入的位置和数值(书的信息:编号&书名&价格):";
			                     cin >> a;
			                    cin >> e.id >> e.name >> e.price;
			                    if (ListInsert(L, a, e))
				                         cout << "插入成功.\n\n";
			                     else
				                         cout << "插入失败!\n\n";
			                    break;
			                }
		                case 4: //链表的删除
		                {
						
			                     cout << "请输入所要删除的书籍的位置:";
			                    cin >> a;
			                    if (ListDelete(L, a))
		                        cout << "删除成功!\n\n";
			                    else
		                   cout << "删除失败!\n\n";
			                   break;
			               }
		             case 5: //链表的输出
		             {
					 
		                    cout << "当前图书系统信息读出:\n";
			                   p = L->next;
			                   if (p)
				                       OutputLinkList(p);
			                    else
			                    cout << "没有图书信息!\n\n";
			                    break;
			     	}
			        case 6://链表长度
							{
							cout<<"链表的长度\n";
							int len=ListLength(L);
							printf("%d\n",len); 
							break;}
					case 7://取出元素
					        {cout<<"请输入要取出的元素位置:";
							int i;
							cin>>i;
							GetElem( L,  i) ;
							break;}
					case 8://修改元素
					{
					
							cout<<"请输入要修改元素的位置和数据:";
							int i;Book e;
							cin>>i;
							 cin>>e.id>>e.name>>e.price;
							 SetElem(L,i,e);
						}
				
					 
		     }
		     
		 }
	     return 0;
	 }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值