链表错误 [英] Linked list error

查看:87
本文介绍了链表错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的人们,
我已经被这个代码困住了好几个星期,它有很多错误...我尝试调试并解决其中的每一个问题...但是如何?请帮助窥视.


Dear people,
I have been stuck with this code for weeks, it has many errors...i tried debugging and solving each and one of them...but how? please help peeps.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node          //Structure
{
int code [100];
char name [200];
float amt [999.99];
int share [10];
float price;

node *next;

}NODE;

NODE *search (NODE *pList)
{

    NODE int;
    NODE* pCur=NULL;
    printf ("\nEnter the code that you wanna search :");
    scanf ("%s",&int.code);

    for (pCur=pList; pCur!=NULL; pCur=pCur->next)
    {
        if (strcmp(pCur->code,int.code)==0) {// compare pCur->code and int.code
        printf ("\nName is %s\n\n",pCur->name);
        break;
        }
    }
    if (pCur==NULL)
        printf ("Code not found!");

    return pList;
}

NODE* modify (NODE *pList)
{

    NODE int;
    NODE word;
    NODE float;
    NODE *pPre = NULL;
    NODE *pCur = NULL;

    printf ("\nPlease insert the code that you want to modify  : ");
    scanf ("%s",&int.code);

    pCur=pList;

        if (strcmp(pCur->code,int.code)==0 && pPre== NULL) // compare pCur->code and int.code
    {
        pList=pCur->next; //delete first node
        free (pCur); //free() is opposite of malloc. which means the block of memory in the heap is realeased.
        printf ("\nInsert your code : ");
        scanf ("%s",&int.code);

        printf ("Insert your name : ");
        scanf ("%s",&word.name);

        printf ("Insert your amount invested (999.99) : ");
        scanf ("%s",&float.amt);

        printf ("Insert your shares held : ");
        scanf ("%s",&int.share);

        printf ("Insert your current price : ");
        scanf ("%s",&float.price);



        NODE *pNew = (NODE*)malloc(sizeof(NODE)); // allocates memory in the heap for pNew.
        *pNew=int;


        pNew->next = pList;
        pList = pNew;

    }

    else
    {
        for (pCur=pList;pCur!=NULL;pPre=pCur,pCur=pCur->next){
            if (strcmp(pCur->code,int.code)==0){
        pPre->next = pCur->next; // delete other node
        free (pCur);
        printf ("DELETED\n");
        break;
            }}}

    FILE *fp;

    fp=fopen("source.txt","w");

    for (pCur=pList;pCur!=NULL;pCur=pCur->next)
    {
        fprintf (fp,"%s\t\t\t%s\t\t\t%.2f\t\t\t%d\t\t\t%.2f\n",pCur->code,pCur->name,pCur->amt,pCur->share,pCur->price);
    }

    fclose(fp);

    return pList;
}

NODE *add (NODE *pList)
{


    NODE int;
    NODE word;
    NODE float;

    printf ("\nInsert your code : ");
    scanf ("%s",&int.code);

    printf ("Insert your name : ");
    scanf ("%s",&word.name);

    printf ("Insert your amount invested (999.99) : ");
    scanf ("%s",&float.amt);

    printf ("Insert your shares held : ");
    scanf ("%s",&int.share);

    printf ("Insert your current price : ");
    scanf ("%s",&float.price);



    NODE *pNew = (NODE*)malloc(sizeof(NODE)); // allocates memory in the heap for pNew.
    *pNew=int;


    pNew->next = pList;
    pList = pNew;

    FILE *fp;

    fp=fopen("source.txt","a");

    fprintf (fp,"%d\t\t\t%s\t\t\t%.2f\t\t\t%d\t\t\t%.2f\n",int.code, word.name,float.amt,int.share,float.price);
    fclose(fp);

    return pList;
}

void *display (NODE* pList)
{

    for (NODE* pCur=pList;pCur!=NULL;pCur=pCur->next)

    printf ("%d\t\t\t%s\t\t\t%.2f\t\t\t%d\t\t\t%.2f\n",pCur->code,pCur->name,pCur->amt,pCur->share,pCur->price);

    return pList;
}


int main (void)
{
    int choice;
    NODE int;
    NODE word;
    NODE float;
    NODE *pList = NULL;


    FILE *fp;

    fp=fopen("source.txt","r");

    while (fscanf (fp,"%d %s %.2f %d %.2f",&int.code, &word.name, &float.amt, &int.share, &float.price)!=EOF)// copies all the data from the file to the link list.
    {

        NODE* pNew = (NODE*)malloc(sizeof(NODE)); // allocates memory in the heap for pNew.
        *pNew=int;

        pNew->next=pList;
        pList=pNew;

    }

    fclose (fp);

    printf ("Choice :\n______\n1.Display all the ints\n");
    printf("2. Add new fund:\n");
    printf("3. Modify a data\n");
    printf("4. Search the data\n");
    printf("5. To EXIT\n");
    printf ("Please select your choice : ");
    while (scanf ("%d",&choice)!=EOF) //if control^c is keyed in.Programs exit. This is a loop scanf
    {

    switch (choice)
    {

    case 1 : display (pList); // calls the display function
        break ;

    case 2 :pList = add (pList); // calls the add function
        break;

    case 3 :pList = modify (pList); // calls the modify function
        break ;

    case 4 : search (pList); // calls the search function
        break;

    case 5 :
            exit(100);
        break;


    }
    printf ("\n\nPlease select one of your choice :");
    }
    system ("pause");
    return 0;

}





推荐答案

您已将问题标记为C ++,但您的代码似乎是C,而不是C ++.应该是哪个?

You have tagged your question as C++, but your code appears to be C, not C++. Which should it be?

Lawrence8852写道:
Lawrence8852 wrote:

它有很多错误



你是对的.您应该重新阅读正在学习的书的前几章.



You are correct in this. You should reread the first few chapters of the book you are learning from.

Lawrence8852写道:
Lawrence8852 wrote:

我尝试调试



您发布的代码将无法编译.您必须先经过编译器和链接器,然后才能使用调试器进行认真的调试.如果尝试对其进行编译,则编译器应产生错误消息.您需要查看这些消息以及它们所引用的代码中的位置,并逐一解决它们. (注意:错误消息有时是早期错误的结果,并且在纠正该错误后将消失.)

我还没有讲完所有这些内容,但会指出几个最明显的问题.



The code you posted will not compile. You have to get past the compiler and linker before you can get into serious debugging with the debugger. If you attempt to compile it, the compiler should produce error messages. You need to look at these messages and the locations in the code they refer to and resolve them - one by one. (Note: sometimes error messages are the result of an earlier error and will go away when that error is fixed.)

I haven''t gone through all of this, but will point out a couple of the most blatant issues.

typedef struct node          //Structure
{
    int code [100];      // An array of 100 ints? Is this what you want?
    char name [200];
    float amt [999.99];  // An array of how many floats? 
                          // You can't have 0.99 of an array element.
    int share [10];
    float price;
    node *next;          // Fine in C++, in C needs to be struct node *

} NODE;







NODE int;        // you can't name a variable int, int is a reserved word
NODE float;      // you can't name a variable float, float is a reserved word



另外,您需要重新阅读有关scanf的文档.您正在尝试错误地使用它.

努力解决基本问题-备有教科书和提供了编译器文档,并在需要时可以参考它们.只需一一解决您的编译器错误,它们就会成堆消失.



Also, you need to reread the documentation on scanf. You are trying to use it incorrectly.

Have a go at fixing your basic problems - do have your textbook & compiler documentation available and refer to them at need. Just tackle your compiler errors one by one and they will start disappearing in bunches.


感谢Avi Berger.非常感谢您的帮助=)
Thanks Avi Berger. Really appreciate ur help =)


这篇关于链表错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆