Question
need help getting the correct output when i run my code im not getting any values printed, im just getting lines of space, im supposed
need help getting the correct output when i run my code im not getting any values printed, im just getting lines of space, im supposed to get the output shown below and need help getting it.
bigint_dllist.c
#include "bigint_dllist.h"
#include
#include
#include
NODE *new_node(char data) {
NODE *np = (NODE*) malloc(sizeof(NODE));
if (np == NULL) {
printf("malloc fails");
return NULL;
}
np->data = data;
np->prev = NULL;
np->next = NULL;
return np;
}
void display_forward(NODE *np) {
if (np == NULL)
return;
NODE *ptr = np;
while (ptr != NULL) {
printf("%d", ptr->data);
ptr = ptr->next;
}
}
void display_backward(NODE *np) {
if (np == NULL)
return;
NODE *ptr = np;
while (ptr != NULL) {
printf("%d ", ptr->data);
ptr = ptr->prev;
}
}
void insert_start(NODE **startp, NODE **endp, NODE *new_np) {
if (*startp == NULL)
return;
NODE *temp = *startp;
new_np->next = temp;
temp->prev = new_np;
*startp = new_np;
}
void insert_end(NODE **startp, NODE **endp, NODE *new_np) {
if (*startp == NULL)
return;
NODE *temp = *endp;
temp->next = new_np;
new_np->prev = temp;
*endp = new_np;
}
void delete_start(NODE **startp, NODE **endp) {
if (*startp == NULL)
return;
NODE *temp = *startp;
temp = temp->next;
(*startp)->next = NULL;
temp->prev = NULL;
free(*startp);
(*startp) = temp;
}
void delete_end(NODE **startp, NODE **endp) {
if (*startp == NULL)
return;
NODE *temp = *endp;
temp = temp->prev;
(*endp)->prev = NULL;
temp->next = NULL;
free(*endp);
*endp = temp;
}
void clean(NODE **startp, NODE **endp) {
if (*startp == NULL)
return;
NODE *temp = *startp;
while (temp != NULL) {
NODE *prev = temp;
temp = temp->next;
if (temp)
temp->prev = NULL;
prev->next = NULL;
free(prev);
}
*startp = NULL;
*endp = NULL;
}
main.c
#include "bigint_dllist.h"
#include
#include
int main(int argc, char* args[]) {
NODE *start = NULL, *end = NULL;
int i=0;
for (i = 0; i<10; i++) {
insert_start(&start, &end, new_node('0'+i));
}
display_forward(start);
printf(" ");
display_backward(end);
delete_start(&start, &end);
delete_end(&start, &end);
printf(" ");
display_forward(start);
clean(&start, &end);
for (i = 0; i<10; i++) {
insert_end(&start, &end, new_node('a'+i));
}
printf(" ");
display_forward(start);
clean(&start, &end);
return 0;
}
bigint_dllist.h
#ifndef DLLIST
#define DLLIST
#include
#include
typedef struct node {
char data;
struct node *prev;
struct node *next;
} NODE;
NODE *new_node(char data);
void display_forward(NODE *start);
void display_backward(NODE *end);
void insert_start(NODE **startp, NODE **endp, NODE *new_np);
void insert_end(NODE **startp, NODE **endp, NODE *new_np);
void delete_start(NODE **startp, NODE **endp);
void delete_end(NODE **startp, NODE **endp);
void clean(NODE **startp, NODE **endp);
#endif
required output
9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 a b c d e f g h i j
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