Thursday, 4 August 2016

Linked list

QUESTION:
finding the n'th element in the singly linked list from reverse without finding the size of the list

Solution:

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *new_node,*temp,*start=NULL;
int size,i;
scanf("%d",&size);
for(i=0;i<size;i++)
{
new_node=(struct node*)malloc(sizeof(struct node));
scanf("%d",&new_node->data);
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
temp=new_node;
}
else
{
temp->next=new_node;
temp=new_node;
}
}
new_node=start;
temp=start;
scanf("%i",&size);
size--;
while(size--)
{
new_node=new_node->next;
}
while(new_node->next!=NULL)
{
temp=temp->next;
new_node=new_node->next;
}
printf("%i",temp->data);
return 0;
}

INPUT:
10
1 2 3 4 5 6 7 8 9 10
4
OUTPUT:
7

ideone.com link to run the code:http://ideone.com/P8W7oN

No comments:

Post a Comment

Troubleshooting in SQL

1. SSRS Error  Round () need to be done in  SQL itself  2. Default Date In SSRS  Start Date =CDate(Format(DateAdd("d",-50,Now()), ...