Question
//C program //replace the question marks in the template and //write the code for adding the nodes #include #include struct Node { struct Node *next,
//C program
//replace the question marks in the template and
//write the code for adding the nodes
#include
#include
struct Node
{
struct Node *next, *prev;
int data;
};
void print_list(struct Node *head);
void print_backwards(struct Node *head, struct Node *tail);
void sort_list(struct Node *head);
void search_list (struct Node *head);
struct Node* search_delete (struct Node *head, struct Node *tail);
struct Node* place_tail(struct Node *head);
struct Node* add_node(struct Node *head, struct Node *tail);
void main()
{
struct Node *temp, *head, *tail;
int size, i;
printf("How many numbers do you want to enter? ");
scanf("%d", &size);
printf("Enter %d numbers ", size);
temp=(struct Node*)malloc(sizeof(struct Node));
scanf("%d", &temp->data);
temp->next=NULL;
temp->prev=NULL;
head=temp;
tail=temp;
for(i=0; i
{
temp=(struct Node*)malloc(sizeof(struct Node));
scanf("%d", &temp->data);
temp->next=NULL;
temp->prev=NULL;
tail->next=temp;
temp->prev=tail;
tail=tail->next;
}
print_list(head);
print_backwards(head, tail);
tail=place_tail(head);
print_list(head);
print_backwards(head, tail);
//sort_list(head);
print_list(head);
search_list(head);
head=search_delete(head, tail);
tail=place_tail(head);
print_list(head);
print_backwards(head, tail);
head= add_node(head, tail);
tail=place_tail(head);
print_list(head);
print_backwards(head, tail);
printf("Sorting the list ");
sort_list(head);
print_list(head);
print_backwards(head, tail);
}
void print_list(struct Node *head)
{
struct Node *temp;
printf("Printing the list ");
for(temp=head; temp!=NULL; temp=temp->next)
printf("%d ", temp->data);
printf(" ");
}
void print_backwards(struct Node *head, struct Node *tail)
{
struct Node *temp;
printf("Printing the list backwards ");
for(temp=tail; temp!=NULL; temp=temp->prev)
printf("%d ", temp->data);
printf(" ");
}
void sort_list(struct Node *head)
{
struct Node *temp, *temp1;
int storage;
printf("Sorting the list ");
for(temp1=head; temp1->next!=NULL; temp1=temp1->next)
for(temp=head; temp->next!=NULL; temp=temp->next)
{
if(temp->data > temp->next->data)
{
storage=temp->data;
temp->data=temp->next->data;
temp->next->data=storage;
}
}
}
void search_list (struct Node *head)
{
int found=0;
struct Node *temp;
int target;
printf("Please enter a number you want to search for ");
scanf("%d", &target);
for(temp=head; temp!=NULL && found!=1; temp=temp->next)
if(temp->data==?)
found=1;
if(found==?)
printf("The number is found ");
else
printf("The number is not found ");
printf(" ");
}
struct Node* search_delete (struct Node *head, struct Node *tail)
{
int found=0;
struct Node *temp, *above, *below;
int target;
printf("Please enter a number you want to delete from the list ");
scanf("%d", &target);
for(temp=head; temp!=NULL && found!=1; )
if(temp->data==?)
found=1;
else
temp=temp->next;
if(found==1)
{
if(temp==head)
{
//move head down to point to the next node
head->prev=NULL;
free(temp);
}
else
{
if(temp==tail)
{
//move tail up to point to previous node
tail->next=NULL;
free(temp);
}
else
{
above=?
below=?
above->next=?
below->prev=?
free(temp);
}
}
}
else
printf("The number is not in the list ");
return head;
}
struct Node* place_tail(struct Node *head)
{
struct Node *temp;
for(temp=head; temp->next!=NULL; )
temp=temp->next;
return temp;
}
struct Node* add_node(struct Node *head, struct Node *tail)
{
struct Node *temp;
char input;
printf("Enter the number you want to add to the list ");
temp=(struct Node*)malloc(sizeof(struct Node));
scanf("%d", &temp->data);
temp->next=NULL;
temp->prev=NULL;
printf("Where do you want to place youe new node? Enter 't' for the top of the list and 'b'-for the bottom ");
scanf(" %c", &input);
if(input=='t')
{
//code for inserting a node on the top
}
else
{
//code for inserting a node at the bottom
}
return head;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started