Criação de uma lista encadeada seguida da inversão de seus elementos. Escreva um programa que crie uma lista encadeada de 10 caracteres, e depois crie uma cópia da lista em ordem reversa.
Passo 1
Neste exercício, precisamos escrever um programa que crie uma lista encadeada de 1010 caracteres e os copie na segunda lista na ordem inversa. Para fazer isso, precisamos escrever uma função reverse. A função reverse leva o ponteiro para o primeiro elemento da lista como argumento. A função percorre a lista original do início ao fim e copia cada nó como o primeiro nó da segunda lista. Desta forma, obtemos a ordem inversa. A definição da função é
struct node* reverse(struct node *firstElement){
struct node *secondList,*current,*p;
secondList = NULL;
/*Go through all elements of the first list*/
while(firstElement != NULL){
/*Create node*/
p = malloc(sizeof(struct node));
/*Add value to the node*/
p->data = firstElement->data;
p->next = NULL;
/*If second list is empty, add
*the node to the list
*/
if(secondList == NULL){
secondList = p;
}else{
/*If there are other nodes in the list, add
*the node from the first list to the beginning
*of the second list
*/
p->next = secondList;
secondList = p;
}
firstElement = firstElement->next;
}
return secondList;
}
Passo 2
O programa a seguir cria uma lista vinculada de 1010 caracteres e os copia na lista vinculada na ordem inversa. Para criar a lista original, o programa utiliza a função createList.
#include
#include
/*Create node structure*/
struct node{
char data; /*Node data*/
struct node *next; /*pointer to the next node*/
};
/*Prototypes*/
struct node* createList();
struct node* reverse(struct node *firstElement);
int main(void){
struct node *firstList,*secondList,*p;
firstList = createList();
secondList = reverse(firstList);
/*Print out the first list*/
p = firstList;
while(p != NULL){
printf("%c ", p->data);
p = p->next;
}
printf("\n");
/*Print out the second list*/
p = secondList;
while(p != NULL){
printf("%c ", p->data);
p = p->next;
}
return 0;
}
struct node* createList(){
struct node *firstElement,*p,*previous;
for(int i = 0; i
/*Create node*/
p = malloc(sizeof(struct node));
/*Add random char to the node*/
p->data = 'a'+rand()%26;
/*the node does not link to another*/
p->next = NULL;
if(firstElement == NULL){
/*If the list is empty, set first
*node to node p
*/
firstElement = p;
}else{
/*If list is not empty, add node to
*the end of the list
*/
previous->next = p;
}
previous = p;
}
return firstElement;
}
struct node* reverse(struct node *firstElement){
struct node *secondList,*current,*p;
secondList = NULL;
/*Go through all elements of the first list*/
while(firstElement != NULL){
/*Create node*/
p = malloc(sizeof(struct node));
/*Add value to the node*/
p->data = firstElement->data;
p->next = NULL;
/*If second list is empty, add
*the node to the list
*/
if(secondList == NULL){
secondList = p;
}else{
/*If there are other nodes in the list, add
*the node from the first list to the beginning
*of the second list
*/
p->next = secondList;
secondList = p;
}
firstElement = firstElement->next;
}
return secondList;
}
Passo 3
A saída do programa fornecido
p h q g h u m e a y
y a em u h g q h p
Resposta
Ver o desenvolvimento