我有点问题。每次尝试编译我的程序时,我都会收到“collect2:错误:ld 返回 1 个退出状态”。我用谷歌搜索了这个,查看了示例,发现它通常在没有正确使用头文件时发生;但是,我没有在我的代码中使用头文件,所以我不确定它为什么这样做。我认为这对其他遇到此错误但不明白原因的人会有所帮助。我确实知道我的代码中有很多错误,但我正试图找到可以编译它并解决所有问题的地方。非常感谢!
#include <stdio.h>
#include <stdlib.h>
struct node_t {
double x;
struct node_t *next;
};
struct node_t *create_node(double n);
void print_node (struct node_t * node );
void print_list (struct node_t * head );
struct node_t * insert_head (struct node_t *head , struct node_t * node );
struct node_t * insert_tail (struct node_t *head , struct node_t * node );
struct node_t * insert_middle (struct node_t *head , struct node_t *node , int pos );
int count_nodes (struct node_t * head );
struct node_t * delete_node (struct node_t *head , double n);
void delete_list (struct node_t * head );
int main (void)
{
double n;
int pos;
/** Asks the user what they would like to do. */
printf("Please select an option: ");
printf("1. Enter a number ");
printf("2. Delete a number ");
printf("3. Print all numbers ");
printf("4. Tell how many items in the list ");
printf("5. End program ");
printf("Please enter your choice (1, 2, 3, 4, 5): ");
while ((tmp = getchar()) != '\n')
in = tmp; /** Tells the compiler to use the number that the use put in. */
switch (in){
case '1': {/** If the user chooses Enter a Number, go through this process. */
printf("1. Enter item at head of list ");
printf("2. Enter item in middle of list ");
printf("3. Enter item at tail of list ");
printf("Please enter your choice (1,2,3): ");
while ((tmp = getchar()) != '\n')
in = tmp;
switch (in) {
case '1':{
printf("Enter the number to be entered at head: ");
fgets(s, 1024, stdin);
struct node_t * create_node (double n);
struct node_t *insert_head(struct node_t *head, struct node_t *node);
break;
}
case '2':{
printf("Enter the number to be entered in middle: ");
fgets(s, 1024, stdin);
struct node_t * create_node (double n);
struct node_t * insert_middle (struct node_t *head , struct node_t *node , int pos);
break;
}
case '3':{
printf("Enter the number to be entered at tail: ");
fgets(s, 1024, stdin);
struct node_t * create_node (double n);
struct node_t *insert_tail(struct node_t *tail, struct node_t *node);
break;
}
break;
}
}
case '2':{/** If the user selected Delete a Number, go through this process. */
printf("Enter the node that needs to be deleted: ");
fgets(s, 1024, stdin);
struct node_t * delete_node (struct node_t *head , double n);
break;
}
case '3':{/** If the user selected Print All Numbers, go through this process. */
//void print_list (struct node_t *head)
break;
}
case '4':{ /** If the user selected Tell How Many Items in the List, go through this process. */
int count_nodes (struct node_t * head );
break;
}
case '5':{ /** If the user selected End Program, go through this process. */
return 1;
break;
}
}
return 0;
}
/** Creates a new node
* @param double n, a number n
* @return pointer
*/
struct node_t * create_node (double n)
{
struct node_t *p = malloc (size of (struct node_t))
p -> x = n;
p -> next = NULL; /*initializes pointer to null*/
return p;
}
/** Prints the nodes
* @param struct node_t *node
*/
//void print_node (struct node_t *node )
//{
// prints out the node and the address of the node
// print(*node, &node)
//}
/** Prints list
* @param struct node_t *head
*/
//void print_list (struct node_t *head)
//{
// print contents of list. both the doubles and the address.
// while (pointer is pointing to something, keep going through the list. )
// print(nodes in list, address of nodes )
//}
/** Inserts a node at the head
* @param struct node_t *head, the structure where head is.
* @param struct node_t *node, the structure where node is.
* @return head.
*/
struct node_t *insert_head(struct node_t *head, struct node_t *node)
{
node-> next = head;
head = node;
return head;
}
/** Inserts a node at the tail.
* @param struct node_t *tail, the structure where tail is.
* @param struct node_t *node, the structure where node is.
* @return head.
*/
struct node_t *insert_tail(struct node_t *tail, struct node_t *node)
{
struct node_t *p = head;
if (head == NULL)
head = node;
return head;
while (p->next ! = NULL)
p = p-> next;
p-> next = node;
return head;
}
/** Inserts a node at the middle.
* @param struct node_t *head, the structure where head is.
* @param struct node_t *node, the structure where node is.
* @param int pos, the position of the current node.
* @return head.
*/
struct node_t * insert_middle (struct node_t *head , struct node_t *node , int pos);
{
int pos = 1;
struct node_t *p = head;
*head = pos, pos = 1;
if (pos > SIZE )
p-> next = node;
else if (pos < 1)
printf("Error\n");
return head;
}
/** Counts the number of nodes
* @param struct node_t *head, the structure where head is.
* @return count.
*/
int count_nodes (struct node_t * head );
{
struct node_t *p = head;
while(p->next ! = NULL)
return count;
if (head == NULL)
count = 0;
return count;
}
/** Deletes nodes
* @param struct node_t *head, the structure where head is.
* @param double n, a number n.
* @return head.
*/
struct node_t * delete_node (struct node_t *head , double n);
{
if (node == head)
free head;
p->next = head; //head now points to the pointer that was next after the node that was deleted
else if (node == middle)
free node;
p = p->next //pointer before node that was deleted now points to node that was after deleted node
else if (node == tail)
free p = p->next = node; //free the last node that holds data. The node before it now points to NULL
p->next = NULL;
else printf("Error\n")
return head;
}
/** Deletes list
* @param struct node_t *head, the structure where head is.
* @return head.
*/
void delete_list (struct node_t * head );
{
while((p->next ! = NULL)
free head; //This will lose all memory after head.
head == NULL;
if (head == NULL)
return 0;
return 0;
}