C/Visual C - Lista simplemente ligada con arreglos

 
Vista:

Lista simplemente ligada con arreglos

Publicado por ritcher (1 intervención) el 03/09/2021 23:32:30
Ayuda, mi funcion remplazar no funciona y necesito agregar una funcion que busque el nombre del registro y si se encuentra mostrar todos los datos del registro.

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <string.h>
 
const int MaxData=10;
 
struct Address
{
    char name[50];
    char street[100];
    char city[50];
    char state[20];
    int pin;
} data[10];
 
int head=0;
int actual=0;
int ultimo=0;
 
void inserta()
{
    ultimo++;
    fflush(stdin);
    printf("\tGive the name ");
    gets(data[ultimo].name);
    printf("\tGive the city ");
    gets(data[ultimo].city);
    printf("\tEstado: ");
    gets(data[ultimo].state);
    printf("\tPin: ");
    scanf("%i",&data[ultimo].pin);
    printf("\t");
    system("pause");
    system("cls");
}
 
void siguiente()
{
    printf("\n\tName: %s\n", data[actual].name);
    printf("\tCity: %s\n", data[actual].city);
    printf("\tState: %s\n", data[actual].state);
    printf("\tpin: %i\n\n", data[actual].pin);
    actual++;
 
    if (actual >MaxData || actual>ultimo)
    {
        printf ("\tA llegado al ultimo nodo\n");
        actual--;
    }
    printf("\t");
    system("pause");
    system("cls");
}
 
 
void imprimeLista()
{
    int i;
    actual=head;
 
    for(i=head; i<= ultimo; i ++)
        siguiente();
}
 
void remover()
{
    system("cls");
    int indice;
    printf("\tQue nodo quieres remover?\n\t");
    scanf("%i", &indice);
    indice--;
    if (indice > ultimo || indice <head )
    {
        printf("\tindice fuera de rango");
        return;
    }
    int i;
    for(i=indice; i<=ultimo; i++)
    {
        strcpy(data[i].name, data[i+1].name);
        strcpy(data[i].city, data[i+1].city);
        strcpy(data[i].street, data[i+1].street);
        strcpy(data[i].state, data[i+1].state);
        data[i].pin=data[i+1].pin;
    }
    if(indice == MaxData-1)
    {
        strcpy(data[indice].name,"");
        strcpy(data[indice].city,"");
        strcpy(data[indice].street,"");
        strcpy(data[indice].state,"");
        data[indice].pin-0;
    }
    ultimo--;
    actual=head;
    printf("\t");
    system("pause");
    system("cls");
}
 
int main()
{
    int opcion;
 
    strcpy(data[0].name, "Jhon Brown");
    strcpy(data[0].street, "5th");
    strcpy(data[0].city, "NY");
    strcpy(data[0].state, "NY");
    strcpy(data[1].name, "Ana");
    strcpy(data[1].street, "alcalde");
    strcpy(data[1].city, "Gdl");
    strcpy(data[1].state, "Jal");
    ultimo=1;
 
    while(opcion!=5)
    {
        printf("\tQue opcion deseas: \n\t1-inserta \n\t2-imprime lista \n\t3-siguiente nodo \n\t4-Remover \n\t5-salir\n\n\tOpcion: ");
        scanf("%i", &opcion);
 
        switch(opcion)
        {
        case 1:
            system("cls");
            inserta();
            break;
        case 2:
            system("cls");
            imprimeLista();
            break;
        case 3:
            system("cls");
            siguiente();
            break;
        case 4:
            system("cls");
            remover();
            break;
        case 5:
            break;
        default:
            printf("\tEsa opcion no existe...\n");
        }
    }
    return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Lista simplemente ligada con arreglos

Publicado por anonymous (8 intervenciones) el 04/09/2021 03:59:25
Hola, podrías dar más detalles de tu problema para ayudarte? Indicar con exactitud apartir de donde la función que mencionas no funciona correctamente, etc.

Por cierto en la función remover hay una línea que dice "data[indice].pin-0" pero eso no hará nada debes cambiarlo por "data[indice].pin = 0"; Porque según veo es lo que creo que querías hacer en la función.

Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar