Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C++
#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();
}
Posted
Updated 3-Oct-15 7:22am
v4
Comments
OriginalGriff 3-Oct-15 11:53am    
What are the errors?
Copy and paste the error messages!

Use the "Improve question" widget to edit your question and provide better information.
OriginalGriff 3-Oct-15 11:55am    
And do yourself a favour: I went to edit your program to add a code block to preserve the formatting and indentation to make it more readable. And...it doesn't have any.
Indent your code! It makes it a lot, lot easier to see what is going on, what starts where, what ends where.
Most modern IDE's will do it automatically for you!
PIEBALDconsult 3-Oct-15 13:23pm    
Could it have been due to a missing n in the first #include ?
KarstenK 3-Oct-15 13:45pm    
And what are the linker errors???

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.
 
Share this answer
 
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).
 
Share this answer
 
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.


Quote:
solution(errors rectified):

C#
#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();
}




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.

C#
if ((pos & lt; 1) || (pos & gt; = count + 1))
    {
        printf("\n Error: Position out of range to delete");
        return;
    }
 
Share this answer
 
v5
Comments
Richard MacCutchan 4-Oct-15 8:38am    
Please do not SHOUT!
[no name] 4-Oct-15 9:06am    
Can You please say me why my answer is down-voted so that I can rectify it.
Richard MacCutchan 4-Oct-15 9:55am    
Don't use all capitals in messages, it is equivalent to shouting; use proper casing. Also do not just rewrite someone's code as a solution. Explain what the problem is and how to resolve it. People learn much more by trying to follow advice than just copying code from the internet.
[no name] 4-Oct-15 11:33am    
Thank you so much I will improve my solution.Pardon me since English is not my First language and I did not copy the code from the internet I copied the code from his question and pasted on my compiler(code blocks) and rectified the errors in it.
Richard MacCutchan 4-Oct-15 11:42am    
I mean that it does not help the questioner when you provide just the complete code for them to copy. You should just show the corrections and explain why they will solve their problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900