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):
#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.
if ((pos & lt; 1) || (pos & gt; = count + 1))
{
printf("\n Error: Position out of range to delete");
return;
}