For Linked Lists, I know the code for insertBack and deleteFront....but How do I change insertBack into insertFront and deleteFront into deleteBack with the code I currently have? Please help!


deleteFront code:

void deleteFront(nodeType*& first)
{

  nodeType *last,*current,*trailcurrent;
  bool found;
  int num;

  
  cout<<endl;
  cout<<"Inside deleteFront...removing item from front of list..."<<endl;
  cout<<endl;
    
             
         if (first->info == 1)
         {
           current = first;
           first = first->link;
         }
         if (first == NULL)
         {
            last = NULL;
            
            delete current;
         }
         else
         {
         found = false;
         trailcurrent = first;
         
         current = first->link;
         }
         
}

Insert Back code:

void insertBack(nodeType*& last)
{
      
  int num;
  nodeType *first,*newNode;
    
  
  cout<<endl;
  cout<<"Enter a number to add to the END of the list: "<<endl;
  cin>>num;
  

         newNode = new nodeType;
         newNode->info = num;
         newNode->link = NULL;
    
         if (first == NULL)
         {
           first = newNode;
           last = newNode;
           
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }

         
}

Recommended Answers

All 19 Replies

What you have clearly doesn't work:

Consider insertBack, you do not set first. It can be anything!

IF you had set first then conversion is easy.

newNode->link=first;

Now consider deleteFront, I do not understand what you are doing checking first->info (and what would happen if first was zero!)

So try testing these two functions with a range of input.

I just want to know what I need to change in my code to make insertBack into insertFront and deleteFront to deleteBack. Do you know what I need to change to make this happen?

It is simple you fix deleteFront and insertBack, because neither work.

This looks like homework -- therefore show some effort.

If it not homework, show some pride in your work. Fix the first problems first.

No one said this is homework. I asked a simple question. If you are not going to help me (which you have not done so already) then do not respond.

If it is to much to change in my code then that is all you had to say.

What are the common identifiers that I would need for the new function(s), and I can try to work with that for now.

If it is to much to change in my code then that is all you had to say.

Well it works like this, you have posted two functions that DONT work.
I wrote a little bit about why they don't work and you don't want to try to fix them. Then you want the community to tell you how to change them to what you want!!

So it is either (a) a learning project (b) homework , (c) real production code. If it is (c) then you would be using std::list (or at least saying why you can't). If it is (a) then start by fixing what you have posted.

So don't just demand the answer you want. Enter a discussion.

I just want to know what I need to change in my code to make insertBack into insertFront and deleteFront to deleteBack. Do you know what I need to change to make this happen?

For Linked Lists, I know the code for insertBack and deleteFront....

No, you DON'T know the code for insertBack and deleteFront. As StuXYZ mentioned, the code for these won't work, and he told you why. He HAS helped you. You completely blew him off. He has explained that what you want to "convert" to the other functions don't work themselves. You can either try to understand what he has said or you can "convert" two functions that don't work into two more functions that don't work.

1) I have tested both functions in my original post, and they both work fine.

2) This is not A (Learning project), B (Homework), nor C (A real production code).

3) If you can not answer my question then just don't respond.

I didn't blow him off. I didn't say anything mean to him.

I'm just here for some help, and I feel that him making accusations towards me was not helping.

Well, if he felt that way then I'm sorry. It is just how I type.

Here is some code I wrote to make the other guy feel better.

For insertFront function, the number that input does not get inserted into the Linked List. For my deleteBack function, the number gets deleted, but it is replaced with a 0.

My two questions are:


What is the problem in my insertFront function which does not insert a new element in to my Linked List?

How do I get rid of the 0 as my result in deleteBack function?

void insertFront(nodeType*& first)
{

nodeType *newNode;
int num;

cout<<endl;
cout<<"Enter a number to add to the Front of the list: "<<endl;
cin>>num;

newNode = new nodeType;
newNode->info = num;

if (first == NULL)
{
first = newNode;
}
else
{
newNode->link = first->link;
first = newNode;
}

}
void deleteBack(nodeType*& last)
{

nodeType *current,*previous,*first;
bool found;
int num;


cout<<endl;
cout<<"Inside deleteBack...removing item from back of list..."<<endl;
cout<<endl;


current = new nodeType;
current = first; 
previous = new nodeType;

while(current->link != NULL)
{
previous = current; 
current = current->link; 
}

previous->link = NULL;
delete current;
}

1) I have tested both functions in my original post, and they both work fine.

Test them some more. I don't know what assumptions you are making, you haven't posted the struct itself, and you haven't posted the test program. Try passing the function NULL. You are also very likely "deleting" the current node that is uninitialized. num is not used. The fact is that the code you posted has some problems and doesn't make much sense. Maybe it would make more sense if you posted the entire program, but I doubt it. So you are trying to model two new functions after two functions that don't work themselves. Anyone here is going to tell you to fix the original two functions first.

If you don't understand why we are saying those original two functions don't work, post the nodeType struct along with your test program and any assumptions that you make regarding variable parameters.

Read Post #12 please.

Read Post #12 please.

I've read it. You have a memory leak. You are calling new twice and delete once in deleteBack. If your are deleting, you shouldn't be creating any new nodes.

I've read your post 12. You read my post 13. You need to post your struct and your test program so people can get a fuller picture and can run the program easily.

Well it is something that I just threw together a few minutes ago.. Please tell me what I did wrong. I'm trying to memorize this stuff, so I can get good at it.


Here is the code:

#include <iostream>

using namespace std;
struct nodeType
{
    int info;
    nodeType *link;
};

void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first);
void insertFront(nodeType*& last);
void deleteBack(nodeType*& first);

int main()
{
    
    nodeType *first, *last;
    int num;
    
    createList(first,last);
    printList(first);
    
    
    insertFront(last);
    printList(first);
    
    deleteBack(first);
    printList(first);
    
  system("PAUSE");
   return 0;
}

void createList(nodeType*& first, nodeType*& last)
{
    int number;
    int emptyList;
    nodeType *newNode;
    int counter = 0;
    
    first = NULL;
    last = NULL;
    
    
    cout<<"Enter an integer (-999 to stop): ";
    cin>>number;
    cout<<endl;

     while (number != -999)
     {
         newNode = new nodeType; // create new node
         newNode->info = number;
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
         cout<<"Enter an integer (-999 to stop): ";
         cin>>number;
         cout<<endl;
         counter++;
     }
        
     if (first == NULL)
   cout<<"Empty list"<<endl;
    else
   cout<<"There are "<<counter<<" items in the linked list"<<endl;   
        
}

void printList(nodeType*& first)
{
    
    cout<<endl;
    cout<<"Inside printList...printing linked list...\n"<<endl;
        nodeType *current;
        current = new nodeType;
        current = first;    
        while (current != NULL)
        {
          cout << current->info<<endl;
          current = current->link;
        }
}  

void insertFront(nodeType*& first)
{

nodeType *newNode;
int num;

cout<<endl;
cout<<"Enter a number to add to the Front of the list: "<<endl;
cin>>num;

newNode = new nodeType;
newNode->info = num;

if (first == NULL)
{
first = newNode;
}
else
{
newNode->link = first->link;
first = newNode;
}

}


void deleteBack(nodeType*& last)
{

nodeType *current,*previous,*first;
bool found;
int num;


cout<<endl;
cout<<"Inside deleteBack...removing item from back of list..."<<endl;
cout<<endl;


current = new nodeType;
current = first; 
previous = new nodeType;

while(current->link != NULL)
{
previous = current; 
current = current->link; 
}

previous->link = NULL;
delete current;
}

One, I would not prompt for input inside your insert functions. Pass the insert functions both the pointer to the node and the value to be inserted. Prompt for the value to insert somewhere else, like in createList, which you do. Call the insert functions from createList.

Two, create one new node with the new command in the insert functions and nowhere else. Delete one node in the delete functions and nowhere else. For sure, there is no reason to have the word delete in the insert function or new in the delete functions.

Three, get rid of any variables, including pointers, that you don't use. Make sure that any local pointer variable that you create inside your functions is initialized before assigning anything to it.

Four, in your delete function, check for NULL-ness at the top of the function and if the variable passed to it is null, return immediately.

Five, is this a doubly linked list or a singly linked list? If it's a singly linked list, and it appears to be a singly linked list, what does last represent? You can't traverse backwards from last to first in a singly linked list, can you?

Alright, thank you. I'm going to try what you say. I will report back to you soon.

I got my delete function to work, but I'm still having trouble with my insert function. Can you tell me what I am doing wrong with the code?

Goal: I'm asking a user what number does he/she wants to add to the front of the list, but when I print my list, the number doesn't get inserted. Thanks

#include <iostream>

using namespace std;
struct nodeType
{
    int info;
    nodeType *link;
};

void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first);
void insertFront(nodeType*& last);
void deleteBack(nodeType*& first);

int main()
{
    
    nodeType *first, *last;
    int num;
    
    createList(first,last);
    printList(first);
    
    
    insertFront(last);
    printList(first);
    
    deleteBack(first);
    printList(first);
    
  system("PAUSE");
   return 0;
}

void createList(nodeType*& first, nodeType*& last)
{
    int number;
    int emptyList;
    nodeType *newNode;
    int counter = 0;
    
    first = NULL;
    last = NULL;
    
    
    cout<<"Enter an integer (-999 to stop): ";
    cin>>number;
    cout<<endl;

     while (number != -999)
     {
         newNode = new nodeType; // create new node
         newNode->info = number;
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
         cout<<"Enter an integer (-999 to stop): ";
         cin>>number;
         cout<<endl;
         counter++;
     }
        
     if (first == NULL)
   cout<<"Empty list"<<endl;
    else
   cout<<"There are "<<counter<<" items in the linked list"<<endl;   
        
}

void printList(nodeType*& first)
{
    
    cout<<endl;
    cout<<"Inside printList...printing linked list...\n"<<endl;
        nodeType *current;
        current = new nodeType;
        current = first;    
        while (current != NULL)
        {
          cout << current->info<<endl;
          current = current->link;
        }
}  

void insertFront(nodeType*& first)
{

nodeType *newNode,*last;
int num;


cout<<endl;
cout<<"Enter a number to add to the Front of the list: "<<endl;
cin>>num;

newNode = new nodeType;
newNode->info = num;
newNode->link = NULL;

if (first == NULL)
{
first = newNode;
}
else
{
newNode->link = first;
first = newNode;
}

}


void deleteBack(nodeType*& last)
{

nodeType *current,*previous,*first;
bool found;
int num;

current = last; 

cout<<endl;
cout<<"Inside deleteBack...removing item from back of list..."<<endl;
cout<<endl;
 

while(current->link != NULL)
{
previous = current; 
current = current->link; 
}

previous->link = NULL;
delete current;
}

I have solved the problem. Thanks to all who helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.