C_Datastructure:位置中的数字 [英] C_Datastructure:Number in a position

查看:139
本文介绍了C_Datastructure:位置中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我给定了一个整数序列.序列的长度未知,但是序列以-1结尾.我又给了另一个号码pos.我想在位置pos中输出整数.如果pos>序列输出的长度-1.

我被告知要实现两个功能:
节点loadNum():用于将数字加载到链接列表&的函数.返回指向列表开头的指针
void releaseMem(Node head):每次迭代后释放内存的函数

我写了以下代码:

I''m given a sequence of integers. The length of the sequence is unknown but the sequence ends with a -1. I''m given another number pos . I want to output the integer in position pos. If pos > length of the sequence output -1.

I''m told to implement two functions :
Node loadNum(): Function to load the numbers onto the linked list & Return a pointer to the head of the list
void releaseMem(Node head): Function to release the memory after every iteration

I''ve written following code:

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

struct node{
    int data;
    struct node* next;
};

typedef struct node* Node;

/*    Function to load the numbers onto the linked list
      Return a pointer to the head of the list  */
Node loadNum();

/*    Function to print the number in position pos
      head is guaranteed to be non-NULL */
/*    Function to print the number in position pos
      head is guaranteed to be non-NULL */
void printNum(Node head,int pos)
{
    int i = 1;
    Node temp = head;
    while( i != pos ){
   	 temp = temp->next;
   	 if( temp == NULL){
   		 printf("-1");
   		 return;   	 
   	 }
   	 i++;
    }
    printf("%d",temp->data);
}


/* Function to release the memory after every iteration */
void releaseMem(Node head);

int main()
{
    int i,T;
    int pos;
    Node head;
    scanf("%d",&T);
    for (i = 0; i < T; i++){

   	head = loadNum();
   	scanf("%d",&pos);
   	printNum(head, pos);
        if(i<t-1)>
          printf("\n"); // Will add a new line for after all output
                        // except for last.
   	releaseMem(head);
    }
    return 0;
}
/*    Function to load the numbers onto the linked list
      Return a pointer to the head of the list  */
Node loadNum()
{
   Node temp=NULL,head=NULL;
   int n;
   scanf("%d",&n);
   if(n!=-1)
   {
   		temp=(Node)malloc(sizeof(struct node));
   		head=temp;
   }
   while(n!=-1)
   {
   		temp->data=n;
   		temp->next=(Node)malloc(sizeof(struct node));
   		temp=temp->next;
   		scanf("%d",&n);
   }
   temp=NULL;
   return head;   
}

/* Function to release the memory after every iteration */
void releaseMem(Node head)
{
	Node temp;
	while(head!=NULL)
	{
		temp=head;
		head=head->next;
		free(temp);
	}
}


输入/输出应如下所示:
输入:
2
9 8 5 2 ‐1
4
5 3 1 9 10 ‐1
9

输出:
2
‐1

程序提供了正确的输出,除了少数测试用例显示给定输入的超过时间限制" :-
输入:
3
5 4 1 3 5 ‐1
5
2 3 6 7 ‐1
4
6 3 4 3 8 ‐1
2
我要去哪里了?


input/Output should be like this:
Input:
2
9 8 5 2 ‐1
4
5 3 1 9 10 ‐1
9

output:
2
‐1

Program provides correct output except for few test cases which is showing "time limit exceeded" for given input:-
input:
3
5 4 1 3 5 ‐1
5
2 3 6 7 ‐1
4
6 3 4 3 8 ‐1
2
Where am I going wrong?

推荐答案

不运行您的代码,我不知道.

但幸运的是,您可以使用一个可以告诉您的工具.它被称为调试器,它使您可以精确地跟踪代码在做什么(以及其他事情).因此,请在调试器中运行您的代码,并逐步完成每条指令.在某个时刻,将变得很清楚,它正在某个地方循环,并且循环检查的条件没有改变,或者以循环永远不会结束的方式改变.

当您知道那是哪里时,就可以开始找出原因.
调试是一项技能,值得一学.用这样的简单代码学习它比以后使用复杂代码要容易得多!

因此,请尝试一下!
Without running your code, I have no idea.

But fortunately you have a tool at your disposal which will tell you. It''s called a debugger, and it lets you follow exactly what your code is doing (among other things). So run your code in the debugger and step through each instruction. At some point it will become clear that somewhere it is looping and the conditions that the loop checks for either aren''t changing, or are changing in such a way that the loop can never end.

When you know where that is, you can start to work out why.

Debugging is a skill, and it''s one well worth learning. And it''s much, much easier to learn it with a simple piece of code like this than it is later with complicated code!

So give it a try!


这篇关于C_Datastructure:位置中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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