如何从C链表中用户输入的结构中减去整数值?

问题描述:

下面是我的代码在这里我试图用用户输入值(x1)减去struct node中的itemnum ....但它无法正常工作。







Below is my code here i am trying to subtract itemnum in struct node with user input value (x1) ....but it is not working properly .



struct node                                                   // main structure
{
        int itemcode;
        int itemnum;                             //itemnum
        char name[30];
        struct node *next;
}
struct node *create,*end,*start,*temp,*prev,*temp1,*start1,*end1;
int x,y,x1,num,total;


void orderitem( int x,int x1)         // I am passing item code and itemnum 
{


    if(isempty())                        // function to check head pointer is !null
    {

        printf("Empty, can't order now");
    }
    else
    {

        temp=start;
        while(temp->next!=NULL && temp->itemcode!= x)
        {
            prev=temp;
            temp = temp-> next;
        }

        if(temp->next==NULL&&temp->itemcode!=x)  // searching with x i.e item code

        {
            printf("Element %d is not present in the list\n",x);
            return;
        }

        else if(start->itemcode==x)              //if item code is at first 
        {
            start1->itemnum=x1;

            start->itemnum-=start1->itemnum;

            if(start->itemnum<=0)         //deletes start if itemnumber less than 0
            {         
                start=start->next;
            }
        }
        else if(end->itemcode==x)              //checks the item code with end  pos
        {                                             

            end1->itemnum=x1;
            end->itemnum-=end1->itemnum;
        }
        if(end->itemnum<=0)                       //deletes end 
        {   end=prev;
            end->next=NULL;

        }


        else
        {
                temp1->itemnum=x1;                 //storing user's item number in  
                                                      temp1

                temp->itemnum-=temp1->itemnum;       

                if(temp->itemnum<=0)
                    prev->next=temp->next;

           
        }

    }
}





我尝试了什么:



我尝试过结构变量来保存用户输入并减去它在结构中的项目编号....但它不起作用......我是数据结构的新手,......代码可能不那么好......但如果有人可以告诉我减去过程的程序,我将非常感激。具有用户输入值的项目编号。谢谢



What I have tried:

I have tried structure variable to hold the user input and minus it with item number in struct ....but it didn't work......I am new to data structures,....the code may not be that good....but it would be hugely appreciated if someone can tell me the procedure to minus the item number with user input value. thanks

给自己一个帮助并正确地缩进代码:它几乎是不可读的,并且缩进使得查看正在发生的事情变得容易得多。随着代码在非法麻醉品上像蜘蛛一样左右跳跃,甚至不容易找到orderitem功能开始和结束的地方,更不用说它在此期间的作用! :)



我们无法在你做的相同条件下运行你的代码 - 我们不知道你输入什么数据或者你做什么到底发生了什么。因此,你可以开始开始这里发生了什么?。

幸运的是,你几乎肯定有一个很好的工具来帮助你做到这一点:它被称为调试器(虽然我们不知道你使用的是哪种系统,所以我们可以;不会给你如何使用它的精确说明 - 但谷歌可以,我敢肯定。



在函数的第一行放置一个断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。



对不起,但是我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
Do yourself a favour and indent that code properly: it's pretty much unreadable as it is, and indenting makes it a lot easier to see what is going on. With the code jumping about left and right like a spider on illegal narcotics it's not even easy to find where the "orderitem" function starts and ends, much less what it does in the meantime! :)

We can't run your code under the same conditions you do - we have no idea what data you input or what exactly you expect to happen when you do. So it's going to be up to you to get started on "what is going on here?".
Fortunately, you almost certainly have an excellent tool to help you do this: it's called a Debugger (though we don't know which system you are using so we can;'t give you precise instructions on how to use it - but Google can, I'm sure).

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


查找错误的唯一方法你的代码是使用调试器来看看它到底在做什么。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - *,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
The only way to find what is wrong in your code is to use the debugger to see what it is really doing.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.