Linked list operation in c....what's wrong with my code? Can anyone help?


#1

#include <stdio.h>
#include <stdlib.h>
void create(); //prototype
void display();
void insert_beginning();
void insert_end();
void insert_specific_position();
void del_beginning();
void del_end();
void del_specific_position();
typedef struct node
{
int data;
struct node *next;
}node;
node *head=NULL;
main()
{
int select;
while(1){
printf(“Linked List Operations\n\n”);
printf(“1.Create a linked list\n”);
printf(“2.Display the list\n”);
printf(“3.Insert at the beginning\n”);
printf(“4.Insert at the end\n”);
printf(“5.Insert at specified position\n”);
printf(“6.Delete from beginning\n”);
printf(“7.Delete from the end\n”);
printf(“8.Delete from specified position\n”);
printf(“9.Exit\n”);
printf(“Select one(1-9)”);
scanf("%d",&select);
switch(select)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_beginning();
break;
case 4:
insert_end();
break;
case 5:
insert_specific_position();
break;
case 6:
del_beginning();
break;
case 7:
del_end();
break;
case 8:
del_specific_position();
break;
case 9:
exit(0);
break;
default:
printf(“Wrong Input!!!\n”);
break;
}
}
}
void create()
{
node *temp, temp1;
temp=(node
)malloc(sizeof(node));
printf(“Enter the data for node\n”);
scanf("%d", &(temp->data));
temp->next=NULL; //protita individual node er sheshe NULL hobe…
if(head==NULL) //jodi r kono node na thake…
{
head=temp;
}
else
{
temp1=head;
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
}
}
void display()
{
node *temp1;
if(head==NULL)
{
printf(“Linked list is empty…\n”);
}
else
{
temp1=head; //for printing the elements of the list
while(temp1!=NULL)
{
printf("%d->", temp1->data);
temp1=temp1->next;
}
}
void insert_beginning()
{
node temp;
temp=(node
)malloc(sizeof(node));
if(temp==NULL)
{
printf(“Can’t allocate the memory\n”);
return;
}
printf(“Enter value for 1st the node\n”);
scanf("%d", &(temp->data));
temp->next=NULL;
if(head==NULL) //jodi r kono node na thake…
{
head=temp;
}
else
{
temp->next=head;
head=temp;
}
}
void insert_end()
{
node *temp, temp1;
printf("\n\nInsert a node at the END\n");
temp=(node
)malloc(sizeof(node));
if(temp==NULL)
{
printf(“Can’t allocate the memory\n”);
return;
}
printf(“Enter value for the last node\n”);
scanf("%d", &(temp->data));
temp->next=NULL;
if(head==NULL) //jodi r kono node na thake…
{
head=temp;
}
else
{
temp1=head;
while(temp1->next!=NULL)
{

            temp1=temp1->next;
        }
        temp1->next=temp;
    }

}
void insert_specific_position()
{
int i, n;
node *temp, temp1;
printf("\n\nInsert a node at the specified position\n");
temp=(node
)malloc(sizeof(node)); //single isolated node
if(temp==NULL)
{
printf(“Can’t allocate the memory\n”);
return;
}
printf(“Where the node to be inserted?\n”);
scanf("%d", &n);
printf(“Enter value for the node\n”);
scanf("%d", &(temp->data));
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
temp1=head;
for(i=1; i<n-1;i++) //jodi i<n hoi tahole next position e node create hobe bt i<n-1 hole borabor position e node create hobe
{
temp1=temp1->next;
}
temp->next=temp1->next;
temp1->next=temp;
}
}
void del_beggining()
{
node *temp;
printf("\n\nDelete a node from Beginning\n");
if(temp==NULL)
{
printf("\nList is Empty:\n");
return;
}
else
{
temp=head;
head=temp->next;
free(temp);
}
}
void del_end()
{
node *temp, *temp1;
printf("\n\nDelete a node from END\n");
if(head==NULL)
{
printf("\nList is Empty:");
}
else
{
temp1=head;
while(temp1->next!=NULL)
{
temp=temp1;
temp1=temp1->next;
}
temp->next=NULL;
free(temp1);
}
}
void del_specific_position()
{
int i, n;
node *temp, *temp1;
printf("\n\nDelete a node from a Specific position\n");
if(head==NULL)
{
printf("\nList is Empty:");
}
else
{
printf(“Which node to be deleted?\n”);
scanf("%d", &n);
temp1=head;
for(i=1; i<n;i++)
{
temp=temp1;
temp1=temp1->next;
}
temp->next=temp1->next;
free(temp1);
}
}