this code has three functions to do three options. how can i add two more option
ID: 666835 • Letter: T
Question
this code has three functions to do three options. how can i add two more options which print the first letter and the last letter to the (void print) function ?
#include <stdio.h>
#include <stdlib.h>
#include "function.h"
typedef struct Node
{
char data;
struct Node* next;
}node;
struct Node* head = NULL; // Two glboal variables to store address of front and rear nodes.
struct Node* tail = NULL;
void insert( char data)
{
node* temp = (node*)malloc(sizeof(node));
temp->data =data;
temp->next = NULL;
if(head == NULL){
head = tail = temp;
Print(head);
return;
}
node *traverse=head;
// Traverse list upto end
while(traverse->next)
traverse = traverse->next;
traverse->next = temp;
Print(head);
}
void remove1()
{
node* temp = head;
if(head == NULL) {
printf("Queue: Queue is Empty ");
return;
}
else {
Print(head);
printf(" dequeued.");
head = head->next;
}
Print(head);
free(temp);
}
void Print(node *head) {
node* temp = head;
printf("Queue: ");
while(temp != NULL) {
printf("%c -> ",temp->data);
temp = temp->next;
}
printf("null ");
}
Explanation / Answer
Changes are highlighted:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
char data;
struct Node* next;
}node;
struct Node* head = NULL; // Two glboal variables to store address of front and rear nodes.
struct Node* tail = NULL;
void insert( char data)
{
node* temp = (node*)malloc(sizeof(node));
temp->data =data;
temp->next = NULL;
if(head == NULL){
head = tail = temp;
Print(head,0,0); //passing first=last=0 to print whole queue
return;
}
node *traverse=head;
// Traverse list upto end
while(traverse->next)
traverse = traverse->next;
traverse->next = temp;
Print(head,0,0); //passing first=last=0 to print whole queue
}
void remove1()
{
node* temp = head;
if(head == NULL) {
printf("Queue: Queue is Empty ");
return;
}
else {
Print(head,0,0); //passing first=last=0 to print whole queue
printf(" dequeued.");
head = head->next;
}
Print(head,0,0); //passing first=last=0 to print whole queue
free(temp);
}
//in Print function add two arguments first and last
//pass first=1 if you want to print first char
//pass last=1 if you want to print last char
//else pass first=last=0 to print all character
void Print(node *head,int first,int last) {
node* temp = head;
if(first==0 && last==0){ //print whole queue
printf("Queue: ");
while(temp != NULL) {
printf("%c -> ",temp->data);
temp = temp->next;
}
printf("null ");
}
else if(first==1) //print first char
{
if(temp !=NULL)
printf("%c -> ",temp->data);
else
printf("null ");
}
else if(last==1) //print last char
{ node* prev = NULL;
while(temp != NULL) {
prev=temp;
temp = temp->next;
}
printf("%c -> ",prev->data);
}
}
int main()
{ int ch,op;
char d;
do{
printf("Enter choice 1) insert 2) remove 3) Print 4) Print first char 5) Print last char ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter character ");
scanf("%c",&d);
insert(d);
break;
case 2:remove1();
break;
case 3:
Print(head,0,0); //call to print whole queue
break;
case 4:Print(head,1,0); //call to print first char
break;
case 5: Print(head,0,1); //call to print last char
break;
}
printf("wanna continue enter 1 ");
scanf("%d",&op);
}while(op==1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.