A complex number can be represented by two parts, real and image. A series compl
ID: 3557620 • Letter: A
Question
A complex number can be represented by two parts, real and image. A series complex data can be organized as a group of elements by linked list, which is shown as below: struct Complex
{
float real; float image;
struct Complex *next;
};
struct Complex* Generate (float a, float b)
{
// return one new Complex node
}
void Push (struct Complex **source, struct Complex *newNode)
{
// create linked list data by stack creation,
// insert the newNode into the head of source.
// (backward linked list creation)
}
void Print (struct Complex *source)
{
// print out all linked list data as the form, A+Bi,
// and the related distance, |A+Bi|
}
void FrontBackSplit (struct Complex* source, struct Complex** front, struct Complex** back)
{
// Split the nodes of the given list into front and back halves,
// and return the two lists using the reference parameters.
// If the length is odd, the extra node should go in the front list.
// You will probably need special case code to deal with the
// (length <2) cases.
}
void Sort (struct Complex **source)
{
// sort the data of linked list source in increasing order of
// complex number distance
}
struct Complex* AppendMergeReal (struct Complex* source1,
struct Complex* source2)
{
// Merge two complex linked lists (source1 and source2) into
// union of real part of source1 and source2.
// For example, real part of source1 are {1 2 3 3 5}, and real part
// of source2 are {2 3 5 8 8 9}, the merged result are {1 2 3 3 5 8 8 9}.
}
void RecursiveReverse (struct Complex** source)
{
// Recursively reverses the given linked list by changing its .next
// pointers and its head pointer in one pass of the list.
}
Please verify your program by following test code
int main()
{
float data1_real[11] = {3, 2, -5, -9, 7, 9, 4, -2, 4, -7, -7};
float data1_image[11] = {7, -8, -7, 5, -2, 4, 4, 3, -6, 7, 9};
struct Complex *newNode;
struct Complex *source1 = NULL, *dest = NULL;
struct Complex *front, *back; char c;
// Generate Complex data linked list with data1 for (int i=0; i<11; i++) {
newNode = Generate(data1_real[i], data1_image[i]); Push(&source1, newNode);
}
// print out complex data of source1 printf("Original data of source1: "); Print(source1);
// use FrontBackSplit() to divide source1 data into front part
// and back part, and print out these two split data FrontBackSplit(source1, &front, &back); printf(" Front part: ");
print(front); pritf(" Back part: "); print(back);
// sort input complex data according to the distance
// and print out sorted data of front and back individually printf(" Sorted front part: ");
Sort(&front); Print(front);
printf(" Sorted back part: "); Sort(&back);
Print(back);
printf(" ===================================================== ");
// sorted data according to real part
float
data2_real[6] = { 2,
3,
5,
9,
9, 9};
float
data2_image[6] = { 7,
-8,
-7,
5,
-2, 4};
float
data3_real[8] = { 3,
4,
5,
5,
6, 8, 9, 9};
float
data3_image[8] = {-8,
5,
-2,
4,
7, 9, 3, 4};
struct Complex *source2 = NULL, *source3 = NULL; struct Complex *result;
// Generate Complex data linked list with data2 and data3 for (int i=5; i>=0; i--) {
newNode = Generate(data2_real[i], data2_image[i]); Push(&source2, newNode);
}
for (int i=7; i>=0; i--) {
newNode = Generate(data3_real[i], data3_image[i]); Push(&source3, newNode);
}
// Verify AppendMergeReal() function and print out the result printf(" Original data of source2: ");
Print(source2);
printf(" Original data of source3: "); Print(source3);
printf(" Merged data of source2 and source3: "); result = AppendMergeReal(source2, source3); Print(result);
// Reverse merged data of source2 and source3 and print out the result printf(" Reverse test (merged data): "); RecursiveReverse(&result);
Print(result);
getch(); return 1;
}
float
data2_real[6] = { 2,
3,
5,
9,
9, 9};
float
data2_image[6] = { 7,
-8,
-7,
5,
-2, 4};
float
data3_real[8] = { 3,
4,
5,
5,
6, 8, 9, 9};
float
data3_image[8] = {-8,
5,
-2,
4,
7, 9, 3, 4};
Explanation / Answer
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
struct Complex
{
float real; float image;
struct Complex *next;
};
struct Complex* Generate (float a, float b)
{
Complex *t = new Complex;
t->real = a;
t->image = b;
return t;
}
void Push (struct Complex **source, struct Complex *newNode)
{
newNode->next = *source;
*(source) = newNode;
}
void Print (struct Complex *source)
{
Complex *p = source;
while(p)
{
cout<<p->real<<" "<<p->image<<" "<<sqrt(pow(p->real, 2)+ pow(p->image, 2))<<endl;
p = p->next;
}
}
void FrontBackSplit (struct Complex* source, struct Complex** front, struct Complex** back)
{
Complex *p = source;
int l = 0;
while(p)
{
l++;
p = p->next;
}
if( l >= 2)
{
int b_l = l/2;
int f_l = l - b_l;
l = 0;
*front = source;
p = *front;
while(l < f_l-1)
{
p = p->next;
l++;
}
*back = p->next;
p->next = NULL;
}
else
{
*front = source;
*back = NULL;
}
}
struct Complex* AppendMergeReal (struct Complex* source1, struct Complex* source2)
{
Complex * source3, * newNode;
Complex *t1;
t1 = (source1);
Complex *t2;
t2 = (source2);
while( t1 && t2)
{
int d1 = t1->real;
int d2 = t2->real;
if( d1 < d2)
{
newNode = Generate(t1->real, t1->image); Push(&source3, newNode);
t1 = (t1->next);
}
else if( d1 > d2)
{
newNode = Generate(t2->real, t2->image); Push(&source3, newNode);
t2 = (t2->next);
}
else
{
newNode = Generate(t1->real, t1->image); Push(&source3, newNode);
newNode = Generate(t2->real, t2->image); Push(&source3, newNode);
t1 = (t1->next);
t2 = (t2->next);
}
}
if(t1)
{
while(t1)
{
newNode = Generate(t1->real, t1->image); Push(&source3, newNode);
t1 = (t1->next);
}
}
else
{
while(t2)
{
newNode = Generate(t2->real, t2->image); Push(&source3, newNode);
t2 = (t2->next);
}
}
return source3;
}
void RecursiveReverse (struct Complex** source)
{
struct Complex* first;
struct Complex* rest;
if (*source == NULL)
return;
first = *source;
rest = first->next;
if (rest == NULL)
return;
RecursiveReverse(&rest);
first->next->next = first;
first->next = NULL;
*source = rest;
}
void Sort(Complex ** source)
{
if(*source == NULL || (*source)->next == NULL)
return;
Complex *front, *back;
FrontBackSplit (*source, &front, &back);
Sort(&front);
Sort(&back);
*source = AppendMergeReal (front, back);
}
int main()
{
float data1_real[11] = {3, 2, -5, -9, 7, 9, 4, -2, 4, -7, -7};
float data1_image[11] = {7, -8, -7, 5, -2, 4, 4, 3, -6, 7, 9};
struct Complex *newNode;
struct Complex *source1 = NULL, *dest = NULL;
struct Complex *front, *back; char c;
for (int i=0; i<11; i++) {
newNode = Generate(data1_real[i], data1_image[i]); Push(&source1, newNode);
}
printf("Original data of source1: "); Print(source1);
FrontBackSplit(source1, &front, &back); printf(" Front part: ");
Print(front); printf(" Back part: "); Print(back);
printf(" Sorted front part: ");
Sort(&front); Print(front);
printf(" Sorted back part: "); Sort(&back);
Print(back);
printf(" ===================================================== ");
float data2_real[6] = { 2,3,5,9, 9, 9};
float data2_image[6] = { 7,-8,-7,5,-2, 4};
float data3_real[8] = { 3,4,5,5,6, 8, 9, 9};
float data3_image[8] = {-8,5,-2,4,7, 9, 3, 4};
struct Complex *source2 = NULL, *source3 = NULL; struct Complex *result;
for (int i=5; i>=0; i--) {
newNode = Generate(data2_real[i], data2_image[i]); Push(&source2, newNode);
}
for (int i=7; i>=0; i--) {
newNode = Generate(data3_real[i], data3_image[i]); Push(&source3, newNode);
}
printf(" Original data of source2: ");
Print(source2);
printf(" Original data of source3: "); Print(source3);
printf(" Merged data of source2 and source3: "); result = AppendMergeReal(source2, source3); Print(result);
printf(" Reverse test (merged data): "); RecursiveReverse(&result);
Print(result);
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.