473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help regarding Josepheus problem.

Hi All,

I have taken up this problem to learn a bit in using linked lists.
I am trying a variant of it what it is supposed to so is delete the nth
person
clockwise and counter clockwise alternatively until last man is alive

I tried coding it.
I have completed it.
But i have a problem in my code.
The last man that shud live logically is not the result of my code.
I think i am making a small mistake in my logic cud not find it but.
Can u help.
Pls help soon.
I am attaching the code here.
The code is as follows

/************************************************** **************************

This program creates a circular list of specified number of people and
then
eliminates one by one clockwise and counter clockwise until the last
one
element alone is in the list

************************************************** **************************/


#include<stdio.h>
#include<stdlib.h>
typedef struct ppl_list
{
char name[20];
struct ppl_list *prev;
struct ppl_list *next;
}ppl_list;
struct ppl_list *FirstRec;
struct ppl_list *LastRec;
struct ppl_list *CurrElement, *PrevElement,*temp;
int i;
int pplnum,nthdel;
void insert(ppl_list*);
void display(ppl_list*);
void delete(ppl_list*);
void deleteorder(ppl_list*);
void moveclockwise(ppl_list*);
void moveanticlockwise(ppl_list*);
int main()
{

clrscr();
printf("\nEnter the number of ppl in the list\n");
scanf("%d",&pplnum);
printf("\nEnter the nth person to be removed\n");
scanf("%d",&nthdel);
FirstRec=(ppl_list*)malloc(sizeof(ppl_list));
printf("\nEnter the name of the first person\n");
scanf("%s",FirstRec->name);
FirstRec->next=(ppl_list*) malloc (sizeof(ppl_list));
insert(FirstRec);
display(FirstRec);
deleteorder(FirstRec);
display(FirstRec);

printf("\nThe list was created successfully\n");
getch();
}
void insert(ppl_list *FirstRec)

{

PrevElement=FirstRec;
for(i=1;i<pplnum;i++)
{

LastRec=PrevElement->next;
PrevElement->prev=LastRec;
printf("\nEnter the name of next person\n");
scanf("%s",LastRec->name);
LastRec->prev=PrevElement;
LastRec->next=(ppl_list*) malloc (sizeof(ppl_list));
PrevElement=LastRec;
}

LastRec->next=FirstRec;
}
void display(ppl_list *start)
{

PrevElement=start;
printf("\nThe elements in list are\n");
printf("1st is %s\n", PrevElement->name);
for(i=2;i<pplnum + 1;i++)
{

CurrElement=PrevElement->next;
printf("%dth is %s\n",i,CurrElement->name);
PrevElement=CurrElement;
}
}

void deleteorder(ppl_list* start)
{
while(pplnum >1)
{
CurrElement=start ;
moveclockwise(CurrElement);
delete(CurrElement);
pplnum--;
start=CurrElement->prev;

if(pplnum > 1)
{
CurrElement=start;
moveanticlockwise(CurrElement);
delete(CurrElement);
pplnum--;
start=CurrElement->next;
}
}
}

void delete(ppl_list* start)
{

CurrElement=start;
temp=CurrElement;
if(CurrElement == FirstRec)
{
FirstRec=CurrElement->next;
FirstRec->prev=LastRec;
}
else if(CurrElement == LastRec)
{
LastRec=CurrElement->prev;
LastRec->next=FirstRec;
}
else
{
CurrElement->prev->next=CurrElement->next;
CurrElement->next->prev=CurrElement->prev;
}
free(temp);
}

void moveclockwise(ppl_list* CurrElement)
{

for(i=1;i<=nthdel;i++)
{
CurrElement=CurrElement->next;
}
}

void moveanticlockwise(ppl_list* CurrElement)
{
for(i=1;i<=nthdel;i++)
{
CurrElement=CurrElement->prev;
}

}
Thanks in advance

regards
Rohini.

May 12 '06 #1
3 1854
Hi guys..

I have solved the problem successfully..

I made a simple mistake wen i traced thru i spotted it and rectified..
Now its working fine..

regards
Rohini

May 12 '06 #2
vcp
Rohini Ramamurthy wrote:
Hi guys..

I have solved the problem successfully..

Congratulations !

I quickly had a look, just noticed that you are using pointers after
calling free on it.
See segment of your code below :

void deleteorder(ppl_list* start)
{

/* .................. */
delete(CurrElement);
pplnum--;
start=CurrElement->prev; /* WARNING : CurrElement is
freed. No? */

/* .................. */
}

void delete(ppl_list* start)
{

CurrElement=start;
temp=CurrElement;
/* .................. temp is not changing -------------- */
free(temp);

}

You are passing CurrElement and calling free on it, then using it to
access CurrElement->prev.

May be I missed something.

I made a simple mistake wen i traced thru i spotted it and rectified..
Now its working fine..

regards
Rohini


May 12 '06 #3

"Rohini Ramamurthy" <ro*******@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Hi All,

I have taken up this problem to learn a bit in using linked lists.


That's nice, but do you know that you don't need linked lists to solve the
"Ring of Josephus?"

http://groups.google.com/group/comp....90a5aab1?hl=en
Rod Pemberton
May 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Roy J | last post by:
Hello everyone:) My name is Roy, I am new to Java and I have problem regarding to arrays. if you have time and like to help, please help. I am trying to make a program to deal with prime...
19
by: Thue Tuxen Sørensen | last post by:
Hi everybody ! I´m maintaining a large intranet (approx 10000 concurrent users) running on one IIS box and one DB box with sqlserver 2000. Currently there is 2,5 GB Ram, 1 1400 mhz cpu and 2...
1
by: Likhith Areekkal | last post by:
Hi, My background: Passed Bachelors in Computer Science 2 years back with distinction in Canada. Had to do survival jobs for 2 years as the comp. industry was in bad shape. Serious about...
4
by: trialproduct2004 | last post by:
Hi all i am having application in C#. here what i want it to update one datagrid depending on particular value. I want to start minimum of 5 threads at a time and all these threads are updating...
7
by: Rich Denis | last post by:
Hello, I have been trying to solve a mysterious memory leak problem and was hoping that you could help me out on my stuck point. First a bit of background. We have two app servers in an app...
7
by: therod | last post by:
I am running Windows Server 2003 SP1 and .Net Framework 1.1 I want to upgrade to .Net 2.0 and I'm pretty sure I should uninstall 1.1 first. Problem is that .NET 1.1 doesn't show up in Control...
8
by: Mr. SweatyFinger | last post by:
where have they all gone. alt.suicide? alt.findContol.then.slit.your.own.throat?
5
by: dannynnad | last post by:
Hi, SuperAdministrator when trying to edit the frontend content the following error is comingup: "You are not authorized to view this resource." I checked from the backend whether the...
11
by: tracy | last post by:
Hi, I really need help. I run this script and error message appeal as below: drop trigger log_errors_trig; drop trigger log_errors_trig ERROR at line 1: ORA04080: trigger 'LOG_ERRORS-TRIG'...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.