M
Maxx
I have this problem on circular link list which says to move nodes
following t on the list to the node following x on the list. I have
written this program and it also compiled with no errors, but its not
running at all. I have tried a lot to solve this program but can't
seem to make any headway.
here is the program:::
#include <stdio.h>
#include <stdlib.h>
struct node
{
int item;
struct node *next;
};
typedef struct node *link;
link walk(link l,int v) /*to traverse a list pointed by l till node v
*/
{
while(l->next->item!=v)
{
l=l->next;
}
return l;
}
void shift(link l,int x,int t) /*to shift the contents of the list
following t to positon following x on list */
{
int buff[30],i;
l=walk(l,x);
for(i=0; l->next->item!=t; i++)
{
l=l->next;
buff=l->item;
}buff[i++]=9999; /*used to mark the end of the conetents of the
list following x till t*/
for(;l->next!=l;i++)
{
l=l->next;
buff=l->item;
}
l=walk(l,x);
for(i=0;buff!=9999;i++,l=l->next)
{
l->item=buff;
}
for(;l->next!=l;i++,l=l->next)
{
l->item=buff;
}
}
void init(link l,int n) /*to initialize a list with values till n */
{
int i=2;
while(i<=n)
{
l=(l->next=malloc(sizeof *l));
l->item=i;
l->next=l;
i++;
}
}
void traverse(link l)
{
do
{
printf(" %d",l->item);
l=l->next;
}while(l->next!=l);
}
int main(int argc, char **argv)
{
int N=atoi(argv[1]), x=atoi(argv[2]), t=atoi(argv[3]);
link l=malloc(sizeof *l);
l->item=1;
l->next=l;
init(l,N);
shift(l,x,t);
traverse(l);
return 0;
}
Please help, any solutions or hint would be very grateful..
following t on the list to the node following x on the list. I have
written this program and it also compiled with no errors, but its not
running at all. I have tried a lot to solve this program but can't
seem to make any headway.
here is the program:::
#include <stdio.h>
#include <stdlib.h>
struct node
{
int item;
struct node *next;
};
typedef struct node *link;
link walk(link l,int v) /*to traverse a list pointed by l till node v
*/
{
while(l->next->item!=v)
{
l=l->next;
}
return l;
}
void shift(link l,int x,int t) /*to shift the contents of the list
following t to positon following x on list */
{
int buff[30],i;
l=walk(l,x);
for(i=0; l->next->item!=t; i++)
{
l=l->next;
buff=l->item;
}buff[i++]=9999; /*used to mark the end of the conetents of the
list following x till t*/
for(;l->next!=l;i++)
{
l=l->next;
buff=l->item;
}
l=walk(l,x);
for(i=0;buff!=9999;i++,l=l->next)
{
l->item=buff;
}
for(;l->next!=l;i++,l=l->next)
{
l->item=buff;
}
}
void init(link l,int n) /*to initialize a list with values till n */
{
int i=2;
while(i<=n)
{
l=(l->next=malloc(sizeof *l));
l->item=i;
l->next=l;
i++;
}
}
void traverse(link l)
{
do
{
printf(" %d",l->item);
l=l->next;
}while(l->next!=l);
}
int main(int argc, char **argv)
{
int N=atoi(argv[1]), x=atoi(argv[2]), t=atoi(argv[3]);
link l=malloc(sizeof *l);
l->item=1;
l->next=l;
init(l,N);
shift(l,x,t);
traverse(l);
return 0;
}
Please help, any solutions or hint would be very grateful..