Count and modify the elements of a queue

1

I have the following code to work with queues. How could the elements of the queue be counted and modified in the following code?

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;

struct Nodo{
  char dato;
  Nodo *siguiente;  
};

//Prototipos de Funciones
void menu();
void insertarCola(Nodo *&,Nodo *&,char);
bool cola_vacia(Nodo *);
void suprimirCola(Nodo *&,Nodo *&,char &);

int main(){

  menu();

  getch();
  return 0;
}

void menu(){
  int opc;
  char dato;

  Nodo *frente = NULL;
  Nodo *fin = NULL;

  do{
    printf("Menu\n");
    printf("Insertar un caracter a una cola");
    printf("Mostrar todos los elementos de la cola");
    printf("Salir");
    printf("opcion");
    scanf("%d",&opc);
    switch(opc){
        case 1: printf("\nIngrese el caracter para agregar a la cola: ");
                scanf("%d",&dato);
                insertarCola(frente,fin,dato);
                break;
        case 2: printf("\nMostrando los elementos de la cola: ");
                while(frente != NULL){
                    suprimirCola(frente,fin,dato);
                    if(frente != NULL){
                        cout<<dato<<" , ";
                    }
                    else{
                        cout<<dato<<".";
                    }
                }
                printf("\n");
                system("pause");
                break;
        case 3: break;
    }
    system("cls");
  }while(opc != 3);
}

//Función para insertar elementos en la cola
void insertarCola(Nodo *&frente,Nodo *&fin,char n){
  Nodo *nuevo_nodo = new Nodo();

  nuevo_nodo->dato = n;
  nuevo_nodo->siguiente = NULL;

  if(cola_vacia(frente)){
    frente = nuevo_nodo;
  }
  else{
    fin->siguiente = nuevo_nodo;
  }

  fin = nuevo_nodo;
}

//Función para determinar si la cola está vacia
bool cola_vacia(Nodo *frente){
  return (frente == NULL)? true : false; 
}

//Función para eliminar elementos de la cola
void suprimirCola(Nodo *&frente,Nodo *&fin,char &n){
  n = frente->dato;
  Nodo *aux = frente;

  if(frente == fin){
    frente = NULL;
    fin = NULL;
  }
  else{
    frente = frente->siguiente;
  }
  delete aux;
}
    
asked by Laura 11.09.2018 в 03:51
source

3 answers

0

This is the code in the C++ language that I prepared and that does what you request in your question: Count the elements of the list, and be able to access the elements to modify them, either by your index or by looking for a character.

Code:

#include <iostream>

typedef struct node {
    char data;
    struct node *next;
} node_t;

void print_list(node_t *);
void count_list_elements(node_t *);
void modify_an_item_by_index(node_t *, int, char);
void modify_an_item_by_char(node_t *head, char i, char new_data);

int main(void) {
    int i;
    char u;
    char data;

    node_t *head = NULL;
    head = new node_t();

    head->data = 'a';
    head->next = new node_t();
    head->next->data = 'e';
    head->next->next = NULL;
    std::cout << "List: " << std::endl;
    print_list(head);
    std::cout << "Ingresa el indice del elemento a modificar (comienza desde 0): ";
    std::cin >> i;
    std::cout << "Ingresa el nuevo valor: ";
    std::cin >> data;
    modify_an_item_by_index(head, i, data);
    std::cout << "Ingresa el caracter a buscar: ";
    std::cin >> u;
    std::cout << "Ingresa el nuevo valor: ";
    std::cin >> data;
    modify_an_item_by_char(head, u, data);
    print_list(head);
    count_list_elements(head);

    return 0;
}

void print_list(node_t *head) {
    node_t *current = head;

    while (current != NULL) {
        std::cout << current->data << std::endl;
        current = current->next;
    }
}

void count_list_elements(node_t *head) {
    node_t *current = head;
    int n = 0;

    while (current != NULL) {
        current = current->next;
        n += 1;
    }

    std::cout << "La lista tiene " << n << " elementos." << std::endl;
}

void modify_an_item_by_index(node_t *head, int i, char new_data) {
    node_t *current = head;

    for(int x = 0; x < i; x++) {
        current = current->next;
    }

    current->data = new_data;
}

void modify_an_item_by_char(node_t *head, char i, char new_data) {
    node_t *current = head;

    while (current != NULL) {
        if (current->data == i) {
            break;
        }
        current = current->next;
    }

    current->data = new_data;
}

Brief explanation: Observe how the functions that are responsible for modifying the values of a nodo first create a pointer to node called current and is a copy of the pointer to node head , is done in this way so as not to lose your head from the list head , and with current you can browse through the nodes until you find the node with the indicated index or character (depends on which of the two functions you use). Once the node is identified, we can access its values and modify them with the desired value.

As for counting the nodes, we have something very similar to the previous one but much simpler since you only need to go through the list of nodes through the pointer to node current (which is a copy of head ) and store the iterations within a variable and then display it on the screen.

When executing the program and passing some parameters, the following output / result is obtained:

List:
a
e
Ingresa el indice del elemento a modificar (comienza desde 0): 0
Ingresa el nuevo valor: o
Ingresa el caracter a buscar: e
Ingresa el nuevo valor: u
o
u
La lista tiene 2 elementos.

Note: Validations to avoid inappropriate behavior are all yours. I did not include them because it could complicate the understanding of the answer to your question.

    
answered by 11.09.2018 в 05:21
0

Since you have labeled the question as C ++, you should use the language correctly:

answered by 12.09.2018 в 08:07
0

I prepared the following code as an answer, and it does only and precisely what you ask in your question: It counts the number of elements that are in the list and can modify the elements that are inside *.

  

* As you did not specify if you wanted to modify them by index or by character search, I included both forms (please include more   details next time).

And here is the code. **

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    char data;
    struct node *next;
} node_t;

void print_list(node_t *);
void count_list_elements(node_t *);
void modify_an_item_by_index(node_t *, int, char);
void modify_an_item_by_char(node_t *, char, char);

int main(void) {
    int i;
    char u;
    char data;

    node_t *head = NULL;
    head = (node_t *)malloc(sizeof(node_t));

    head->data = 'a';
    head->next = (node_t *)malloc(sizeof(node_t));
    head->next->data = 'e';
    head->next->next = NULL;
    printf("List: \n");
    print_list(head);
    printf("Ingresa el indice del elemento a modificar (comienza desde 0): ");
    scanf("%d", &i);
    printf("Ingresa el nuevo valor: ");
    scanf(" %c", &data);
    modify_an_item_by_index(head, i, data);
    printf("Ingresa el caracter a buscar: ");
    scanf(" %c", &u);
    printf("Ingresa el nuevo valor: ");
    scanf(" %c", &data);
    modify_an_item_by_char(head, u, data);
    printf("New List: \n");
    print_list(head);
    count_list_elements(head);

    return 0;
}

void print_list(node_t *head) {
    node_t *current = head;

    while (current != NULL) {
        printf("%c\n", current->data);
        current = current->next;
    }
}

void count_list_elements(node_t *head) {
    node_t *current = head;
    int n = 0;

    while (current != NULL) {
        current = current->next;
        n += 1;
    }

    printf("La lista tiene %d elementos.\n", n);
}

void modify_an_item_by_index(node_t *head, int i, char new_data) {
    node_t *current = head;

    int x;

    for(x = 0; x < i; x++) {
        current = current->next;
    }

    current->data = new_data;
}

void modify_an_item_by_char(node_t *head, char i, char new_data) {
    node_t *current = head;

    while (current != NULL) {
        if (current->data == i) {
            break;
        }
        current = current->next;
    }

    current->data = new_data;
}
  

** I answered this question when I was in the C section, then the author or someone else changed it to C++ . I added another answer in C++ for this question (look it up).

The output (execution result) of the program when passing some parameters, is the following:

List:
a
e
Ingresa el indice del elemento a modificar (comienza desde 0): 0
Ingresa el nuevo valor: o
Ingresa el caracter a buscar: e
Ingresa el nuevo valor: u
New List:
o
u
La lista tiene 2 elementos.

One more note: The code does only what you asked for, the validations are all yours (to avoid inappropriate behavior), since I did not include them because the code would be complicated and it could be difficult to understand what you really need in response.

    
answered by 11.09.2018 в 04:31