Question
Creating a list from an array: #include #include #include list.h typedef struct node { ElemType val; struct node *next; } NODE; struct list_struct { NODE
Creating a list from an array:
#include
#include
#include "list.h"
typedef struct node {
ElemType val;
struct node *next;
} NODE;
struct list_struct {
NODE *front;
NODE *back;
};
LIST *lst_create() {
LIST *l = malloc(sizeof(LIST));
l->front = NULL;
l->back = NULL;
return l;
}
void lst_push_front(LIST *l, ElemType val) {
NODE *p = malloc(sizeof(NODE));
p->val = val;
p->next = l->front;
l->front = p;
if(l->back == NULL) // was empty, now one elem
l->back = p;
}
void lst_push_back(LIST *l, ElemType val) {
NODE *p;
if(l->back == NULL) // list empty - same as push_front
lst_push_front(l, val);
else { // at least one element before push
p = malloc(sizeof(NODE));
p->val = val;
p->next = NULL;
l->back->next = p;
l->back = p;
}
}
/**
* TODO
* function: lst_from_array
*
* description: creates a new list populated with the
* elements of array a[] in the same order as
* they appear in a[] (element at a[0] will be the
* first element in the list and so-on). List is
* returned as a LIST pointer.
*
* Parameter n indicates the length of the given array.
*
* runtime requirement: THETA(n)
*/
LIST * lst_from_array(ElemType a[], int n) {
return NULL; //placeholder
}
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