Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,460 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,131 people online right now. Registration is fast and FREE... Join Now!




Linked Lists

 
Reply to this topicStart new topic

Linked Lists

olams
22 Jan, 2008 - 04:54 PM
Post #1

New D.I.C Head
*

Joined: 13 Apr, 2007
Posts: 22


My Contributions
Hello,
Please i really need your help.

I am trying writing a code that will delete the last element of the list. However, i keep getting 0 or when i tweak it, i get a message asking me to abort what i am doing.
In this code, i append some values to the list and ask it to do some functions like get first, get last, remove first...these functions work fine. However, i cannot seem to get the remLast right.
thank you very much in advance.
Here is the code:

int LList::remLast()//this does not have parameters.
{
if(size==0){
cout<<"Empty List --- Removal is not";
cout<<"Possible --- Error"<<endl;
return -1;
}
Lnode *ptemp;
Lnode *temp=tail;;
if(size==1)
head=tail=NULL;
ptemp->next = temp->next;
size--;
if(tail==temp)
tail = ptemp;
delete temp;


}

User is offlineProfile CardPM
+Quote Post

#include<wmx010>
RE: Linked Lists
22 Jan, 2008 - 05:38 PM
Post #2

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
Post your code like this: IPB Image
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Linked Lists
22 Jan, 2008 - 05:40 PM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, please when posting code use the code tags

code.gif

Now the problem, why don't you take a pointer of the list whose last element should be removed and use a while loop to go to the last element and then delete it.

Here is what I think:

CODE
int LList::remLast(Lnode *x)//this does not have parameters.
{
if(x==NULL){
cout<<"Empty List --- Removal is not";
cout<<"Possible --- Error"<<endl;
return -1;
}
Lnode *temp=x;
while(temp->next)
{
     temp=temp->next;
}
size--;
delete temp;
}


Hope you see where I'm getting at.
User is offlineProfile CardPM
+Quote Post

nirvanarupali
RE: Linked Lists
22 Jan, 2008 - 06:28 PM
Post #4

D.I.C Stomach
Group Icon

Joined: 1 Aug, 2007
Posts: 994



Thanked: 4 times
Dream Kudos: 375
My Contributions
Here is a good tutorial of Linked Lists

http://www.dreamincode.net/forums/showtopic31357.htm

http://www.dreamincode.net/forums/blog/mar...=522#comment666
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 01:55PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month