6 Replies - 11008 Views - Last Post: 12 July 2016 - 11:01 AM Rate Topic: -----

#1 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Generic Linked List

Posted 30 September 2012 - 08:56 PM

Description: A generic linked list implemented with void pointers
/* Written by JJL */

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


typedef struct Node {
   void *data;
   struct Node *next;
} Node;

Node *add(Node *head, void *data, int bytes) {
   Node *tmp = malloc(sizeof(Node));
   memcpy(tmp->data = malloc(bytes), (char*)data, bytes);
   tmp->next = head;
   return tmp;
}

void *get(Node *head, int index) {
   for(; index--; head = head->next);
   return head->data;
}

int size(Node *head) {
   int size = 0;
   for(; head; head = head->next, size++);
   return size;
}

void free_list(Node *head) {
   if(head->next)
      free_list(head->next);
   free(head->data);
   free(head);
}

int main() {
   char a = 'a';
   int b = 20003;
   short c = 8343;

   Node *head = NULL;
   head = add(head, &a, 1);
   head = add(head, &b, 4);
   head = add(head, &c, 2);

   printf("list size: %dn", size(head));
   printf("values: %c, %d, %hdn", *(char*)get(head, 2),
                                   *(int*)get(head, 1),
                                   *(short*)get(head, 0));

   free_list(head);
   getchar();
   return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Generic Linked List

#2 Gikoskos   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-January 12

Re: Generic Linked List

Posted 28 August 2015 - 12:41 AM

The free_list function isn't correct, right? I mean you're only freeing the 1st and 2nd node of the list.

View PostGikoskos, on 28 August 2015 - 12:39 AM, said:

The free_list function isn't correct, right? I mean you're only freeing the 1st and 2nd node of the list.

Disregard this it's completely wrong, I just saw the recursion.

Disregard this it's completely wrong, I just saw the recursion.
Was This Post Helpful? 0
  • +
  • -

#3 rodiongork   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 61
  • Joined: 28-August 15

Re: Generic Linked List

Posted 28 August 2015 - 03:59 AM

Quote

Disregard this it's completely wrong, I just saw the recursion.


I think you are not "completely" wrong. Using recursion for this operation seems unnecessary and as it limits the list size with the available stack size, I think it is better to avoid it, moreover that loop could be written quite easily...
Was This Post Helpful? 0
  • +
  • -

#4 Gikoskos   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-January 12

Re: Generic Linked List

Posted 28 August 2015 - 06:24 AM

I do it like this, is this correct on a singly linked list?
void freeList(Node *head)
{
	Node *temp;
	while (head != NULL) {
		temp = head;
		head = head->nxt;
		free(temp);
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 rodiongork   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 61
  • Joined: 28-August 15

Re: Generic Linked List

Posted 28 August 2015 - 07:01 AM

Yes, looks perfect!
Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7380
  • View blog
  • Posts: 15,311
  • Joined: 16-October 07

Re: Generic Linked List

Posted 28 August 2015 - 08:49 AM

Almost. The data was missed. And next was fully spelled out.
e.g.
void freeList(Node *head) {
    while (head) {
        Node *p = head;
        head = head->next;
        free(p->data);
        free(p);
    }
}


Was This Post Helpful? 2
  • +
  • -

#7 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Generic Linked List

Posted 12 July 2016 - 11:01 AM

The recursion was purely an academic exercise :) Obviously not suitable for application.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1