I have to ask the user a question on what number they would like to search for in the list. The code compiles fine but when I run it, nothing shows up for the user to ask a question. here is the code:
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next, *prev;
};
typedef struct Node* Box;
Box search_number(Box head, Box tail);
int main()
{
//pointers capable of pointing to nodes.
struct Node *head, *temp, *tail, *above,*below, *del;
int min, max, target;
//first node
temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=9;
temp->next=NULL;
temp->prev=NULL;
//placing head
head=temp;
//second node
temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=3;
temp->next=NULL;
temp->prev=NULL;
//connections
head->next=temp;
temp->prev=head;
//third node
temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=10;
temp->next=NULL;
temp->prev=NULL;
//connections
head->next->next=temp;
temp->prev=head->next;
//fourth node
temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=6;
temp->next=NULL;
temp->prev=NULL;
//connections
head->next->next->next=temp;
temp->prev=head->next->next;
//fifth node
temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=16;
temp->next=NULL;
temp->prev=NULL;
//connections
head->next->next->next->next=temp;
temp->prev=head->next->next->next;
//sixth node
temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=8;
temp->next=NULL;
temp->prev=NULL;
//connections
head->next->next->next->next->next=temp;
temp->prev=head->next->next->next->next;
//placing the tail
tail=temp;
printf("\n");
printf("\n Printing the list \n");
for(temp=head; temp!=NULL; temp=temp->next)
printf("%d, ",temp->data);
printf("\n");
printf("\n Printing the list bakwards \n");
for(temp=tail; temp!=NULL; temp=temp->prev)
printf("%d, ",temp->data);
printf("\n");
//finding min --start from the top
min=head->data;
for(temp=head; temp!=NULL; temp=temp->next)
if(temp->data < min)
min=temp->data;
printf("min is %d \n",min);
printf("\n");
//finding max --start from the bottom
max=tail->data;
for(temp=tail; temp!=NULL; temp=temp->prev)
if(temp->data > max)
max=temp->data;
printf("max is %d \n",max);
printf("\n");
//deleting the node with number 10
//placing pointers del, above, below
del=head->next->next;
above=del->prev;
below=del->next;
//making new connections
above->next=below;
below->prev=above;
//free memory
free(del);
printf("\n Printing updated list \n");
for(temp=head; temp!=NULL; temp=temp->next)
printf("%d, ",temp->data);
printf("\n");
//deleting the first node in the list
del=head;
head=head->next;
free(del);
head->prev=NULL;
printf("\n Printing updated list \n");
for(temp=head; temp!=NULL; temp=temp->next)
printf("%d, ",temp->data);
printf("\n");
printf("\n Printing updated list bakwards \n");
for(temp=tail; temp!=NULL; temp=temp->prev)
printf("%d, ",temp->data);
printf("\n");
//deleting the last node in the list
del=tail;
tail=tail->prev;
free(del);
tail->next=NULL;
printf("\n Printing updated list \n");
for(temp=head; temp!=NULL; temp=temp->next)
printf("%d, ",temp->data);
printf("\n");
return 0;
}
Box search_number(Box head, Box tail)
{
char target[20];
char found='N';
Box here, above, below;
printf("What number would you like to search for?\n");
scanf("%s", target);
here=head;
while(here!=NULL && found !='Y')
{
if(strcmp(here->next, target)==0)
found='Y';
else
here=here->next;
}
if(found=='N')
{
printf("Sorry, I cannot find that number. \n\n");
}
if(found=='Y')
{
printf("The number %s is in the list. \n\n", here->next);
if(here==head)
{
head=head->prev;
head->prev=here;
free(here);
}
else
{
if(here!=head && here!=tail)
{
above=head->next;
below=head->prev;
above->next=here;
below->prev=here;
free(here);
}
else
if(here==tail)
{
tail=tail->next;
tail->next=here;
free(here);
}
}
}
return head;
}
and here is the code for asking the question, it is at the bottom of the original code.
Box search_number(Box head, Box tail)
{
char target[20];
char found='N';
Box here, above, below;
printf("What number would you like to search for?\n");
scanf("%s", target);
here=head;
while(here!=NULL && found !='Y')
{
if(strcmp(here->next, target)==0)
found='Y';
else
here=here->next;
}
if(found=='N')
{
printf("Sorry, I cannot find that number. \n\n");
}
if(found=='Y')
{
printf("The number %s is in the list. \n\n", here->next);
if(here==head)
{
head=head->prev;
head->prev=here;
free(here);
}
else
{
if(here!=head && here!=tail)
{
above=head->next;
below=head->prev;
above->next=here;
below->prev=here;
free(here);
}
else
if(here==tail)
{
tail=tail->next;
tail->next=here;
free(here);
}
}
}
return head;
}