linkd List

May 8, 2013 at 8:51pm
The assignment is to store 5 names with the ID Number using linklist but the problem with my code
is that the last name is start repeating
Example Run
Entre un Nombre
juan
Entre un Nombre
jose
Entre un Nombre
carlos
Entre un Nombre
jesus
Entre un Nombre
alexander
ID:5 Nombre :alexander
ID:4 Nombre :alexander
ID:3 Nombre :alexander
ID:2 Nombre :alexander
ID:1 Nombre :alexander

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/ main.ccp
#include<iostream>
#include<cstdio>
using namespace std;
#include "lista.h"


int main() {
    lista cadenas;
    char respuesta[50];
    char nombre[50];
    
    
    
   
   for(int i=1;i<=5;i++){      
         cout<<"Entre un Nombre"<<endl;
         cin>>nombre;
         cadenas.listaCreate(i,nombre);
   }
  cadenas.mostrar(); 
    
    
    return 0;
};

/ source.ccp


#include<cstdlib>
#include<iostream>


#include "lista.h"

using namespace std;

lista::lista(){
    head=NULL;
    current=NULL;
    
};
void lista::listaCreate(int ID, char *tmp){
    struct nodo*ptr;
    ptr=(struct nodo*)malloc(sizeof(struct nodo));
    
    ptr->data=ID;
    ptr->name=tmp;
    if(head==NULL){
        head=ptr;
        head->next=NULL;
    }
    else
    {
        
        ptr->next=head;
        head=current=ptr;
     }
   
  
  
    
   
};

 

void lista::mostrar(){
    struct nodo*ptr = head;
    
   
    while(ptr !=NULL){
        cout<<"ID:"<<ptr->data <<" Nombre :"<<ptr->name<<endl;
        ptr=ptr->next;
     
         }
};

/lista.h

#ifndef LISTA_H
#define LISTA_H


class lista 
{
    private:
        struct nodo
        {
            int data;
            char *name;
            struct nodo*next;

        };

        nodo*head;
        nodo*current;

    public:
        lista();
        void listaCreate(int x,char *y);
        void mostrar();
};

#endif
May 8, 2013 at 9:09pm
`ptr->name=tmp;' is copying the pointers. You want to copy the content instead.
I suggest you to use `std::string' and `std::list'
May 9, 2013 at 12:18pm
Can you be a little more specific or give a example
May 9, 2013 at 9:23pm
1
2
3
4
5
6
7
8
#include <iostream>
int main(){
   char array[] = {"hello"};
   char *ptr;
   ptr = array;
   ptr[0] = 'x';
   std::cout << array << '\n';
}
xello
May 10, 2013 at 6:57pm
Thanks a lot you resolve my problems I change everything to string
Hire my code

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



int main() {
lista cadenas;
string nombre;




for(int i=1;i<=2;i++){
cout<<"Entre un Nombre"<<endl;
cin>>nombre;
cadenas.listaCreate(i,nombre);
}
cadenas.mostrar();





return 0;
};

#include<cstdlib>
#include<iostream>


#include "lista.h"

using namespace std;

lista::lista(){
head=NULL;
current=NULL;

};
void lista::listaCreate(int ID, string tmp){
struct nodo*ptr;
ptr=(struct nodo*)malloc(sizeof(struct nodo));

ptr->data=ID;
ptr->name=tmp;

if(head==NULL){
head=ptr;
head->next=NULL;

cout << head->name << " Data has been created\n";
}
else
{
struct nodo*ptmp;
ptmp=(struct nodo*)malloc(sizeof(struct nodo));

ptmp = head;
ptmp->next = NULL;
cout << ptmp->name << " Temp data created.";

head = ptr;
cout << ptr->name << "New data created.";
ptr->next=ptmp;


}

};


#ifndef LISTA_H
#define LISTA_H


class lista
{
private:
struct nodo
{
int data;
std::string name;
struct nodo*next;

};

nodo*head;
nodo*current;

public:
lista();
void listaCreate(int x,std::string y);
void mostrar();



};

#endif
Topic archived. No new replies allowed.