| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1149 人关注过本帖
标题:链表的打印问题
只看楼主 加入收藏
九笙
Rank: 1
等 级:新手上路
帖 子:48
专家分:5
注 册:2019-10-9
收藏
 问题点数:0 回复次数:0 
链表的打印问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define len 20

typedef struct b_inf{
 int code[len];
 char name[len];
 char type[len];
 int remain_num;
 struct b_inf *next;
}B_INF;

void Mainmenu(int);
void Addbook(B_INF **head);
void Access(char *str);
void Showbook(B_INF *head);
void Searchbook(B_INF *head);
void Deletebook(B_INF **head);
void Borrowbook(B_INF *head);

void Access(char *str)
{
    int i,a=0,b=0;
    while(1)
    {
    for(i=0;i<6;i++)
    {
        if(str[i] >= 'A' && str[i] <= 'Z')
        a++;
        else if(str[i] >= 'a' && str[i] <= 'z')
        b++;
    }
    if(a==0||b==0)
    {
        printf("Wrong input, try again :");
        gets(str);
        break;
    }
    else
    {
        system("cls");
        break;
    }
    system("cls");
    }
}

int main()
{
    B_INF *head=NULL;
    int total,option,flag=1;
    char str[6];
    printf("-----Welcome-----\n");
    printf("Please input your password:");
    gets(str);
    Access(str);
    while(flag)
    {
    do
    {
        printf("\n****** Main Menu ******\n");
        printf("1. Show Book's Imformation\n");
        printf("2. Add A Book\n");
        printf("3. Delete A Book\n");
        printf("4. Search A Book\n");
        printf("5. Borrow A Book\n");
        printf("6. Exit the program\n");
        printf("************************\n");
        printf("Please choose a option:");
        scanf("%d",&option);
    }while(option>6||option<1);
     Mainmenu(option);
    }
}

void Mainmenu(int option)
{
    B_INF *head=NULL;
    int n;
    char bkNum[20];
    switch (option)
    {
    case 1:
        Showbook(head);
        break;
    case 2:
        Addbook(&head);
        break;
    case 3:
        Deletebook(&head);
        break;
    case 4:
        Searchbook(head);
        break;
    case 5:
        Borrowbook(head);
        break;
    case 6:
        exit(1);
    }
}

void Addbook(B_INF **head)
{
    system("cls");
    B_INF *pnew,*current;
    printf("Book's Code: ");
    scanf("%d",&pnew->code);
    printf("Book's Name: ");
    getchar();
    gets(pnew->name);
    printf("Book's Type: ");
    getchar();
    gets(pnew->type);
    printf("Remaining of the new book: ");
    scanf("%d",&pnew->remain_num);
    printf("All compeleted!");
    if(NULL == *head)
   {
        *head = pnew;
        (*head)->next = NULL;
   }
    else
   {
        current = *head;
        while(current)
        {
            if(NULL == current->next)
            {
                current->next = pnew;
                pnew->next = NULL;
            }
            current = current->next;
        }
    }
}

void Deletebook(B_INF **head)
{
    system("cls");
    int bookcode;
    B_INF *current;
    B_INF *previous;
    current=*head;
    previous=NULL;
    printf("Please enter the code of the book to delete:");
    scanf("%d",&bookcode);
    while(current!=NULL&&*current->code!=bookcode)
        {
            previous=current;
            current=current->next;
        }
    if(current==NULL)
    {
        printf("Book not found!");
        return ;
    }
    else
    {
        if(previous==NULL)
        {
            *head=current->next;
        }
        else
        {
            previous->next=current->next;
        }
    }
    printf("\nDelete compeleted!\n");
}

void Searchbook(B_INF *head)
{
    system("cls");
    int bookcode;
    B_INF *current;
    current=head;
    printf("Please enter the code of the book to search:");
    scanf("%d",&bookcode);
    while(current!=NULL&& current->code!=bookcode)
        {
            current=current->next;
        }
    if(current==NULL)
        printf("\nBook %d not found!\n",bookcode);
    else
    {
        printf("\n%d\t%s\t%s\t%d",current->code,current->name,current->type,current->remain_num);
    }
}

void Showbook(B_INF *head)
{
    system("cls");
    B_INF *current;
    current =head;
    printf("**************   Book's Info   ******************");
    while(current!=NULL)
    {
        printf("\n%d\t%s\t%s\t%d",current->code,current->name,current->type,current->remain_num);
        current = current->next;
    }
}

void Borrowbook(B_INF *head)
{
    system("cls");
    int number,bookcode;
    B_INF *p=head;
    printf("Please enter the code of the book you want to borrow:");
    scanf("%d",&bookcode);
    printf("\nPlease enter the number of the book you want to borrow:");
    scanf("%d",&number);
    while(p!=NULL&&*p->code!=bookcode)
        {
            p=p->next;
        }
    p->remain_num=p->remain_num-number;
}
  
代码就这样emmm    但为什么添加完之和输入1什么信息都莫得  求改正
搜索更多相关主题的帖子: void printf current NULL head 
2019-12-22 21:46
快速回复:链表的打印问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038670 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved