Jump to content

linked list

- - - - -

  • Please log in to reply
21 replies to this topic

#1
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
void addStudent(student *ptr,char stName[],int stID,int stG1,int stG2,int stG3)
{
    student *p;
    student *t;
    p = new student;
    strcpy(p->stName,stName);
    p->stID = stID;
    p->stG1 = stG1;
    p->stG2 = stG2;
    p->stG3 = stG3;
    if(ptr == NULL)
    {
        ptr = p;
    }else{
        t = ptr;
        while(t->next != NULL)
        {
            t = t->next;
        }
        t->prev = t;
        t->next = p;
    }
    //sortList(ptr,0);
}

I don't know what is the problem with this add new student!

#2
Infinity

Infinity

    Newbie

  • Members
  • PipPip
  • 24 posts
I don't know where is the problem, but i think, i can help you, if you post here where ( and how ) it is crashing and/or student class.

Infinity

#3
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

I think you need to do something like this!


 if(ptr == NULL)

    {

        ptr = p;

        [COLOR="red"]ptr->next = NULL;

        ptr->prev = NULL;

[/COLOR]    }else{

        t = ptr;

        while(t->next != NULL)

        {

            t = t->next;

        }

[COLOR="red"]        p->prev = t;

        t->next = p;

        p->next = NULL;

[/COLOR]    }


I hope this helps!

Munir

#4
Infinity

Infinity

    Newbie

  • Members
  • PipPip
  • 24 posts
Yes it looks like good idea. But i don't know if it will help. :c-cool:

Infinity

#5
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
I found my problem but now I have another problem:
but I have another problem just one data added I can not add more!!
void addStudent(student *ptr,char stName[],int stID,int stG1,int stG2,int stG3)
{
    student *p;
    student *t;
    p = new student;
    strcpy(p->stName,stName);
    p->stID = stID;
    p->stG1 = stG1;
    p->stG2 = stG2;
    p->stG3 = stG3;
    p->next = NULL;
    t = ptr;
    while(t->next != NULL)
    {
            t = t->next;
    }
    p->prev = t;
    t->next = p;
    //sortList(ptr,0);
}


#6
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

you probably missed my last post! The following code should help you!


if(ptr == NULL)

    {

        ptr = p;

        ptr->next = NULL;

        ptr->prev = NULL;

    }else{

        t = ptr;

        while(t->next != NULL)

        {

            t = t->next;

        }

        p->prev = t;

        t->next = p;

        p->next = NULL;

    }


Let me know if that works!

Munir

#7
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
It worked well!
now I have problem with while!!!
no end for this while!!! what should I do?
int main()
{
    student *ptr;
    ptr->next = NULL;
    char c;
    while(1)
    {
        cout<<"Double Linked list"<<endl;
        cout<<"1:Add new node."<<endl;
        cout<<"2:Remove one student."<<endl;
        cout<<"3:List in name order."<<endl;
        cout<<"4:List in avg order."<<endl;
        cout<<"5:Exit."<<endl;
        cin>>c;
        switch©
        {
            case '1':
                addStudent(ptr);
                break;
            case '2':
                cout<<"Enter ID:"<<endl;
                int id;
                cin>>id;
                removeStudent(ptr,id);
                break;
            case '3':
                sortList(ptr,1);
                printList(ptr);
                break;
            case '4':
                sortList(ptr,2);
                printList(ptr);
                break;
            case '5':
                return 0;
        }
    }
    return 0;
}


#8
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
All codes!
#include <iostream>
#include <cstring>

using namespace std;
struct student
{
    int stID;
    char stName[20];
    int stG1;
    int stG2;
    int stG3;
    student *next;
    student *prev;
};
void sortList(student *ptr,int tp)
{
    student *p;
    if(tp == 0)
    {
        student *t;
        for(p = ptr->next; p->next != NULL; p=p->next)
        {
            for(t = p->next;t != NULL; t=t->next)
            {
                if(p->stID > t->stID)
                {
                    int stID,stG1,stG2,stG3;
                    char stName[20];
                    stID = p->stID;
                    p->stID = t->stID;
                    t->stID = stID;
                    stG1 = p->stG1;
                    p->stG1 = t->stG1;
                    t->stG1 = stG1;
                    stG2 = p->stG2;
                    p->stG2 = t->stG2;
                    t->stG2 = stG2;
                    stG3 = p->stG3;
                    p->stG3 = t->stG3;
                    t->stG3 = stG3;
                    strcpy(stName,t->stName);
                    strcpy(t->stName,p->stName);
                    strcpy(p->stName,stName);
                }
            }
        }
    }
    if(tp = 1)
    {
        student *t;
        for(p = ptr->next; p->next != NULL; p=p->next)
        {
            for(t = p->next;t != NULL; t=t->next)
            {
                if(strcmp(p->stName,t->stName) !=0)
                {
                    int stID,stG1,stG2,stG3;
                    char stName[20];
                    stID = p->stID;
                    p->stID = t->stID;
                    t->stID = stID;
                    stG1 = p->stG1;
                    p->stG1 = t->stG1;
                    t->stG1 = stG1;
                    stG2 = p->stG2;
                    p->stG2 = t->stG2;
                    t->stG2 = stG2;
                    stG3 = p->stG3;
                    p->stG3 = t->stG3;
                    t->stG3 = stG3;
                    strcpy(stName,t->stName);
                    strcpy(t->stName,p->stName);
                    strcpy(p->stName,stName);
                }
            }
        }
    }
    if(tp = 2)
    {
        student *t;
        for(p = ptr->next; p->next != NULL; p=p->next)
        {
            for(t = p->next;t != NULL; t=t->next)
            {
                int a1 = (p->stG1+p->stG2+p->stG3)/3;
                int a2 = (t->stG1+t->stG2+t->stG3)/3;
                if(a1 > a2)
                {
                    int stID,stG1,stG2,stG3;
                    char stName[20];
                    stID = p->stID;
                    p->stID = t->stID;
                    t->stID = stID;
                    stG1 = p->stG1;
                    p->stG1 = t->stG1;
                    t->stG1 = stG1;
                    stG2 = p->stG2;
                    p->stG2 = t->stG2;
                    t->stG2 = stG2;
                    stG3 = p->stG3;
                    p->stG3 = t->stG3;
                    t->stG3 = stG3;
                    strcpy(stName,t->stName);
                    strcpy(t->stName,p->stName);
                    strcpy(p->stName,stName);
                }
            }
        }
    }
}
void addStudent(student *ptr)
{
    student *p;
    student *t;
    char stName[20];int stID,stG1,stG2,stG3;
    cout<<"Enter ID:"<<endl;
    cin>>stID;
    cout<<"Enter Name:"<<endl;
    cin>>stName;
    cout<<"Enter grades:"<<endl;
    cin>>stG1>>stG2>>stG3;
    p = new student;
    strcpy(p->stName,stName);
    p->stID = stID;
    p->stG1 = stG1;
    p->stG2 = stG2;
    p->stG3 = stG3;
    p->next = NULL;
    t = ptr;
    while(t->next != NULL)
    {
            t = t->next;
    }
    p->prev = t;
    t->next = p;
    sortList(ptr,0);
}
void removeStudent(student *ptr,int stID)
{
    if(ptr == NULL)
    {
        cout<<"Linked list is empty.";
        return;
    }else{
        student *p;
        student *t;
        t = ptr;
        while(t != NULL)
        {
            if(t->stID = stID)
            {
                if(t == ptr)
                {
                    ptr=t->next;
                    (t->next)->prev=NULL;
                    delete t;
                }else{
                    if(t->next == NULL)
                    {
                        p=t->prev;
                        p->next=NULL;
                        delete t;
                    }else{
                        p=t->next;
                        p->next=t->next;
                        p=t->next;
                        p->prev=t->prev;
                        delete t;
                    }
                }
            }
            cout<<"Target deleted successfully.";
            return;
            t=t->next;
        }
    }
}
void printList(student *ptr)
{
    student *p = ptr->next;
    while(p != NULL)
    {
        cout<<"StID:"<<p->stID<<" Name:"<<p->stName<<" G1:"<<p->stG1<<" G2:"<<p->stG2<<" G3:"<<p->stG3<<" Avg:"<<(p->stG1+p->stG2+p->stG3)/3<<endl;
        p = p->next;
    }
}
int main()
{
    student *ptr;
    ptr->next = NULL;
    char c;
    for(;;)
    {
        cout<<"Double Linked list"<<endl;
        cout<<"1:Add new node."<<endl;
        cout<<"2:Remove one student."<<endl;
        cout<<"3:List in name order."<<endl;
        cout<<"4:List in avg order."<<endl;
        cout<<"5:Exit."<<endl;
        cin>>c;
        switch©
        {
            case '1':
                addStudent(ptr);
                break;
            case '2':
                cout<<"Enter ID:"<<endl;
                int id;
                cin>>id;
                removeStudent(ptr,id);
                break;
            case '3':
                sortList(ptr,1);
                printList(ptr);
                break;
            case '4':
                sortList(ptr,2);
                printList(ptr);
                break;
            case '5':
                return 0;
        }
    }
    return 0;
}


#9
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

Please elaborate what's the problem?

Munir

#10
Infinity

Infinity

    Newbie

  • Members
  • PipPip
  • 24 posts
It was failing at many points, but evrytime on the ptr initiallization so i made these changes:
>>Add constructor function to you Student struct:

student() : stID(), stName(), stG1(), stG2(), stG3(), next(NULL), prev(NULL) {}


>>Change:

student *ptr;

ptr->next = NULL;

To:

student *ptr = new student;

...its calling constructor.
If you will have other problem, just ask, I'll be glad to help you. :c-thumbup:

Infintiy

#11
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
not solved the no loop stop!
This is my main.cpp file please check it!@

Attached Files



#12
Infinity

Infinity

    Newbie

  • Members
  • PipPip
  • 24 posts
You forgot add constructor, as i said. Your student class will look like this:

struct student

{

	int stID;

	char stName[20];

	int stG1;

	int stG2;

	int stG3;

	student *next;

	student *prev;

	student() : stID(), stName(), stG1(), stG2(), stG3(), next(NULL), prev(NULL) {};

};




Infinity




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users