Chetan Malhotra

Chetan Malhotra

  • NA
  • 23
  • 6.7k

C program error

Oct 2 2015 3:16 AM
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.
 
 
#inlude<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<1) || (pos>=count + 1))
{
printf("\n Error: Position out of range to delete");
return;
}
if(h==NULL)
{
printf("\n Error: Empty iist");
return;
}
else
{
while(i < 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();
}

Answers (3)