我的c程序中的链接器错误。该程序在双向链表中插入和删除。 [英] Linker error in my c program. The program is of insertion and deletion in doubly linked list.
问题描述
以下代码是在不同位置的双向链表中插入和删除的程序。它正在编译,但没有运行,因为它显示3个链接器错误。请尽快帮助。谢谢。问候。
The following code is a program for insertion and deletion in a doubly linked list at various positions. It is getting compiled, but not running as it shows 3 linker errors. Please help ASAP. Thankyou. Regards.
#include<alloc.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node {
struct node *prev;
int n;
struct node *next;
} *h, *temp, *temp1, *temp2, *temp4;
void create();
void insert_beg();
void insert_end();
void delete_pos();
void traverse();
int count = 0;
void main()
{
int ch;
clrscr();
printf("\n1.Create list\n2.Insert at beginning\n3.Insert at end\n4.Delete from given position\n5.Display");
while (1)
{
printf("\n Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
create_list();
break;
case 2:
insert_beg();
break;
case 3:
insert_end();
break;
case 4:
delete_pos();
break;
case 5:
traverse();
break;
default:
printf("\n Wong choice entered");
}
getch();
}
}
void create()
{
int data;
temp = (struct node *)malloc(1 * sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\n Enter the data: ");
scanf("%d", &data);
temp->n = data;
count++;
}
void insert_beg()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
void insert_end()
{
if (h == NULL)
{
create();
temp->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
void del_pos()
{
int i = 1, pos;
printf("\n Enterposition to be deleted: ");
scanf("%d", &pos);
temp2 = h;
if ((pos & lt; 1) || (pos & gt; = count + 1))
{
printf("\n Error: Position out of range to delete");
return;
}
if (h == NULL)
{
printf("\n Error: Empty iist");
return;
}
else
{
while (i & lt; pos)
{
temp2 = temp2->next;
i++;
}
if (i == 1)
{
if (temp2->next == NULL)
{
printf("Node deleted from list");
free(temp2);
temp2 = h = NULL;
return;
}
}
if (temp->next == NULL)
{
temp2->prev->next = NULL;
free(temp2);
printf("Node deleted from list");
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next;
if (i == 1)
h = temp2->next;
printf("\n Node deleted");
free(temp);
}
count--;
}
void diaplay()
{
temp2 = h;
if (temp2 == NULL)
{
printf("List empty to traverse\n");
return;
}
printf("\n Lined list is:\n ");
while (temp2->next != NULL)
{
printf("%d", temp2->n);
temp2 = temp2->next;
}
printf("%d", temp2->n);
getch();
}
推荐答案
一瞥你的代码就会显示你在循环中调用了许多函数而不是在你的程序中定义。您需要确保您的函数调用与您定义的函数的实际名称相匹配。
A glance at your code would show that you are calling a number of functions in your loop that are not defined in your program. You need to make sure your function calls match the actual names of your defined functions.
链接器会出现以下错误:
The linker gives the following errors:
[...] undefined reference to `create_list'
[...] undefined reference to `delete_pos'
[...] undefined reference to `traverse'
因为你正在调用这些函数但它们没有被定义(它们没有正文)。
Because you are calling such functions but they aren't defined (they have no body).
请注意要纠正的错误:
1.您将头文件拼错为alloc.h请将其更改为malloc.h
2.您已将该函数声明为void create() ;但是将函数称为
create_list();
3.声明和定义与void traverse()不匹配;和void显示()
4.在del_pos的定义部分,变量gt和lt未声明请声明它。
Please note the errors to be rectified:
1. you have misspelled the header file as alloc.h please change it to malloc.h
2.You have declared the function as void create(); but called the function as
create_list();
3. declaration and definition does not match void traverse(); and void display()
4.In the definition part of del_pos the variables gt and lt is not declared please declare it.
解决方案(错误已解决): < br $>
solution(errors rectified):
#include<malloc.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node {
struct node *prev;
int n;
struct node *next;
} *h, *temp, *temp1, *temp2, *temp4;
void create_list();
void insert_beg();
void insert_end();
void delete_pos();
void traverse();
int count = 0;
void main()
{
int ch;
system("cls");
printf("\n1.Create list\n2.Insert at beginning\n3.Insert at end\n4.Delete from given position\n5.Display");
while (1)
{
printf("\n Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
create_list();
break;
case 2:
insert_beg();
break;
case 3:
insert_end();
break;
case 4:
delete_pos();
break;
case 5:
traverse();
break;
default:
printf("\n Wong choice entered");
}
getch();
}
}
void create_list()
{
int data;
temp = (struct node *)malloc(1 * sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\n Enter the data: ");
scanf("%d", &data);
temp->n = data;
count++;
}
void insert_beg()
{
if (h == NULL)
{
create_list();
h = temp;
temp1 = h;
}
else
{
create_list();
temp->next = h;
h->prev = temp;
h = temp;
}
}
void insert_end()
{
if (h == NULL)
{
create_list();
temp->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
void delete_pos()
{
int i = 1, pos, gt,lt;
printf("\n Enterposition to be deleted: ");
scanf("%d", &pos);
temp2 = h;
if (h == NULL)
{
printf("\n Error: Empty iist");
return;
}
else
{
while (i & lt, pos)
{
temp2 = temp2->next;
i++;
}
if (i == 1)
{
if (temp2->next == NULL)
{
printf("Node deleted from list");
free(temp2);
temp2 = h = NULL;
return;
}
}
if (temp->next == NULL)
{
temp2->prev->next = NULL;
free(temp2);
printf("Node deleted from list");
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next;
if (i == 1)
h = temp2->next;
printf("\n Node deleted");
free(temp);
}
count--;
}
void traverse()
{
temp2 = h;
if (temp2 == NULL)
{
printf("List empty to traverse\n");
return;
}
printf("\n Lined list is:\n ");
while (temp2->next != NULL)
{
printf("%d", temp2->n);
temp2 = temp2->next;
}
printf("%d", temp2->n);
getch();
}
注意:
你在函数del_pos的if部分使用了未声明的变量。因为它只是一个错误消息我已经删除了它。如果你想要它,请声明变量gt&使用它。删除的部分在下面给出了您的身份证明。
Note:
you have used undeclared variables in the if part of the function del_pos.Since it is just an error message I have deleted it.If you want it kindly declare the variables gt & lt and use it.The deleted part is given below for your identification.
if ((pos & lt; 1) || (pos & gt; = count + 1))
{
printf("\n Error: Position out of range to delete");
return;
}
这篇关于我的c程序中的链接器错误。该程序在双向链表中插入和删除。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!