Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
#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);
	}
}

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?
Posted
Updated 5-May-15 1:41am
v2

1 solution

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!
 
Share this answer
 
Comments
CPallini 5-May-15 7:41am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900