can i have the code for the whole program, please!!!
Printable View
can i have the code for the whole program, please!!!
This is what I've done so far. Can you please tell me what is wrong with the code?
The reverse function is only printing the last element of the list and the delete function has an error on the declaration part.
PLEASE HELPCode://Do not forget to free memory
#include "stdio.h"
struct details* remove(struct details *dlptr,int pos,int *flag);
void reverse(struct details *rpoint);
struct details *find(int n);
void display(struct details *dpoint);
void insert(struct details *ipoint,int position);
struct details
{
char name[25];
struct details *prev, *next;
};
struct details *start, *point, *prior;
void main()
{
int option,pstn,s;
clrscr();
start=point=prior=NULL;
do
{ clrscr();
printf("\n\n\t MAIN MENU\n");
printf("\n1\t Create a list");
printf("\n2\t Insert");
printf("\n3\t Delete");
printf("\n4\t Find");
printf("\n5\t Display list");
printf("\n6\t Reverse List");
printf("\n7\t Exit program\n\n Enter your choice\t");
scanf("%d",&option);
switch (option)
{
case 1: { /********create********/
char c;
clrscr();
start=(struct details *)malloc(sizeof(struct details));
printf("\n Name:\t\t");
scanf("%s",start->name);
prior=start;
start->prev=start->next=NULL;
printf("\n\n Press q to go back to the MAIN NENU\n");
if((c=getch())=='q')
break;
else
{
do
{ clrscr();
point = (struct details *)malloc(sizeof(struct details));
printf("\n Name:\t\t");
scanf("%s",point->name);
prior->next = point; /* point last "next" to this record */
point->prev = prior; /* point record's prev to prior */
point->next = NULL; /* point this "next" to NULL */
prior = point; /* this is now the prior record */
printf("\n\n Press q to go back to the MAIN NENU");
c=getch();
} while( c != 'q' );
}
break;
}
case 2: {
/*********INSERT**********/
clrscr();
point=(struct details*)malloc(sizeof(struct details));
printf("Enter name to be added to thr list\n\t");
scanf("%s",point->name);
printf("\n\n Insert \"%s\" on which position? ",point->name);
scanf("%d",&pstn);
point->next=point->prev=NULL;
insert(point,pstn);
break;
}
case 3: {
/********DELETE*********/
clrscr();
printf("Enter position of name to be deleted. ");
scanf("%i",&pstn);
point=remove(start,pstn,&s);
if(s==1)
printf("\n\n\"%s\" deleted successfuly",point->name);
break;
}
case 4: {
/*********FIND**********/
printf("\n\nEnter position to find\t");
scanf("%d",&s);
point=find(s);
printf("On position %d is \"%s\"",s,point->name);
getch();
break;
}
case 5: {
display(start); break;
}
case 6: {
display(prior); break;
}
default: break;
}
} while( option != 7);
printf(" -=-=-=-=-=-The end!!!-=-=-=-=-=-");
getch();
}
/*********************************DISPLAY**************************************/
void display(struct details *dpoint)
{
int i=0;
clrscr();
if(dpoint==NULL)
printf("\tList is empty");
else
{
printf(" List contains:\n\n");
while(dpoint != NULL)
{
i++;
printf("%d\t%s\n",i,dpoint->name);
dpoint=dpoint->next;
}
} getch();
}
/*********************************FIND***************************************/
struct details* find(int n)
{
int a;
struct details *ptr;
ptr=start;
clrscr();
if(ptr==NULL)
return NULL;
else
{
for(a=1; a<n; ++a)
ptr=ptr->next;
return ptr;
}
}
/*****************************INSERT***************************************/
struct details *hold1,*hold2;
void insert(struct details *ipoint,int position)
{
if(ipoint==NULL)
return;
else if(position==1)
{
ipoint->next=start;
start->prev=ipoint;
start=ipoint;
}
else {
hold2=find(position);
hold1=find(position-1);
if( (hold2->next==NULL) && (hold1->next!=NULL) )
{
ipoint->prev=hold1;
hold1->next=ipoint;
}
else {
ipoint->prev=hold1;
ipoint->next=hold2;
hold2->prev=ipoint;
hold1->next=ipoint;
}
}
printf("\n\n\t\t-=-=-=-=-=-SUCCESS!!!-=-=-=-=-=-");
getch();
}
/**************************DELETE**************************************/
struct details* remove(struct details *dlptr,int pos,int *flag)
{
dlptr=find(pos);
if(dlptr==NULL)
{
*flag=0;
return NULL;
}
else {
hold1=dlptr->prev;
hold2=dlptr->next;
if(pos==1)
{
start=hold2;
start->prev=NULL;
}
else if(hold2==NULL)
hold1->next=NULL;
else {
hold1->next=hold2;
hold2->prev=hold1;
}
hold1=dlptr;
free(dlptr);
*flag=1;
return hold1;
}
}
/********************REVERSE**********************/
void reverse(struct details *rpoint)
{
int i=0;
clrscr();
if(rpoint==NULL)
printf("\tList is empty");
else
{
printf(" The reversed List is :\n\n");
while(rpoint != NULL)
{
i++;
printf("%d\t%s\n",i,rpoint->name);
rpoint=rpoint->prev;
}
} getch();
}
You never call the reverse() function from anywhere.
Note also that your insert is broken (just try to insert an element at the back of the list -- which crashes -- or in the middle of the list -- which severs the list since it doesn't set all the pointers).