Finishing the third problem
This commit is contained in:
parent
812d509b53
commit
0892f8c587
1 changed files with 157 additions and 1 deletions
156
lab12/3.c
156
lab12/3.c
|
@ -1,7 +1,163 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* Single Linked list related code */
|
||||||
|
typedef struct _selist * Slist;
|
||||||
|
|
||||||
|
struct _selist {
|
||||||
|
int el;
|
||||||
|
Slist next;
|
||||||
|
};
|
||||||
|
|
||||||
|
int head(Slist lst) {
|
||||||
|
return lst->el;
|
||||||
|
}
|
||||||
|
|
||||||
|
Slist tail(Slist lst) {
|
||||||
|
return lst->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
Slist insert(int element, Slist list) {
|
||||||
|
Slist * addr = &list;
|
||||||
|
while (*addr) { // While addr' not null, go to the end.
|
||||||
|
addr = &(*addr)->next; // the list is modified by this. I think...
|
||||||
|
} // we should be at the end now.
|
||||||
|
Slist node = malloc(sizeof(struct _selist));
|
||||||
|
node->el = element;
|
||||||
|
node->next = NULL; // we set to head if we want circular, I think?
|
||||||
|
*addr = node;
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
Slist delete(int element, Slist list) {
|
||||||
|
Slist * addr = &list;
|
||||||
|
while (*addr) {
|
||||||
|
if ( (*addr)->el == element) {
|
||||||
|
Slist temp = (*addr)->next;
|
||||||
|
free(*addr);
|
||||||
|
*addr = temp;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
addr = &(*addr)->next;
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t size(Slist list) {
|
||||||
|
size_t elements = 0;
|
||||||
|
while (list) {
|
||||||
|
elements += 1;
|
||||||
|
list = list->next;
|
||||||
|
}
|
||||||
|
return elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_list(Slist list) {
|
||||||
|
printf("SLIST PRINT\n");
|
||||||
|
Slist * addr = &list;
|
||||||
|
while (*addr) {
|
||||||
|
printf("%d ", (*addr)->el);
|
||||||
|
addr = &(*addr)->next;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Double Linked list related code */
|
||||||
|
|
||||||
|
typedef struct _dilist * Dlist;
|
||||||
|
|
||||||
|
struct _dilist {
|
||||||
|
int element;
|
||||||
|
Dlist next;
|
||||||
|
Dlist prev;
|
||||||
|
};
|
||||||
|
|
||||||
|
Dlist insert_dl(int element, Dlist list) {
|
||||||
|
Dlist * addr = &list;
|
||||||
|
Dlist prev = NULL;
|
||||||
|
while (*addr) { // While addr' not null, go to the end.
|
||||||
|
if ( !(*addr)->next ) {
|
||||||
|
prev = *addr;
|
||||||
|
}
|
||||||
|
addr = &(*addr)->next; // the list is modified by this. I think...
|
||||||
|
} // we should be at the end now.
|
||||||
|
Dlist node = malloc(sizeof(struct _dilist));
|
||||||
|
node->element = element;
|
||||||
|
node->next = NULL; // we set to head if we want circular, I think?
|
||||||
|
node->prev = prev; // 1 - NULL, 2 - 1, 3- 2, 4 - 3
|
||||||
|
// printf("%d", prev->element);
|
||||||
|
*addr = node;
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_dlist(Dlist list) {
|
||||||
|
printf("DLIST PRINT\n");
|
||||||
|
Dlist * addr = &list;
|
||||||
|
while (*addr) {
|
||||||
|
// if (((*addr)->prev))
|
||||||
|
// printf("prev %d\n", ((*addr)->prev)->element);
|
||||||
|
printf("%d ", (*addr)->element);
|
||||||
|
addr = &(*addr)->next;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Code related to the split function */
|
||||||
|
|
||||||
|
void split_li_to_di(Slist sli, Dlist * dli1, Dlist * dli2) {
|
||||||
|
short first_insertion;
|
||||||
|
size_t sizzle = size(sli); // ma fizzle, drizzle
|
||||||
|
if (!sizzle) { return; } // exit if size is zeero.
|
||||||
|
size_t middle = sizzle / 2;
|
||||||
|
|
||||||
|
|
||||||
|
// Handle first double linked list.
|
||||||
|
*dli1 = malloc(sizeof(struct _dilist));
|
||||||
|
first_insertion = 1;
|
||||||
|
for (int i = 0; i < middle; i++) {
|
||||||
|
if ( first_insertion) {
|
||||||
|
*dli1 = insert_dl(sli->el, NULL);
|
||||||
|
first_insertion = 0; // :>
|
||||||
|
} else {
|
||||||
|
*dli1 = insert_dl(sli->el, *dli1);
|
||||||
|
}
|
||||||
|
sli = sli->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle seccond double linked list.
|
||||||
|
if ( !(middle % 2) ) { middle += 1; } // if not even, add one.
|
||||||
|
*dli2 = malloc(sizeof(struct _dilist));
|
||||||
|
first_insertion = 1;
|
||||||
|
for (int i = 0; i < middle; i++) {
|
||||||
|
if ( first_insertion) {
|
||||||
|
*dli2 = insert_dl(sli->el, NULL);
|
||||||
|
first_insertion = 0; // :>
|
||||||
|
} else {
|
||||||
|
*dli2 = insert_dl(sli->el, *dli2);
|
||||||
|
}
|
||||||
|
sli = sli->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
// Write a function to split a simple linked list into two double linked lists at a
|
||||||
|
// specific point in the original list. The specific point is provided by the key value.
|
||||||
|
Slist li = insert(1, insert(2, insert(3, insert(4, insert(5, NULL)))));
|
||||||
|
|
||||||
|
Dlist dli1 = NULL;
|
||||||
|
Dlist dli2 = NULL;
|
||||||
|
|
||||||
|
split_li_to_di(li, &dli1, &dli2);
|
||||||
|
|
||||||
|
print_list(li);
|
||||||
|
print_dlist(dli1);
|
||||||
|
print_dlist(dli2);
|
||||||
|
|
||||||
|
// Test
|
||||||
|
// Dlist dli = insert_dl(1, insert_dl(2, insert_dl(3, insert_dl(4, NULL))));
|
||||||
|
// print_dlist(dli);
|
||||||
|
// Slist li = insert(1, insert(2, insert(3, insert(4, NULL))));
|
||||||
|
// li = delete(1, li);
|
||||||
|
// print_list(li);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue