[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Linked Lists in C
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

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 replies: 11
Thread images: 1

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

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.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.