void my_int_sllist::pop_back()
{
//head and tail pointers 
	cout<<"enter pop_back";
//contains one node
	if(head==tail)
	{
		cout<<"enter head==tail?";
		delete head;
		head=tail=0;
		siz--;
	}
	else
	{
		cout<<"enter's other condition";
		my_int_node* temp;
		for(temp=head; temp->next!=tail; temp=temp->next)
		{
			delete tail;
			tail=temp;
			tail->next=0;
		}
		siz--;
	}
}

Recommended Answers

All 10 Replies

How many nodes do you want to delete? Line 19 deletes a node every time it goes through the for-loop. If you only want to delete one node, don't stick "delete" in a loop.

STILL UNSOLVED
NODE CLASS

//class used to create a node

class my_int_node
{
public:
	//member that stores the nodes data value
	int data;
	
	//pointer to next list node
	my_int_node* next;

	/*constructor that creates a node when invoked
	by specifiying what value and a pointer to the 
	next node */
	inline my_int_node(int value, my_int_node* follow=0)
	{
		data=value;
		next=follow;
	}
};

LIST.h

#include "My_Int_Node.h"

class my_int_sllist
{
public:
	//constructor to create an empty list
	my_int_sllist();

	/*Insert a node containing number at the first position 
	of the singly linked list*/
	void push_front(int number);

	/*Insert a new node containing number at the last position 
	of the singly linked list*/
	void push_back(int number);

	// Delete the first node of the singly linked list
	void pop_front();

	// Delete the first node of the singly linked list
	void pop_back();

	//number of nodes that store values
	int size();

	//prints out the list
	void show_int_sllist();

private:
	//pointers to front and back of list
	my_int_node* head, *tail;

	//tell number of nodes that are filled in the list
	int siz;
};

List.cpp

#include "My_Int_Sllist.h"
#include <iostream>
using namespace std;

my_int_sllist::my_int_sllist()
{
	head=0;
	tail=0;
	siz=0;
}

void my_int_sllist::push_front(int number)
{
	/*1.Creates an empty node
	2.nodes data member initalized to a particular value
	3.adding to front, so to connect current node to rest must update
	the next member to head position
	4.must update value of head to make it point to current node*/
	head=new my_int_node(number,head);
	//if their is an empty list
	if(tail==0)
	{
		tail=head;
	}
	siz++;
}

void my_int_sllist::push_back(int number)
{
	//the list isn't empty
	if(tail!=0)
	{
		/*1.create an empty node
		2.the new nodes member is initalized to number
		3.next is set to null since it is at end of list 
		and doesn't need to create a new link to the next node
		4.tail->next is the next member of the node pointed to by tail 
		thus making the next member point to the new node*/
		tail->next=new my_int_node(number);

		//must update value of tail so that the last value may be accessed
		tail=tail->next;
	}
	//list is empty
	else
	{
		/*1.create a empty node
		2.new nodes member is initalized to number
		3.need to update tail and head pointer so that 
		they point to the node allowing access*/
		head=tail=new my_int_node(number);
	}
	siz++;
}

void my_int_sllist::pop_front()
{
	//int el=head->data;
	//temp is a pointer that gets node pointed to by head
	my_int_node* temp=head;

	//empty list
	if(head==0&&tail==0)
	{
		cout<<"can't remove from an already empty list\n";
	}

	//one node in the list
	else if(head==tail)
	{
		head=tail=0;
		siz--;
	}
	//list contains more than one node
	else
	{
		/*head updated to next node that follows by using the
		pointer next of the first node */
		head=head->next;
		siz--;
	}
	delete temp;
	temp=NULL;
		//cout<<"value deleted"<<el<<"\n";
}

void my_int_sllist::pop_back()
{	
	cout<<"enter pop_back";
	if(head==tail)
	{
		cout<<"enter head==tail?";
		delete head;
		head=tail=0;
		siz--;
	}
	else
	{
		cout<<"enter's other condition";
		my_int_node* temp;
		for(temp=head; temp->next!=tail; temp=temp->next)
		{
			tail=temp;
			tail->next=0;
		}
		delete tail;
		siz--;
	}
}		

int my_int_sllist::size()
{
	return siz;
}
void my_int_sllist::show_int_sllist()
{
	my_int_node* temp=head;
	if(head==0&&tail==0)
	{
		cout<<"nothing to show\n";
	}
	else
	{
		while(temp->next!=0)
		{
			cout<<temp->data<<"\n";
			temp=temp->next;		
		}
		cout<<temp->data<<endl;
	}
}

TEST

#include "My_Int_Sllist.h"
#include <iostream>

using namespace std;

int main()
{
	//Test 1
	cout<<"TEST1\n";
	my_int_sllist a;
	a.push_back(1);
	a.push_back(2);
	a.show_int_sllist();
	cout<<"Size is:"<<a.size()<<"\n"<<endl;

	//Test 2
	cout<<"TEST2\n";
	my_int_sllist b;
	b.push_back(3);
	b.push_back(4);
	b.push_front(5);
	b.push_front(6);
	b.push_front(7);
	b.show_int_sllist();
	cout<<"Size is:"<<b.size()<<"\n"<<endl;

	//Test 3
	cout<<"TEST3\n";
	my_int_sllist c;
	c.push_back(3);
	c.push_back(4);
	c.push_front(5);
	c.push_front(6);
	c.push_front(7);
	c.pop_front();
	c.pop_front();
	c.show_int_sllist();
	cout<<"Size is:"<<c.size()<<"\n"<<endl;

	//Test 4
	cout<<"TEST4\n";
	my_int_sllist d;
	d.push_back(3);
	d.push_back(4);
	d.push_front(5);
	d.push_front(6);
	d.push_front(7);
	d.pop_front();
	d.pop_front();
	d.pop_back();
	d.pop_back();
	d.show_int_sllist();
	cout<<"Size is:"<<d.size()<<"\n";

	
	return 0;
}
for(temp=head; temp->next!=tail; temp=temp->next)
{
tail=temp;
tail->next=0;
}

Same problem as before. How many pointers need to be set to null? One? Then it shouldn't be inside of a loop.

for(temp=head; temp->next==tail; temp=temp->next)
{
tail=temp;

}
tail->next=0;

any hints as to how to fix this?

any hints as to how to fix this?

Anything that happens once doesn't happen every time you go through a loop. Anything that happens roughly once per node goes in a loop. Your task, not necessarily in this order:

  1. Find the tail.
  2. Delete the tail.
  3. Find anything that points to the tail.
  4. Set anything that points to the tail to null.

Figure out what order these things need to be done.

Pencil and paper. Make a list. Figure out exactly what you do on paper. Translate that to code. You have three possible places to put something.

  1. Before the loop.
  2. Inside the loop.
  3. After the loop.

The order that the tasks need to be done and how many times they need to be done tells you where to put everything.

sweet kind of got it to work but the problem is returns memory location.

thanks that helped a lot. Almost got it figured out just don't know why the last element in list would be a memory location.

SO CLOSE BUT LAST NODE STORES memory address

void my_int_sllist::pop_back()
{	
	cout<<"enter pop_back";
	if(head==tail)
	{
		//cout<<"enter head==tail?";
		delete head;
		head=tail=0;
		siz--;
	}
	else
	{
		//cout<<"enter's other condition";
		my_int_node* temp=head;
		for(temp=head; temp->next!=0; temp=temp->next)
		{
			cout<<"number";
			tail=temp;
		}
		delete tail;
		tail->next=0;
		
		siz--;
	}
}

thanks for all the help. Figured it out :)

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.