I've been working on a Linked List assignment. I completed it but I still have a problem on deleting an element that has a predecessor or after an element and inserting an element after an element...can anyone give me an idea?
This is what I have so far

#include <iostream>
    using namespace std;

    struct Node{
    int x, y;
    Node *next;
    };
    Node *start_ptr = NULL;

    void traversal()
    {
    Node *temp;
    temp = new Node;
    temp = start_ptr;
    while (temp != NULL)
    {
    temp = temp -> next;
    }
    }
    void delete_start__node()
    {
    Node *temp;
    temp = start_ptr;
    start_ptr = start_ptr -> next;
    delete temp;
    }
    void insert_at_Beginning()
    {
    Node *newptr;
    newptr = new Node;
    newptr -> next = start_ptr;
    start_ptr = newptr;
    }

How/where are x and y used? Do they represent data? So you want to be able to delete or insert a node somewhere in the middle or end of the list? You want something like this for a prototype?

void DeleteNode (int n); // deletes nth node in the list
void InsertNode (int n);  //  inserts new node at nth spot in the list

The above prototypes assume that there is no member of the Node struct that represents some data value.

Here's how:

void removeFromFront(nodeType *&first, nodeType *&current)
{
	char touch;

	if (isEmpty(first))
	{
		cout << "There are no elements in the list. The list is empty." << endl;
		touch = getch();
	}
	else if (first == current)
	{
		delete first;
		first = NULL;
		current = NULL;
	}
	else
	{
		nodeType *tmp = first;
		first = first->link;
		delete tmp;
	}
}
void removeLastElement(nodeType *&first)
{
	nodeType *p = first, *tmp;

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

	tmp = p->link;
	p->link = NULL;
	delete tmp;
}
This article has been dead for over six months. Start a new discussion instead.