The best location of book information table based on chain storage structure

Keywords: iOS Database Programming

The best location of book information table based on chain storage structure

describe

 

Define a linked list containing book information (book number, book name, price), read in the corresponding book data to complete the creation of the book information table, and then search for the books in the location according to the designated number of the best location, and output the corresponding book information.

input

Total n+m+2 lines. First, input line n+1, where the first line is the number of books n, and the next N lines are the information (book number, book name, price) of n books. Each book information takes up one line. The book number, book name, price are separated by spaces, and there is no space after the price. The book number and title are of string type, and the price is of floating-point type. Then enter m+1 line, where the first line is an integer m, representing m times of searching, and the last m line is an integer, representing the location sequence number of the book to be searched.

output

Output line m if the search is successful: the output content is the information (book number, book name, price) of a book at the specified location of the i-th query. The book number, book name, and price are separated by spaces, and the price output keeps two decimal places. If the search fails: only the following prompt is output: sorry, the book in the best location does not exist!

Input sample 1

8
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
2
2
0

Output sample 1

9787302164340 Operating-System 50.00
Sorry,the book on the best position doesn't exist!
#include <iostream>
#include<iomanip>
using namespace std;
#define MAXSIZE 10000

typedef struct 
{
	string no;
	string name;
	double price;
}Book;

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

void CreateList_R000(LinkList &L)
{
	LinkList r,p;int i=0;
	L=new LNode;
	L->next=NULL;
	r=L;
	while(1) 
	{
		p=new LNode;
		//cin>>p->data;
		string a,b;
		double c;
		cin>>a>>b>>c;
		if(a=="0"&&b=="0"&&c==0) break;
		p->data.no=a;
		p->data.name=b;
		p->data.price=c; 
		//cin>>p->data;
		p->next=NULL;
		r->next=p;
		r=p;
		i++;
	}
}

void CreateList_R(LinkList &L)
{
	LinkList r,p;int i=0;
	L=new LNode;
	L->next=NULL;
	r=L;
	
	int n;
	cin>>n;
	
	while(1) 
	{
		p=new LNode;
		//cin>>p->data;
		string a,b;
		double c;
		cin>>a>>b>>c;
		//if(a=="0"&&b=="0"&&c==0) break;
		p->data.no=a;
		p->data.name=b;
		p->data.price=c; 
		//cin>>p->data;
		p->next=NULL;
		r->next=p;
		r=p;
		i++;
		if(i==n) break;
	}
	return;
}

void CreateList_H(LinkList &L)
{
	LinkList r,p;int i=0;
	L=new LNode;
	L->next=NULL;
	
	int n;
	cin>>n;
	while(1) 
	{
		p=new LNode;
		//cin>>p->data;
		string a,b;
		double c;
		cin>>a>>b>>c;
		//if(a=="0"&&b=="0"&&c==0) break;
		p->data.no=a;
		p->data.name=b;
		p->data.price=c; 
		//cin>>p->data;
		p->next=L->next;
		L->next=p;
		i++;
		if(i==n) break;
	}
	return;
}


void Show(LinkList &L)
{
	LinkList p=L->next;
	while(p)
	{ 
		cout<<p->data.no<<" "<<p->data.name<<" ";
		cout<<fixed<<setprecision(2)<<p->data.price<<endl;//Save two decimal places
		p=p->next;
	} 
}

int ListLength(LinkList L)
{
    LinkList p;
    p=L->next;  //p points to the first node
    int i=0;             
    while(p){//Traverse single chain table, count node number
           i++;
           p=p->next;   
    } 
    return i;                             
 }

void Sort(LinkList &L)
{
	
    int n=ListLength(L);
    LinkList p;
        for(int i=1;i<=n-1;i++)
        {
            p=L->next;//p points to the primary node 
            int j=0;
            while(p&&j<n-i)// When p is not = NULL and the last is not compared, continue the loop 
            {
                if(p->data.price < p->next->data.price)
                {
                	Book t;
                    t=p->data;
                    p->data=p->next->data;
                    p->next->data=t;//Exchange data fields only 
                }
                p=p->next;//Downward pointing 
                ++j;//Number of compared 
            }
        }
    return;
	
}

void Aver(LinkList &L)
{
	double aver=0,sum=0;int i;int num=0;
	LinkList p;p=L->next;
	while(p){
		sum=sum+p->data.price;
		num++;
		p=p->next;
	}
	aver=sum/num;
	p=L->next;
	while(p)
	{
		if(p->data.price<aver)
			p->data.price=1.2*p->data.price;
		else if(p->data.price>=aver)
			p->data.price=1.1*p->data.price;
		p=p->next;
	}
	cout<<setiosflags(ios::fixed)<<setprecision(2)<<aver<<endl;
}


void mostexpen(LinkList &L)
{
	LinkList p;p=L->next;
	LinkList max=p;LinkList maxs[3];//Using uninitialized pointers will produce unexpected results
	
	while(p)
	{
		if(p->data.price > max->data.price)
		{
			max=p;
		}
		p=p->next;
	}
	
	p=L->next;int k=0;
	
	while(p)	
	{
		if(p->data.price==max->data.price)
		{
			maxs[k]=p;
			k++;
		}
		p=p->next;
	}
	
	cout<<k<<endl;
	int i=0;
	while(maxs[i]&&i<k)
	{
		cout<<maxs[i]->data.no<<" "<<maxs[i]->data.name<<" ";
		cout<<fixed<<setprecision(2)<<maxs[i]->data.price<<endl;
		i++;
	}

}


void favourite(LinkList &L)
{
	int n;int i;int j;string fav[10];	
	LinkList flag[3]; int k=0;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>fav[i];
	}
	
	LinkList p=L->next;
	for(j=0;j<n;j++)
	{
		p=L->next;//Can not leak 
		while(p)
		{
			if(p->data.name==fav[j])
			{
				flag[k]=p;
				k++;
			}			
			p=p->next;
		}
		if(k==0)
		cout<<"Sorry,there is no your favourite!"<<endl;
		else
		{
			cout<<k<<endl;
			for(i=0;i<k;i++)
				cout<<flag[i]->data.no<<" "<<flag[i]->data.name<<" "<<setiosflags(ios::fixed)<<setprecision(2)<<flag[i]->data.price<<endl;
		}
		k=0;//Can not leak 
	}
	
}

void BestLocate(LinkList &L)
{
	int n;int i;int j;int no[10]; int k=0;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>no[i];
	}
	
	LinkList p=L->next;i=0;
	LinkList flag=NULL;
	for(j=0;j<n;j++)
	{
		p=L->next;
		while(p)
		{
			i++;
			if(no[j]==i)
			{
				flag=p;
				k++;
				break;
			}			
			p=p->next;
		}
		if(k!=0)
		{
			cout<<flag->data.no<<" "<<flag->data.name<<" "<<setiosflags(ios::fixed)<<setprecision(2)<<flag->data.price<<endl;
			flag=NULL;	
		}
		if(k==0)
		cout<<"Sorry,the book on the best position doesn't exist!"<<endl;
		k=0;
	}
		
}




int main()
{
	LinkList L;
	CreateList_R(L);
	BestLocate(L); 
	//favourite(L); 
	//CreateList_H(L);
	//mostexpen(L);
	//Sort(L);
	//Aver(L); 
	//Show(L);
	return 0;
}

 

Published 42 original articles, won praise 5, visited 3040
Private letter follow

Posted by aquayle on Sun, 16 Feb 2020 04:07:07 -0800