I am using the file ex3test.c to run the following program, but the UNIX gives the errors:
[38] $ gcc -o ex3test ex3test.c
Undefined first referenced
symbol in file
discrete_uniform /tmp/scratch.hYayTQ/ccRDoT1o.o
printFirst /tmp/scratch.hYayTQ/ccRDoT1o.o
addToList /tmp/scratch.hYayTQ/ccRDoT1o.o
printSecond /tmp/scratch.hYayTQ/ccRDoT1o.o
disparity /tmp/scratch.hYayTQ/ccRDoT1o.o
clearList /tmp/scratch.hYayTQ/ccRDoT1o.o
ld: fatal: Symbol referencing errors. No output written to ex3test
collect2: ld returned 1 exit status
how can I fix this?
#include <stdio.h>
#include <stdlib.h>
/* Step 1: define your struct here */
typedef struct _Node {
int data1, data2;
struct _Node* next;
struct _Node* next2;
} Node;
typedef struct _List {
Node* front;
Node* front2;
Node* rear;
Node* rear2;
} List;
static List list;
/* Step 2: put code in the functions below to make the list work. */
void clearList ()
{
while (list.front)
{
Node* tmp = list.front;
list.front = list.front->next;
free(tmp);
}
list.front = list.front2 = list.rear = list.rear2 = NULL;
}
void addData1(Node* newNode, int data1)
{
Node* prev = NULL;
Node* iter = list.front;
newNode->data1 = data1;
while (iter && iter->data1 < data1)
{
prev = iter;
iter = iter->next;
}
if (list.front == NULL)
{
list.front = list.rear = newNode;
newNode->next = NULL;
}
else
if (prev == NULL)
{
newNode->next = list.front;
list.front = newNode;
}
else {
newNode->next = iter;
prev->next = newNode;
if (prev == list.rear)
list.rear = newNode;
}
}
void addData2(Node* newNode, int data2)
{
Node* prev = NULL;
Node* iter = list.front2;
newNode->data2 = data2;
while (iter && iter->data2 < data2)
{
prev = iter;
iter = iter->next2;
}
if (list.front2 == NULL)
{
list.front2 = list.rear2 = newNode;
newNode->next2 = NULL;
}
else
if (prev == NULL)
{
newNode->next2 = list.front2;
list.front2 = newNode;
}
else {
newNode->next2 = iter;
prev->next2 = newNode;
if (prev == list.rear2)
list.rear2 = newNode;
}
}
void addToList (int data1, int data2)
{
Node* newNode = (Node*)malloc(sizeof(Node));
addData1(newNode, data1);
addData2(newNode, data2);
}
void printFirst ()
{
Node* iter = list.front;
while (iter)
{
printf("(%d %d)\n", iter->data1, iter->data2);
iter = iter->next;
}
}
void printSecond ()
{
Node* iter = list.front2;
while (iter)
{
printf("(%d %d)\n", iter->data1, iter->data2);
iter = iter->next2;
}
}
int findPos2(int data1, int data2)
{
int pos = 0;
Node* iter = list.front2;
while (iter && !(iter->data1 == data1 && iter->data2 == data2))
{
++pos;
iter = iter->next2;
}
return pos;
}
int disparity ()
{
int result = 0;
Node* iter = list.front;
int pos = 0;
while (iter)
{
result += abs(findPos2(iter->data1, iter->data2) - pos);
iter = iter->next;
++pos;
}
return result;
}