[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Linked Lists in C

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 11
Thread images: 1

I recently wrote a linked list in C for fun.

I implemented a delete and insert function. I chose to do it in a way, that multiple occurrences will lead to multiple deletions/insertions. Now I wanted to implement a move function too, but it is not clear multiple occurrences now.

Do you have any suggestions?
>>
>>55097021
>Linked List
please don't ever do this again.
>>
>>55097021

Here is my code:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

typedef struct llist llist;

struct llist {
int value;
llist* next;
};

llist* head = NULL;
llist* curr = NULL;

llist* append(int value) {
llist* ptr = (llist*) malloc(sizeof(llist));
ptr->value = value;
ptr->next = NULL;

if (!head) {
head = curr = ptr;
}
else {
curr->next = ptr;
curr = ptr;
}
return ptr;
}

void delete(int target) {
llist* ptr = head;
while (ptr) {
llist* nxt = ptr->next;
while (nxt && nxt->value == target) {
ptr->next = nxt->next;
nxt = nxt->next;
}
ptr = ptr->next;
}
}

void insert(int target, int value) {
llist* ptr = head;
while (ptr) {
if (ptr->value == target) {
llist* new = (llist*) malloc(sizeof(llist));
new->value = value;
new->next = ptr->next;
ptr->next = new;
}
ptr = ptr->next;
}
}


>>
>>55097039
Linked lists are needed for certain applications faggot. Take a data structures class And an OS class. Processes are best stored in a linked list for OS because of the O(1) addition/removal of them. Other aspects too
>>
>>55097059

...
/*                                                                                                                   
void move(int source, int target) {
llist* ptrS = head;
while (ptr) {
llist* nxt = ptrS->next;
while (nxt && nxt->value == source) {


ptrS->next = nxt->next;
nxt = nxt->next;
}
ptrS = ptrS->next;
}
}
*/

void main() {
int i;
llist* p;
for (i=0; i<9; i++) {
p = append(2*i);
}
printf("\n");

append(16);
delete(12);
insert(6, 2);
insert(10, 16);

struct llist* ptr = head;
while (ptr) {
printf("%d ", ptr->value);
ptr = ptr->next;
}

printf("\n");
}


>>
>>55097021
a pointer to the end of your list can make things easier
>>
>>55097081

This is the output:
$ ./a.out 

0 2 4 6 2 8 10 16 14 16 16
>>
>>55097039

I know that Linked Lists are not efficient any more with modern prefetching. But it is not like, I am going to use this implementation for anything.
>>
>>55097096
Why?
The point I am not sure about, is how to handle a move operation, if you find your source and/or your target multiple times.
Should I do it only once.
What does not look very useful to me is to collect all sources together, copy them for each appearance of target, and insert the whole collection after each target.
What I specially do not like about this approach, is that I have to copy the move candidates...
>>
>>55097306
Have an int to select which occurrence you want to move, and pass it to your move function. (i.e., second occurrence, int x = 2).

In your move function, increment a counter variable every time the item you want to move is found. Once the counter value equals the int you've passed to your function, start moving that item.
>>
>>55097021
Just use C++ faggot.
Thread posts: 11
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.