Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

lista dwukierunkowa dodawanie i wyswietlanie danych

Ostatnio zmodyfikowano 2017-01-03 00:54
Autor Wiadomość
mikewazowski
Temat założony przez niniejszego użytkownika
lista dwukierunkowa dodawanie i wyswietlanie danych
» 2017-01-02 22:06:28
jest ktoś w stanie wytlumaczyc mi co w moim kodzie jest nie tak? nie działa dodawanie osob na liste

C/C++
#include <iostream>
#include <string>
#include <cstdlib>
#include <stdio.h>
using namespace std;
typedef struct DANE
{
    string * imie;
} Dane;
typedef struct STUDENT Student;
struct STUDENT
{
    Dane dane;
    Student * next;
    Student * prev;
   
};
Student * stworzelement()
{
    Student * element;
    string * imie;
   
    cout << "podaj imie: ";
    cin >> imie;
    imie = new string;
    element = new Student;
    element->next = NULL;
    element->prev = NULL;
    element->dane.imie = imie;
    return element;
}
void dodajkoniec( Student ** head )
{
    Student * pom, * tmp = stworzelement();
    if( * head == NULL )
         * head = NULL;
    else
    {
        pom = * head;
        while( pom->next != NULL )
             pom = pom->next;
       
        tmp->prev = pom;
        pom->next = tmp;
    }
}
void wyswietl( Student * head )
{
    Student * tmp;
    tmp = head;
    if( tmp == NULL )
         cout << "brak elementow";
    else
    {
        int n = 1;
        while( tmp != NULL )
        {
            cout << "imie: " << tmp->dane.imie << endl
            tmp = tmp->next;
        }
    }
}


int main()
{
    dodajkoniec( & head );
    wyswietl( head );
    return 0;
}
P-155847
pekfos
» 2017-01-02 22:25:28
C/C++
if( * head == NULL )
     * head = NULL;
No.. ta instrukcja wszystko zmienia. Wszystko poza robieniem czegokolwiek użytecznego.
P-155849
mikewazowski
Temat założony przez niniejszego użytkownika
» 2017-01-02 22:30:31
poprawione na
* head = tmp;
 ale nadal wywala blad w linijce
cin >> imie;
 "no match for 'operator >>' "
P-155851
michal11
» 2017-01-02 22:40:35
Dlaczego trzymasz stringa przez wskaźnik?
P-155853
mikewazowski
Temat założony przez niniejszego użytkownika
» 2017-01-02 22:43:38
w sumie trudno stwierdzić czemu ustawiłam 2 razy
string * imie;
 ale po wpisaniu
string imie;
 nadal ten sam problem w
cin >> imie;
P-155854
michal11
» 2017-01-02 23:07:04
Zmieniłaś to w funkcji stworzelement czy w strukturze DANE? Wstaw aktualny kod.
P-155856
mikewazowski
Temat założony przez niniejszego użytkownika
» 2017-01-02 23:09:13
C/C++
#include <iostream>
#include <string>
#include <cstdlib>
#include <stdio.h>
using namespace std;
typedef struct DANE
{
    string imie;
} Dane;
typedef struct STUDENT Student;
struct STUDENT
{
    Dane dane;
    Student * next;
    Student * prev;
   
};
Student * stworzelement()
{
    Student * element;
    string imie;
   
    cout << "podaj imie: ";
    cin >> imie;
    imie = new string;
    element = new( struct Student ); // czy element = new Student; poprawne?
    element->next = NULL;
    element->prev = NULL;
    element->dane.imie = imie;
    return element;
}
void dodajkoniec( Student ** head )
{
    Student * pom, * tmp = stworzelement();
    if( * head == NULL )
    { * head = tmp;
        cout << "dodano do bazy" << endl; }
    else
    {
        pom = * head;
        while( pom->next != NULL )
             pom = pom->next;
       
        tmp->prev = pom;
        pom->next = tmp;
    }
}
void wyswietl( Student * head )
{
    Student * tmp;
    tmp = head;
    if( tmp == NULL )
         cout << "brak elementow";
    else
    {
        int n = 1;
        while( tmp != NULL )
        {
            cout << "imie: " << tmp->dane.imie << endl
            tmp = tmp->next;
        }
    }
}


int main()
{
    dodajkoniec( & head );
    wyswietl( head );
    return 0;
}
P-155857
mokrowski
» 2017-01-03 00:28:06
C/C++
#include <iostream>
#include <string>
#include <cstdlib>
// XXX: Nagłówki z C, mają prefix ... c :-) czyli stdio.h -> cstdio
// Na marginesie.. Po co Ci on?
#include <cstdio>

using namespace std;

struct Dane
{
    string imie;
};

struct Student
{
    Dane dane;
    Student * next;
    Student * prev;
};


Student * stworzelement()
{
    Student * element;
    string imie;
   
    cout << "podaj imie: ";
    cin >> imie;
    element = new Student(); // czy element = new Student; poprawne?
    // XXX: Takie poprawne ^
    element->next = NULL;
    element->prev = NULL;
    element->dane.imie = imie;
    return element;
}

void dodajkoniec( Student ** head )
{
    // XXX: Odzwyczaj się od notowania zmiennych z przecinkiem.
    // Bardzo błędogenne szczególnie przy wskaźnikach.
    Student * pom;
    Student * tmp = stworzelement();
    if( * head == NULL )
    {
        * head = tmp;
        cout << "dodano do bazy" << endl;
    } else {
        pom = * head;
        while( pom->next != NULL ) {
            pom = pom->next;
        }
       
        tmp->prev = pom;
        pom->next = tmp;
    }
}

void wyswietl( Student * head )
{
    Student * tmp;
    tmp = head;
    if( tmp == NULL )
         cout << "brak elementow";
    else
    {
        int n = 1;
        while( tmp != NULL )
        {
            cout << "imie: " << tmp->dane.imie << endl;
            tmp = tmp->next;
        }
    }
}


int main()
{
    Student * head = NULL;
    dodajkoniec( & head );
    wyswietl( head );
    return 0;
}
P-155861
« 1 » 2
  Strona 1 z 2 Następna strona