Programming (c++) plz help me ,explain this problem Q.1. (Concatenating Linked L
ID: 668656 • Letter: P
Question
Programming (c++)
plz help me ,explain this problem
Q.1.(Concatenating Linked Lists) Write a program that concatenates two linked list objects of characters.The program should include function concatenate, which takes references to both list objects as arguments and concatenates the second list to the first list.
Q.2. (Summing and Averaging Elements in a Linked List) Write a program that inserts 25 random integers from 0 to 100 in order in a linked list object. The program should calculate the sum of the elements and the floating-point average of the elements.
Explanation / Answer
1.
#include<stdlib.h>
#include<stdio.h>
struct list_el {
char data;
struct list_el * next;
};
typedef struct list_el item;
// Function prototypes
void printList ( item * list );
void concatenate( item * list_A, item * list_B );
void main ( void ) {
item * list1, * list2, * head;
int i;
head = NULL;
// generate list of chars
for(i = 74; i >= 65; i--) {
list1 = (item *)malloc(sizeof(item));
list1->data = i;
list1->next = head;
head = list1;
}
head = NULL;
// generate a second list of chars
for(i = 84; i >= 75; i--) {
list2 = (item *)malloc(sizeof(item));
list2->data = i;
list2->next = head;
head = list2;
}
printf( "The first list is: " );
printList( list1 );
printf( "The second list is: " );
printList( list2 );
printf( "List 2 concatenated to List 1 is: " );
concatenate( list1, list2 );
printList( list1 );
return 0;
}
void printList ( item * list )
{
while(list!=NULL) {
printf("%c ", list->data);
list = list->next ;
}
printf( " " );
}
void concatenate( item * list_A, item * list_B )
{
while(list_A ->next != NULL) {
list_A = list_A->next ;
}
list_A ->next = list_B ; /* set next address of last struct in list_A to
the address of the beginning of list_B */
}
2
.#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Listnode {
int data;
struct Listnode *nextPtr;
}ListNode;
typedef ListNode *ListNodePtr;
int sumList(ListNodePtr a);
double averageList(ListNodePtr a);
void insert(ListNodePtr *sPtr, int value);
void printList(ListNodePtr currentPtr);
int main(void)
{
srand(time( NULL));
for (i=1;<=25; i++) {
insert(rand() %101);
)
printf("The list is: ");
printf("The sum is %d ");
printf("The average is%f ");
return 0;
}
int sumList(ListNodePtr a)
{
while ( currentPtr !=NULL) {
total +=currentPtr->data;
}
double averageList(ListNodePtr a)
{
while (currentPtr !=NULL){
++count;
total += currentPtr->data;
}
return total / count;
}
void insert(ListNodePtr *sPtr, int value)
{
}
void printList(ListNodePtr currentPtr)
{
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.