Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-09-2004, 09:11 PM   #1 (permalink)
if13121
Registered User
 
if13121's Avatar
 
Join Date: Oct 2004
Location: Bandung,Indonesia
Posts: 16
if13121 is on a distinguished road
Unhappy linked list problem

This is my list declaration
Code:
#define Nil NULL
#define info(P) (P)->info
#define next(P) (P)->next
#define First(L)((L).First)


typedef int infotype;
typedef struct tElmtlist *address;
typedef struct tElmtlist
{
	infotype info;
	address next;
}Elmtlist;


typedef struct
{
	address First;
}List;

i have this procedure:

void DeleteLast(List * L,infotype * X)
/*is : list not empty
 fs : last Element  list deleted. X save its value*/
{
	address Last,Prev;
	Last = First(*L);
	Prev = Nil;
	First(*L)=Last;
	while (next(Last) != Nil)
	{	Prev = Last;
		Last = next(Last);
	}
	*X = info(Last);
	next(Prev) = Nil;
}
 why if i changed the procedure become:

void DeleteLast(List * L,infotype * X)
{
	address Last;
	Last = First(*L);
	First(*L)=Last;
	while (next(Last) != Nil)
	{	Last = next(Last);
	}
	*X = info(Last);
	Last = Nil;
}
the procedure not work when i print all list element i still have last element

Last edited by Valmont; 11-10-2004 at 02:03 AM.
if13121 is offline   Reply With Quote
Old 11-10-2004, 09:27 AM   #2 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
This function would not work as you expect to because you are assigning null to a local pointer. In this function, Last points to the last node. If you assign null to this pointer, it will no longer point to the last node, but null. This has nothing to do to where the previous noue, before this one, points to.

This was the point of the Prev variable in the first iteration of the function. When we finally established that we were at the end of the list, we knew what node was pointing to the last node. In the second iteration, we lose that information, and so we cannot break the ties to the last node as we should be able to.

Just because you assign null to a pointer, it does not mean that every other pointer that points to the same location as that pointer pointed to will become null. You essentially did the following:

Code:
int real = 32;

int * pointer1 = real;
int * pointer2 = real;

pointer 1 = null
Would you expect pointer 2 above to now point to null or 32? It indeed points to 32. Now look back on your code. Pointer1 is Last, while next(Prev) is Pointer2.

I hope this explains the issue well enough. Good Luck!

-Ted
__________________
while(1) fork();
ender is offline   Reply With Quote
Old 11-10-2004, 07:39 PM   #3 (permalink)
if13121
Registered User
 
if13121's Avatar
 
Join Date: Oct 2004
Location: Bandung,Indonesia
Posts: 16
if13121 is on a distinguished road
you may run this program and it is work for me
Code:
/*File : flist.h*/

#include <stdlib.h>
#define Nil NULL
#define info(P) (P)->info
#define next(P) (P)->next
#define First(L)((L).First)

typedef int infotype;
typedef struct tElmtlist *address;
typedef struct tElmtlist
{
  infotype info;
  address next;
}Elmtlist;


typedef struct
{
  address First;
}List;

/*prototype*/

void CreateList(List * L);
address Alokasi(infotype X);
void InsFirst(List * L,address P);
void InsVFirst(List * L,infotype X);
void DeleteLast(List * L,  infotype * X);
int NbElmtList(List L);
void PrintList(List L);
void ReadList(List *L);




/*File : flist.c*/
/*body prototype */
#include "flist.h"

void CreateList(List * L)
/*create empty list */
{First(*L) = Nil;}
address Alokasi(infotype X)

{  address P;
  P = (address)malloc(sizeof (Elmtlist));
  /*address kan sudah pointer*/
  if(P != Nil)/*ALokasi berhasil*/
  {
    info(P) = X;
    next(P) = Nil;
  }
    return P;
}
void InsFirst(List * L,address P)
{
  next(P) = First(*L);
  First(*L)= P;
}

void InsVFirst(List * L,infotype X)
{
  address P;
  P = Alokasi(X);
  if (P != Nil)
  {InsFirst(L,P);}
}

void DeleteLast(List * L,infotype * X)
/*is : list not empty*/
/*fs : last Element  list deleted. X save its value*/
{
  address Last,Prev;
  Last = First(*L);
  Prev = Nil;
  First(*L)=Last;
  while (next(Last) != Nil)
  {  Prev = Last;
    Last = next(Last);
  }
  *X = info(Last);
  next(Prev) = Nil;
}


/*===========read and write operation===============*/
int NbElmtList(List L)
/*return  nb of  elemen list*/
{  address P;
  int Nb = 1;
  P  = First(L);
  while(P != Nil){
  P = next(P);
  Nb++;
  }
  return Nb;
}

void ReadList(List * L)
/*insert element list from user input*/
{
  infotype X;  
  printf("input  int to list (using insert first) ended it with  999:\n");
  while(X != 999)
  {  printf("input :");
    scanf("%d",&X);
    if (X != 999)
    {InsVFirst(L,X);}
    else
    {printf("finish input element list\n");}
  }
}

void PrintList(List L)
/*display all element list */
{  address P;
  int i,NbList;
  P = (First(L));
  NbList  = NbElmtList(L);
  for (i = 1;i < NbList;i++)
  {      
    printf("%d\n",info(P));
    if (P == Nil)printf("finish print list\n");
    else P = next(P);
  }
} 

/*File : mlist.c*/
#include "flist.h"
int main()
{  
  List L;
  infotype Z;
  CreateList(&L);
  address P,Q,R;
  ReadList(&L);
  PrintList(L);
  printf("delete last\n");
  DeleteLast(&L,&Z);
  printf("elemen deleted = %d\n",Z);
  PrintList(L);
  return 0;
}

My question was
when i changed function DeleteLast above
become:


C / C++ Code:

void DeleteLast(List * L,infotype * X)
{
address Last;
Last = First(*L);
First(*L)=Last;
while (next(Last) != Nil)
{Last = next(Last);
}
*X = info(Last);
Last = Nil;
}
it is not work Why???

Last edited by Valmont; 11-14-2004 at 03:36 AM.
if13121 is offline   Reply With Quote
Old 11-12-2004, 08:58 AM   #4 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
I'm not sure what you are asking then. In my view, I answered your question the first time: why the procedure/function will not work when you changed it from the first implementation to the your current one. Maybe I am not explaning it well enough. I'm sorry.

-Ted
__________________
while(1) fork();
ender is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
A lil help with linked list Night_Prawler Standard C, C++ 2 08-11-2004 02:02 AM
Linker errors on my linked list... Danish Standard C, C++ 1 03-02-2003 07:59 PM


All times are GMT -8. The time now is 09:06 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting