arunks
Hey here is the aks
I m writing a program for various operations on linked list in C... Program is given below.. and i m getting one error and two warnings..
Plz see by executing it and help me to remove it and run the program successfully...
Plz see by executing it and help me to remove it and run the program successfully...
Code:
#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *next;
};
void create_list(struct node **head)
{
*head = (struct node *) NULL;
}
struct node *search(struct node *head,int item)
{
while(head!=(struct node *) NULL)
{
if(head->info==item)
return head;
head=head->next;
}
return NULL;
}
void insert_at_beginning(struct node *head,int item)
{
struct node *ptr;
ptr=(struct node *) malloc (sizeof(struct node));
ptr->info=item;
if(head==(struct node *)NULL)
{
ptr->next=(struct node *)NULL;
}
else
ptr->next=head;
head=ptr;
}
void insert_at_end(struct node *head,int item)
{
struct node *ptr;
ptr=(struct node * )malloc(sizeof(struct node));
ptr->info=item;
ptr->next=(struct node *)NULL;
if(head==(struct node *)NULL)
{
head=ptr;
}
else
{
while(head->next!=(struct node *)NULL)
{
head=head->next;
}
head->next=ptr;
}
}
void insert_after_element(struct node *head,int item,int after)
{
struct node *ptr,*loc;
loc =(struct node *) search(head,after);
if(loc== ( struct node * ) NULL )
{
printf("no element specified is there");
return;
}
ptr=(struct node *)malloc(sizeof(struct node));
ptr->info=item;
ptr->next=loc->next;
loc->next=ptr;
}
void traverse_list(struct node *head)
{
while(head!= (struct node * ) NULL)
{
printf("%d\n",head->info);
head=head->next;
}
}
void delete_at_beginning(struct node *head)
{
struct node *ptr;
if(head==(struct node *)NULL)
return;
else
{
ptr=head;
head= head->next;
free(ptr);
}
}
void delete_at_end(struct node *head)
{
struct node *ptr,*loc;
if(head== (struct node *)NULL)
return;
else
if(head->next==(struct node *)NULL)
{
ptr=head;
head=(struct node *)NULL;
free(ptr);
}
else
{
loc=head;
ptr=head->next;
while(ptr->next!= (struct node *)NULL)
{
loc=ptr;
ptr=ptr->next;
}
loc->next=(struct node *) NULL;
free(ptr);
}
}
void delete_after_element(struct node *head,int after)
{
struct node *ptr, *loc;
loc=search(head,after);
if(loc==(struct node *)NULL)
return;
ptr=loc->next;
loc->next=ptr->next;
free(ptr);
}
void delete_list(struct node *head)
{
struct node *ptr;
while(head != (struct node *)NULL)
{
ptr=head;
head=head->next;
free(ptr);
}
}
main()
{
struct node *head;
int choice,item,after;
clrscr();
printf("Linked List Program\n\n\n Press any key to start!!!!!!");
while(1)
{
clrscr();
printf("Linked List Program\n\n\n");
printf("1. Create a new linked list\n\n");
printf("2. Insert an element at the beginning\n\n");
printf("3. Insert an element after an element\n\n");
printf("4. Insert an element at end\n\n");
printf("5. Traverse all the elements of linked list\n\n");
printf("6. Search an element in linked list\n\n");
printf("7. Delete an element at the beginning\n\n");
printf("8. Delete an element after an element\n\n");
printf("9. Delete an element at end\n\n");
printf("10.Exit\n\n");
printf("\n\nEnter the choice:(1-10)");
scanf("%d",&choice);
if(choice<1 || choice>9)
{
clrscr();
printf("enter correct choice\n");
}
else
if(choice==1)
{
create_list(&head);
printf("empty List created\n");
}
else
if(choice==2)
{
printf("Enter the no. to store\n");
scanf("%d",&item);
insert_at_beginning(head,item);
}
else
if(choice==3)
{
printf("Enter the no. to store\n");
scanf("%d",&item);
printf("Enter no. after which %d is to be stored\n",item);
scanf("%d",&after);
insert_after_element(head,item,after);
}
else
if(choice==4)
{
printf("Enter the no. to store\n");
scanf("%d",&item);
insert_at_end(head,item);
}
else
if(choice==5)
{
if(head==(struct node *)NULL)
printf("\n List is empty......");
else
traverse_list(head);
printf("\nPress any ley to continue.......");
getch();
}
else
if(choice==7)
{
delete_at_beginning(head);
}
else
if(choice==8)
{
printf("Enter the no. after which item is to be deleted\n");
scanf("%d",&after);
delete_after_element(head,after);
}
else
if(choice==9)
{
delete_at_end(head);
}
else
if(choice==10)
{
delete_list(head);
exit(1);
}
}
getch();
}
Last edited: