logo
down
shadow

How to manage different kinds of data in a linked list?


How to manage different kinds of data in a linked list?

By : Samir
Date : October 22 2020, 06:10 PM
I wish this helpful for you In general you need to add a type tag to struct Node so that you can track the kind of data stored in the individual nodes.
For storing data you can use a void pointer or you can use a union. If you use a void pointer you'll need casting whenever accessing data. If you use a union every node will end up using memory corresponding to the size of the largest union member.
code :
#include <stdio.h>
#include <stdlib.h>

enum ListType 
{
    INT = 0,
    FLOAT,
    CHAR,
    STRING,
};

struct Node
{
    struct Node *next;
    enum ListType type;
    void *data;
};

void printNode(struct Node *p)
{
    switch (p->type)
    {
        case INT:
            printf("%d ", *((int*)p->data));
            break;
        case FLOAT:
            printf("%f ", *((float*)p->data));
            break;
        case CHAR:
            printf("%c ", *((char*)p->data));
            break;
        case STRING:
            printf("%s ", (char*)p->data);
            break;
        default:
            printf("ERROR ");
            break;
    }
}

void printList(struct Node *p)
{
    while(p)
    {
        printNode(p);
        p = p->next;
    }
}

void freeListData(struct Node *p)
{
    while(p)
    {
        free(p->data);
        p = p->next;
    }
}

int main(void) {
    // Build the list manually to illustrate the printing
    struct Node N1;
    struct Node N2;

    N1.type = FLOAT;
    N1.data = malloc(sizeof(float));
    *((float*)N1.data) = 3.14;
    N1.next = &N2;

    N2.type = INT;
    N2.data = malloc(sizeof(int));
    *((int*)N2.data) = 100;
    N2.next = NULL;

    // .. more nodes

    printList(&N1);

    freeListData(&N1);

    return 0;
}
3.140000 100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum ListType 
{
    INT = 0,
    FLOAT,
    CHAR,
    STRING,
};

union ListData
{
    int d;
    float f;
    char c;
    char *str;  // Memory for the string must be malloc'ed
};

struct Node
{
    struct Node *next;
    enum ListType type;
    union ListData data;
};

void printNode(struct Node *p)
{
    switch (p->type)
    {
        case INT:
            printf("%d ", p->data.d);
            break;
        case FLOAT:
            printf("%f ", p->data.f);
            break;
        case CHAR:
            printf("%c ", p->data.c);
            break;
        case STRING:
            printf("%s ", p->data.str);
            break;
        default:
            printf("ERROR ");
            break;
    }
}

void printList(struct Node *p)
{
    while(p)
    {
        printNode(p);
        p = p->next;
    }
}

void freeListStrings(struct Node *p)
{
    while(p)
    {
        if (p->type == STRING) free(p->data.str);
        p = p->next;
    }
}

int main(void) {
    // Build the list manually to illustrate the printing
    struct Node N1;
    struct Node N2;
    struct Node N3;

    N1.type = FLOAT;
    N1.data.f = 3.14;
    N1.next = &N2;

    N2.type = INT;
    N2.data.d = 100;
    N2.next = &N3;

    N3.type = STRING;
    N3.data.str = malloc(sizeof "Hello World");
    strcpy(N3.data.str, "Hello World");
    N3.next = NULL;

    // .. more nodes

    printList(&N1);

    freeListStrings(&N1);

    return 0;
}
3.140000 100 Hello World 


Share : facebook icon twitter icon
JAVA Linked List Search Linked List to compare data with user inputted data?

JAVA Linked List Search Linked List to compare data with user inputted data?


By : user3152004
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have a linked list. Everything is going great inside of it. My only problem is how do I compare a variable to the contents to see if their is a match. , Use the contains() method in the List interface.
code :
if (NameList1.contains("Johnny")) {
    // code to execute if Johnny is in the list
}
C++ Simplified Version Linked list, when i load in data to create a linked list, and enter in more data, there seems to

C++ Simplified Version Linked list, when i load in data to create a linked list, and enter in more data, there seems to


By : Faolan Bozkurtlar
Date : March 29 2020, 07:55 AM
Does that help I have a read function, which reads external files and makes each name a new node, and I'd like to build on the list that I've created, but somehow there's like a gap in the list
code :
while(!read.eof())
{
    read.getline(temp, TEMP_SIZE);
    ...
}
for (;;)
{
    read.getline(temp, TEMP_SIZE);
    if (read.failed())
        break;
    ...
}
while (read.getline(temp, TEMP_SIZE))
{
    ...
}
Creating a data structure that consists of a linked balanced bst and a doubly linked list

Creating a data structure that consists of a linked balanced bst and a doubly linked list


By : ZhangZhang
Date : March 29 2020, 07:55 AM
wish helps you std::set iterators remain valid when you insert new elements in the set, so this should not cause problems when you keep the iterators in a list. Erasing elements from the set will invalidate iterators to those elements (obviously), but other iterators remain valid.
Functions that manage a linked list

Functions that manage a linked list


By : Sardar Esm
Date : March 29 2020, 07:55 AM
I wish this help you I have a class called StringList than has multiple functions that manage and manipulate a linked list of strings. I have an AddtoTop which adds to the top of the list, AddtoBottom, a function called add which adds a string but places it in alphabetical order, a clear function, a find function, print and a remove function. I have written every single function, however i have one main issue. When i use "AddtoTop" and "AddtoBottom", I add a string to the linked list. My remove function works when i ask to remove a string that i placed using those two functions only. When i try to remove a word i added with the "add" function, my program crashes. The function "remove" (shown below) can remove strings that were added using the "AddtoBottom" and "AddtoTop" functions but not the strings added using "add" Can anyone help me figure out why? Im trying to re write it a different way but i get stuck. Here are my functions: , Try something like this:
code :
void StringList::add(string s) //adds and places in alphabetical order
{
    if(pTop)
    {
        StringListNode *iter = pTop;
        while (iter && s > iter->data)
            iter = iter->pNext;

        if (iter)
        {
            // insert before
            StringListNode *A=new StringListNode;
            A->data = s;
            A->pNext = iter;
            A->pPrev = iter->pPrev;
            if (iter->pPrev)
                iter->pPrev->pNext = A;
            else
                pTop = A;
            iter->pPrev = A;
            return;
        }
    }

    // add to bottom
    addToBottom(s);
}
After inserting new data to linked list, can't deleting any data from new linked list

After inserting new data to linked list, can't deleting any data from new linked list


By : Tani
Date : October 03 2020, 12:00 AM
fixed the issue. Will look into that further In deleteNode function, you are checking the second element and if that is not the data which needs to be deleted, you print out "Not found to delete" and exit from the function. Hence, data you want to delete is not deleted.
Related Posts Related Posts :
  • dynamic memory for 2D char array
  • C compiler producing lightweight executeables
  • Cannot Open Shared Object cygmpfr-1.dll
  • How to access a structure member in a function that get it as void* type?
  • Replace for loop with formula
  • how to find if stack increases upwards or downwards?
  • Why does my homespun sizeof operator need a char* cast?
  • Setting exit status when creating core dump
  • Signal Handling in C
  • How to implement B+ Tree for file systems?
  • Stack storage at function call
  • Manipulate the Clipboard in C on Mac OS X
  • Problem with writing a hexadecimal string
  • Why does this code read all ' ' for the anything after the 4th character?
  • GUI for linux c
  • Where do I put all these function-like #defines, in C?
  • Padding in 24-bits rgb bitmap
  • how to acess and change variable of kernel space from user space
  • Can a conforming C implementation #define NULL to be something wacky
  • send() always interrupted by EPIPE
  • Socket Select with empty fd set
  • Coding self similar fractals in computer graphics using C
  • Forking with Pipes
  • sscanf + c99 not working on some platforms?
  • Problem with C function of type char pointer, can someone explain?
  • Macros in C.... please give the solution
  • Unix Piping using Fork and Dup
  • Any difference between these two while loops?
  • Hashtable implementation for C
  • Problems with Linked List in C
  • Stack Allocation in C
  • Random function stuck on one number
  • printf following zeros to a certain degree
  • Measuring CPU clocks consumed by a process
  • How is a CRC32 checksum calculated?
  • Tips on redefining a register bitfield in C
  • Converting a program from c to bash script
  • Static library not included in resulting LLVM executable
  • What is the meaning of "wild pointer" in C?
  • Reading DWORD from binary file
  • Efficient way to find divisibility
  • Whats the fastest, portable way to hash pointers we know are pointer aligned to a fixed size int?
  • Why does my summation program behaves weird?
  • How to determine values saved on the stack?
  • Thread scheduling C
  • C - converting to 2s complement
  • wireless networks c program
  • From where does the uboot get the information about flash, RTC, RAM in uboot/kernel development
  • printf behaviour in C
  • Measure size and way-order of L1 and L2 caches
  • Efficient copy of entire directory
  • Efficient way to create/unpack large bitfields in C?
  • getnameinfo prototype asks for sockaddr not sockaddr_in?
  • Linked List Sorting with Strings In C
  • Typecasting a floating value or using the math.h floor* functions?
  • Problem implementing sorting algorithm in C with an array of structs
  • How to copy a ram_base file to disk efficiently
  • How to fix casting a pointer to an integer?
  • How to round floating point numbers to the nearest integer in C?
  • PIC C - Sending 200 values over USB, but it only sends 25 or so of them
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk