(urgent) recursive function and doubly linked list

Hello guys,I have doubts on how to do the function below can you help me?

please guys help me I don't understand anything about recursive function

Consider a doubly linked list that stores the following student data from
a discipline:
- Registration Number: integer
- Note A1: Floating point number
- Note A2: Floating point number
- Note A3: Floating point number

Write a Recursive function in C that receives a vector of n students and
build a doubly linked list by storing the vector elements in the nodes the list, ensuring the same order as the vector. If the vector has no elements, the function
returns an empty list.

I must use this prototype: TLDE * build_recursive (int n, student * v)
Last edited on
Please post the declaration of TLDE.

The function will look something like:
1
2
3
4
5
6
if (n == 0)  return nullptr;

TLDE *rest = build_recursive(n-1, v+1);

// Create a new node for v and prepend it to "rest"
// return the new list. 

this is the declaration?? how do i create the node for v??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct student
{
int data;
struct student*prev;
struct student*next;
};

typedef struct student TLDE;

TLDE* create(void);
TLDE* insert_start(TLDE* l, int elem);
TLDE* insert_end(TLDE* l, int elem);
TLDE* search(TLDE* l, int elem);
TLDE* to_remove(TLDE* l, int elem);
TLDE* inverts(TLDE* l);
Last edited on
You start with some root (first) node:

1
2
3
4
TLDE* root = malloc(sizeof(TLDE));
root->data = 42;
root->prev = NULL;
root->next = NULL;


This logic might go in your create function, I guess.

If you are appending on to an existing node:
1
2
3
4
TLDE* new_node = malloc(sizeof(TLDE));
new_node->data = elem;
new_node->prev = some_prev_node_ptr;
new_node->next = NULL;


What you've posted just looks like what you've been given to start with. Have you actually implemented any of the functions, yet? You might be able to use the insert_start/end functions as part of the build_recursive function.
Last edited on
I already have this code ready but I don't know if it's correct:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include<stdio.h>
#include<stdlib.h>

struct student
{
int data;
struct student *prev;
struct student *next;
};

typedef struct student TLDE;

TLDE* create(void);
TLDE* insert_start(TLDE* l, int elem);
TLDE* insert_end(TLDE* l, int elem);
TLDE* search(TLDE* l, int elem);
TLDE* to_remove(TLDE* l, int elem);
TLDE* inverts(TLDE* l);

TLDE* create(void)
{
return NULL;
}

TLDE* insert_start(TLDE* l, int elem)
{
TLDE* l2 = (TLDE*) malloc(sizeof(TLDE));
l2->data = elem;
l2->next = l;
l2->prev = NULL;

if(l != NULL)
l->prev = l2;
return l2;
}

TLDE* insert_end(TLDE* l, int elem)
{
TLDE* l2 = (TLDE*) malloc(sizeof(TLDE));
l2->data = elem;
l2->next = NULL;

if (l==NULL)
l = l2;
else
{
TLDE* current = l;
while(current->next != NULL){
current = current->next;
}
current->next = l2;
}
return l;
}

TLDE* search(TLDE* l, int elem)
{
TLDE* l2;
for(l2=l;l2!=NULL;l2=l2->next)
{
if(l2->data == elem)
{
return l2;
}
}
return NULL;
}

TLDE* to_print(TLDE* l)
{
TLDE* l2 = l;
for (l2 = l; l2 != NULL; l2 = l2->next)
{
printf("data = %d\n", l2->data);
}
return NULL;
}

TLDE* to_remove(TLDE* l, int elem)
{
TLDE* l2 = search(l,elem);

if(l2 == NULL)
return l;

if(l == l2)
l = l2->next;
else
l2->prev->next = l2->next;

if(l2->next != NULL)
l2->next->prev = l2->prev;

free(l2);

return l;
}

void releases_memory(TLDE *l)
{
TLDE *l2 = l;
while (l2!= NULL)
{
TLDE *t = l2->next;
free (l2);
l2=t;
}
}

TLDE* inverts(TLDE* l){
TLDE *l2 = l;
TLDE *aux=NULL;

l2=l->next;
l->next=NULL;

while(l2!=NULL){
aux=l2->next;
l2->next=l;
l=l2;
l2=aux;
}
return l;
}
void to_count(TLDE* l)
{
TLDE* l2 = l;
int count = 0;
for (l2 = l; l2 != NULL; l2 = l2->next)
{
count++;
}
printf("%d \n",count);
}



int main(void)
{
int elem;
TLDE *l2;


l2=create();
l2=insert_start(l2,10);
l2=insert_start(l2,2);
l2=insert_start(l2,3);
l2=insert_start(l2,10365658);
printf("created list! \n");
to_print(l2);

l2=inverts(l2);
printf("inverted list! \n");
to_print(l2);
system("PAUSE");

return (0);
}
Last edited on
Is build_recursive() supposed to build a new list from the data in the elements of the vector, or is it supposed to modify the pointers of the vector's elements so they form a valid list?

If it's the former, then this should work, taken directly from my previous post:
1
2
3
4
5
6
TLDE * build_recursive (int n, student * v)
{
    if (n == 0) return nullptr;
    TLDE *rest = build_recursive(n-1, v+1);
    return insert_start(rest, v->data);
}


If it's the latter then you need to duplicate some of the code in insert_start():
1
2
3
4
5
6
7
8
9
10
TLDE * build_recursive (int n, student * v)
{
    if (n == 0) return nullptr;
    TLDE *rest = build_recursive(n-1, v+1);

    v->next = rest;
    v->prev = nullptr;
    if (rest) rest->prev = v;
    return v;
}

Topic archived. No new replies allowed.