can anyone help me to slove the problem ??
i wan to delete a node either form the head , midder or last , then print out all the node .

#include<iostream>
#include<string>

using namespace std;

class linklist
{
private:
    struct node{
        struct student{
            string name;
            int id;
            string prog;
            double cgpa;
        }st;
        node *link;
    }*head;

public:
    linklist();
    void linklist_2();
    void insFirstNode (string name, int id, string prog, double cgpa);
    void printAllList();
    void deleteSearchList(int id);

};

linklist::linklist(){
    head = NULL;
}
void linklist::linklist_2()
{
    node *p;
    if (head == NULL)
        return ;
    while (head !=NULL)
    {
        p = head -> link ;
        delete head;
        head = p;
    }
}


void linklist::insFirstNode(string name, int id,string prog, double cgpa)
{
    node * newNode,*p;
    newNode = new node;
    newNode -> st.name = name;
    newNode -> st.id = id;
    newNode -> st.prog = prog;
    newNode -> st.cgpa = cgpa;
    newNode -> link = NULL;

    if (head == NULL)
        head = newNode;
    else
    {
        p = head;

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

void linklist::deleteSearchList(int id)
{
    node * cur,*p;
    p = head;
    cur = head;

    bool found;
    found = false;

    while ((cur !=NULL) && (!found))
    {
        if (cur ->st.id ==id )
            found = true;
        else
            p = cur;
            cur = cur->link;
    }


    p->link = cur ->link;
    delete p;
    cout<<"deleted"<<endl;

}



void linklist :: printAllList()
{
    node * cur;
    cur = head;
    cout<<" { ";

    while (cur !=NULL)
    {
        cout<<cur ->st.name<<" ";
        cout<<cur ->st.id<<" ";
        cout<<cur ->st.prog<<" ";
        cout<<cur ->st.cgpa<<" ";

        cur = cur->link;
        if (cur != NULL)
            cout<<", ";
    }
    cout<<" } "<<endl;
}



int main()
{
    char ans;
    string name1,prog1;
    int id1;
    double cgpa1;

    linklist L;

    do{
    cout<<"A.) Insert new student"<<endl
        <<"b.) Delete student info"<<endl
        <<"D.) Print out ALL student info" <<endl
        <<"E.) View student cgpa 3.0 above "<<endl;

    cout<<"Pls choice one option : ";
    cin>>ans;

    switch (ans)
    {
    case 'a':
    case 'A':

        cout<<"student name : "<<endl;
        cin>>name1;
        cout<<"ID: "<<endl;
        cin>>id1;

        cout<<"prog : "<<endl;
        cin>>prog1;

        cout<<"CGPA: "<<endl;
        cin>>cgpa1;

        L.insFirstNode (name1,id1,prog1,cgpa1);
        L.printAllList();

        break;

    case 'b':
    case 'B':
        cout <<"pls enter Student ID tat wan delete form node:";
        cin >> id1;

        L.deleteSearchList(id1);


        break;


    case 'd':
    case 'D':
        L.printAllList();

        break;


    case 'f':
    case 'F':
        break;
    default :
        cout<<"enter again"<<endl;

    }


    }while (toupper(ans) !='F');

        cout<<"exit the loop"<<endl;




    system("pause");
    return 0;

}

Recommended Answers

Deleting items from a linked list:
1: deleting the head.
If you want to delete your head of the list you should do this:

node temp
set temp = head
set head = head.next
delete temp

2: deleting an item from the middle
For deleting an …

Jump to Post

All 4 Replies

Deleting items from a linked list:
1: deleting the head.
If you want to delete your head of the list you should do this:

node temp
set temp = head
set head = head.next
delete temp

2: deleting an item from the middle
For deleting an item from the middle of the list you should:

traverse the list keeping with you two nodes: current node and its parent.
if current node = searched node then
    set parent.next = current node.next
    delete current node
end_if

3: deleteing the tail:
Probably the esiest to delete

traverse the list keeping two nodes: current node and its parent
if current node.next = NULL then
    set parent.next = NULL
    delete current node
end_if

is there any simple way to delete any node that i wan ???

Nope.
Linked lists don't have direct access to elements, such as arrays.
As you can see from your posted code, the linkage is done via a pointer. Basically, linked lists are just pointers which are connected through each other. If you do want to access an element, you'll have to traverse the whole list in order to get to your node, which makes searching expensive. Nonetheless, inserting can be implemented quickly, such as memory management.
Perhaps this link will explain better how to delete certain nodes: Click Here

ok ok
thanks ya

Be a part of the DaniWeb community

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