Thread: [Dev-C++] A double chain list problem about delete the special node
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <per...@ya...> - 2004-06-03 01:14:52
|
I have write a double chain list program that do only some simple operation.But the result of the function DeleteList() is not natural.It will delete all the node before the ith node.I haven't found the reason of the problem by far.Can someone can me why and how to improve it?
---Thanks,
Bruce.J
My program is list belove:
/* ************************************************************ */
#include <stdio.h>
enum bool {FALSE, TRUE};
typedef enum bool BOOL;
typedef int DATA;
typedef struct DoubleLink {
DATA data;
struct DoubleLink *prev;
struct DoubleLink *next;
} DoubleLink;
/* Input the value of n entries element to create a double chain list */
DoubleLink *CreateList(DoubleLink *p, int n)
{
DoubleLink *ptr;
int i;
p = (DoubleLink *) malloc(sizeof(DoubleLink));
/* First create a empty double chain list with a head pointer */
p->next = NULL;
p->prev = NULL;
for (i = 0; i < n; ++i) {
ptr = (DoubleLink *) malloc(sizeof(DoubleLink));
scanf("%d", &ptr->data);
ptr->prev = p;
ptr->next = p->next;
p->next = ptr;
}
return p;
}
/* Get the length of the double chain list */
int GetLength(DoubleLink *p)
{
DoubleLink *p1;
int i = 0;
p1 = p->next;
while (p1) {
++i;
p1 = p1->next;
}
return i;
}
/* Delete the ith element */
/* It has a problem that it will delete the all element which before the ith element when I run it,but I don't know why.*/
BOOL DeleteList(DoubleLink *p, int i)
{
if (i<1 || i>GetLength(p))
return FALSE;
DoubleLink *p1;
int j = 0;
p1 = p;
while(p1 && j<i) {
p1 = p1->next;
++j;
}
if (p1->next == NULL)
free(p1); /* If it is a last node,direct to free the memory */
else {
p1->prev->next = p1->next;
p1->next->prev = p1->prev;
free(p1);
}
return TRUE;
}
/* Printf the double chain list*/
void OutputList(DoubleLink *p)
{
DoubleLink *p1;
p1 = p;
while (p1 = p1->next)
printf("%d,", p1->data);
printf("\n");
}
/* free the memory of double chain list If not to use it latter */
void DestoryList(DoubleLink *p)
{
while (p) {
free(p);
p = p->next;
}
}
main()
{
DoubleLink *ptr;
int n, i;
printf("Please input the number of data you want to create:\n");
scanf("%d", &n);
ptr = CreateList(ptr, n);
OutputList(ptr);
printf("Please input the ith where you want to delete:\n");
scanf("%d", &i);
if (DeleteList(ptr, i)) {
printf("Delete success!\n");
OutputList(ptr);
} else
printf("Delete failed!\n");
DestoryList(ptr);
system("PAUSE");
}
---------------------------------
Do You Yahoo!?
嫌邮箱太小?雅虎电邮自助扩容! |
|
From: Per W. <pw...@ia...> - 2004-06-03 05:25:06
|
I haven't had the time to check all code, but there are a number of errors here. For some of them, see below: /Per W On Thu, 3 Jun 2004, [gb2312] Bruce jjs wrote: > > I have write a double chain list program that do only some simple ope= ration.But the result of the function DeleteList() is not natural.It will d= elete all the node before the ith node.I haven't found the reason of the pr= oblem by far.Can someone can me why and how to improve it? > > ---Thanks, > > Bruce.J > > My program is list belove: > > /* ************************************************************ */ > > #include <stdio.h> > > enum bool {FALSE, TRUE}; > typedef enum bool BOOL; > typedef int DATA; > typedef struct DoubleLink { > DATA data; > struct DoubleLink *prev; > struct DoubleLink *next; > } DoubleLink; > > /* Input the value of n entries element to create a double chain list */ > DoubleLink *CreateList(DoubleLink *p, int n) > { > DoubleLink *ptr; > int i; > ------ What are you using the parameter p for, since you immediately assigns your own value to it? Besides, when you call CreateList() from main(), you send in an undefined pointer. Maybe you thought of sending in a pointer to a pointer? ------ > p =3D (DoubleLink *) malloc(sizeof(DoubleLink)); > > /* First create a empty double chain list with a head pointer */ > p->next =3D NULL; > p->prev =3D NULL; > for (i =3D 0; i < n; ++i) { > ptr =3D (DoubleLink *) malloc(sizeof(DoubleLink)); > scanf("%d", &ptr->data); > ptr->prev =3D p; > ptr->next =3D p->next; > p->next =3D ptr; > } > return p; > } > > /* Get the length of the double chain list */ > int GetLength(DoubleLink *p) > { > DoubleLink *p1; > int i =3D 0; > > p1 =3D p->next; > while (p1) { > ++i; > p1 =3D p1->next; > } > return i; > } > > /* Delete the ith element */ > /* It has a problem that it will delete the all element which before the = ith element when I run it,but I don't know why.*/ > BOOL DeleteList(DoubleLink *p, int i) > { > if (i<1 || i>GetLength(p)) > return FALSE; > > DoubleLink *p1; > int j =3D 0; > > p1 =3D p; > while(p1 && j<i) { ------ Depending on i, this might loop until p1 =3D=3D NULL ------ > p1 =3D p1->next; > ++j; > } ------ And if p1 is NULL, you're not allowed to try p1->next ------ > if (p1->next =3D=3D NULL) ------ Let's say that p1->next is NULL. Then you still have to check if p1->prev is !=3D and in that case set p1->prev->next =3D NULL; since p1->prev will loose it's last entry. ------ > free(p1); /* If it is a last node,direct to free the memory */ > else { > p1->prev->next =3D p1->next; > p1->next->prev =3D p1->prev; > free(p1); > } > return TRUE; > } > > /* Printf the double chain list*/ > void OutputList(DoubleLink *p) > { > DoubleLink *p1; > > > p1 =3D p; > while (p1 =3D p1->next) > printf("%d,", p1->data); > printf("\n"); > } > > /* free the memory of double chain list If not to use it latter */ > void DestoryList(DoubleLink *p) > { > while (p) { ------ You can't first delete p, and then get p->next. You have to take a copy of p->next before calling free(p) ------ > free(p); > p =3D p->next; > } > } > > main() > { > DoubleLink *ptr; > int n, i; > > printf("Please input the number of data you want to create:\n"); > scanf("%d", &n); > ptr =3D CreateList(ptr, n); > OutputList(ptr); > > printf("Please input the ith where you want to delete:\n"); > scanf("%d", &i); > if (DeleteList(ptr, i)) { > printf("Delete success!\n"); > OutputList(ptr); > } else > printf("Delete failed!\n"); > DestoryList(ptr); > system("PAUSE"); > } > > > > > --------------------------------- > Do You Yahoo!? > =CF=D3=D3=CA=CF=E4=CC=AB=D0=A1=A3=BF=D1=C5=BB=A2=B5=E7=D3=CA=D7=D4=D6=FA= =C0=A9=C8=DD=A3=A1 |
|
From: Dmitry S. <si...@si...> - 2004-06-04 02:26:04
|
Hello all! How could I use cvs with devc++ |
|
From: Scott S. <age...@co...> - 2004-06-05 14:06:45
|
On www.flipcode.com under development tutorials, they have a quick start guide to CVS. -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Dmitry Sidorov Sent: Thursday, June 03, 2004 10:26 PM To: dev...@li... Subject: [Dev-C++] CVS Hello all! How could I use cvs with devc++ ------------------------------------------------------- This SF.Net email is sponsored by the new InstallShield X. From Windows to Linux, servers to mobile, InstallShield X is the one installation-authoring solution that does it all. Learn more and evaluate today! http://www.installshield.com/Dev2Dev/0504 _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |