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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!