this is my code .can any one please tell me why the program is not giving proper results??
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
//2d linked list major aasg#1
//structure type
struct items
{
int id; //item no;
char name[];
int price;
char color[];
items *previous;
items *next;
};
//functions
sort();
show_all();
show();
delet_selectd();
insert_sorted();
dispose();
edit_record();
search_record();
cycle();
//gloabal varialble...
items *header=NULL;
items *footer=NULL;
items *current=NULL;
items *temp=NULL;
char ch='y';
int item_no;
//main function
main()
{
while(ch=='y'||ch=='Y')
{
clrscr();
int choice;
printf("\n**********menu**********\n");
printf("\nshow all records__________1\n\nshow specific record______2");
printf("\n\ndelet selectd record______3\n\ninsert any record_________4");
printf("\n\ndelet all records_________5\n\nedit any record___________6");
printf("\n\nenter your choice in no.s from given menu");
scanf("%d",&choice);
if(choice==1)
{
show_all();
cycle();
}
else if(choice==2)
{
show();
cycle();
}
else if(choice==3)
{
delet_selectd();
cycle();
}
else if(choice==4)
{
insert_sorted();
cycle();
}
else if(choice==5)
{
dispose();
cycle();
}
else if(choice==6)
{
edit_record();
cycle();
}
else
{
printf("invalid input");
cycle();
}
}
getch();
getch();
return(0);
}
//fuction definition
//to show all records
show_all()
{
int cos;
printf("in which order do u want to see records\n\nascending\t1\n\ndescending\t2");
cos=scanf("%d",cos);
if(cos==1) //print in ascending order
{
if (header==NULL)
{
printf("no records found");
}
else
{
current=header;
while(current!=NULL)
{
printf("item no is %d",current->id);
printf("\n");
printf("name is %s\n",current->name);
printf("color is %s\n",current->color);
printf("price is %d",current->price);
printf("\n");
current=current->next;
}
}
}
else if(cos==2) //print in decending order
{
if (footer==NULL)
{
printf("no records found");
}
else
{
current=footer;
while(current!=NULL)
{
printf("item no is %d",current->id);
printf("\n");
printf("name is %s\n",current->name);
printf("color is %s\n",current->color);
printf("price is %d",current->price);
printf("\n");
current=current->previous;
}
}
}
else
{
printf("invalid input");
}
return(0);
}
//to sort all records
sort()
{
int nodes=1;
temp=header;
while((temp->next)!=NULL)
{
temp=temp->next;
nodes++;
}
//bubblesort;
int outer, inner;
current=header;
temp=header->next;
for (outer = nodes - 1; outer > 0; outer--) { // counting down
for (inner = 0; inner < outer; inner++) { // bubbling up
if (current->id > temp->id) { // if out of order...
current->next=temp->next;
temp->next=current;
temp->previous=current->previous;
current->previous=temp;
//int temp = a[inner]; // ...then swap
//a[inner] = a[inner + 1];
//a[inner + 1] = temp;
}
current=temp;
temp=temp->next;
}
}
return(0);
}
//to show specific records
show()
{
search_record();
if(current==NULL&&item_no!=current->id){printf("no records found");return(0);}
printf("item no is %d",current->id);
printf("\n");
printf("%s",current->name);
printf("\n");
printf("%s",current->color);
printf("\n");
printf("price is %d",current->price);
printf("\n");
return(0);
}
//to delete selected records
delet_selectd()
{
search_record();
if(current==NULL&&item_no!=current->id){printf("no records found");return(0);}
temp=current->next;
temp->previous=current->previous;
temp=current->previous;
temp->next=current->next;
free(current);
return(0);
}
//to insert any record in sorted order
insert_sorted() //is just inserting a node not sorted
{
char item[20],pri[20];
current=(items *)malloc(sizeof (items));
printf("enter id or item no.");
gets(item);
gets(item);
current->id=atoi(item);
printf("enter name");
gets(current->name);
printf("enter color");
gets(current->color);
printf("enter price");
gets(pri);
current->price=atoi(pri);
current->next=NULL;
current->previous=NULL;
if(header==NULL&&footer==NULL)
{
header=current;
footer=current;
}
else
{
temp=header;
while((temp->next)!=NULL)
{
temp=temp->next;
}
temp->next=current;
temp=footer;
while((temp->previous)!=NULL)
{
temp=temp->previous;
}
current->previous=footer;
footer=current;
//sort();
}
return(0);
}
//__to delete all records
dispose()
{
current=header;
while (current!=NULL)
{
temp=current->next;
free(current);
current=temp;
}
printf("all records deleted");
header=NULL;
footer=NULL;
return(0);
}
//to edit any record which is already saved
edit_record()
{
search_record();
if(current==NULL&&item_no!=current->id){printf("no records found");return(0);}
//printf("enter id or item no.");
//scanf("%d",current->id);
printf("unedited are:");
puts(current->name);
printf("\n");
puts(current->color);
printf("\n");
printf("price is %d",current->price);
printf("\n");
printf("start editing");
char item[20],pri[20];
printf("enter id or item no.");
gets(item);
gets(item);
current->id=atoi(item);
printf("enter name");
gets(current->name);
printf("enter color");
gets(current->color);
printf("enter price");
gets(pri);
current->price=atoi(pri);
return(0);
}
//to search any record
search_record()
{
int item_no;
printf("enter id u want to search");
scanf("%d",item_no);
current=header;
while (current!=NULL)
{
if(item_no==current->id)
{break;}
current=current->next;
}
return(0);
}
//to execute program again
cycle()
{
printf("\n\ndo you want to restart the program(y/n)");
ch=getch();
if(ch!='y'&&ch!='Y')
{
exit(1);
}
return(0);
}