Question description
Dr.Jegan is faculty, who handling data structure course for software engineering department second year students.
one day this faculty was handling very interesting topic in data structure such that Linked List, he has given the following explanation for Linked list concept.
"Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
LinkedList − A Linked List contains the connection link to the first link called First."
During this lecture time, last bench students was asking surprise test for Linked list concept.
So the faculty decided to conduct test on the topic of Linked List.
the question was given to last bench students that is,
The nodes are deleted D times from the beginning of the given linked list.
For example if the given Linked List is 5->10->15->20->25 and remove 2 nodes,
then the Linked List becomes 15->20->2
5.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *start=NULL;
void create(){
int n,i,x;
struct node *p,*t;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&x);
p=malloc(sizeof(struct node));
p->data=x; p->next=NULL;
if(!start) start=t=p;
else t=t->next=p;
}
}
int main(){
int d;
struct node *p;
create();
scanf("%d",&d);
while(d-- && start){
p=start;
start=start->next;
free(p);
}
printf("Linked List:");
for(p=start;p;p=p->next)
printf("->%d",p->data);
}
0 Comments