Thread: Memory adress as output

  1. #31
    Registered User
    Join Date
    Feb 2021
    Posts
    22
    Also this might be a pointer arthmetic question, but how would I access the next pointer of the node before the one im inserting. I assume it I would do it by using the tail pointer before I dereference it. But how would I do that? can I do something like this? link->tail->next as I think you mentioned.

  2. #32
    Registered User
    Join Date
    Feb 2021
    Posts
    22
    Okay Im getting it to work!! This is how the code looks like now:
    Code:
    #include "stdio.h"
    #include "stdlib.h"
    
    
    typedef struct node
    {
    	struct node *prev;
    	int value;
    	struct node *next;
    }node_t;
    
    
    typedef struct list
    {
    	node_t *head;
    	int size;
    	node_t *tail;
    }list_t;
    
    
    int IsEmpty(list_t *list)
    {
    	if (list->size == 0)
    	{
    		printf("0");
    		return 0;
    	}
    	else
    	{
    		printf("1");
    		return 1;
    	}
    }
    
    
    list_t *createList(void)
    {
        list_t *list = malloc(sizeof(*list));
        if (list)
        {
            list->head = NULL;
            list->size = 0;
    		list->tail = NULL;
        }
        return list;
    }
    node_t * createNode(int n)
    {
    	node_t *newnode = malloc(sizeof(node_t));
    	if (!newnode)
        {
            return NULL;
        }
    	newnode->value = n;
    	newnode->next = NULL;
    	newnode->prev = NULL;
    	return newnode;	
    }
    void printlist(list_t* list)
    {
    	node_t *current = list->head;
    	while (current != NULL)
    	{
    		printf("%d ", current->value);
    		current = current->next;
    	}
    	printf("\n");
    }
    
    
    int insertTail(list_t *list, node_t *node)
    {	
    	if (list->head == NULL)
    	{
    		list->head = node;
    		node->next = NULL;
    		node->prev = NULL;
    		list->tail = node;
    		list->size += 1;
    		return 1;
    	}
    	else
    	{
    		node->prev = list->tail;
    		list->tail->next = node;
    		list->tail = node;
    		node->next = NULL;
    		list->size += 1;
    		return 1;
    	}
    }
    
    
    
    
    int main()
    {
    	list_t *list2;
    	list2 = createList();
    	node_t *node1;
    	node_t *node2;
    	node_t *node3;
    	node_t *node4;
    	node_t *node5;
    	node1 = createNode(6);
    	node2 = createNode(7);
    	node3 = createNode(8);
    	node4 = createNode(4);
    	node5 = createNode(9);
    	insertTail(list2, node1);
    	insertTail(list2, node2);
    	insertTail(list2, node3);
    	insertTail(list2, node4);
    	insertTail(list2, node5);
    	printlist(list2);
    	free(node5);
    	free(node4);
        free(node3);
    	free(node2);
        free(node1);
        free(list2);
    }

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,360
    Congratulations, it looks like you've implemented a working solution. I might tweak it a bit:
    Code:
    void insertTail(list_t *list, node_t *node)
    {
        node->prev = list->tail;
        node->next = NULL;
        if (list->head == NULL)
        {
            list->head = node;
        }
        else
        {
            list->tail->next = node;
        }
        list->tail = node;
        list->size += 1;
    }
    I changed the return type to void because insertTail doesn't fail, so you don't need a value to indicate success/failure.

    I moved out common statements to before/after the if-else chain. So, I start by setting the previous and next pointers of the current node. I use list->tail because whether or not it is a null pointer, node->prev is set correctly. Then of course node->next is always a null pointer, so I set that too. Then it is always the case that we set node to be the new list->tail, so I moved that to after the if-else, and likewise you're always going to increment the size.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    Registered User
    Join Date
    Feb 2021
    Posts
    22
    I can't thank you enough! Your help has been invaluble for me to actually understand this whole ordeal. I really appreciate that you made me conceptualize the problem. I really feel like drawing out the problem really made the whole concept click. Thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory. Output problems.
    By qwes3r in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2015, 09:10 AM
  2. NUMA Virtual Adress Space / Physical Adress Space
    By NUMA Orly in forum Windows Programming
    Replies: 0
    Last Post: 03-14-2011, 03:19 AM
  3. Replies: 4
    Last Post: 11-01-2009, 06:19 AM
  4. Tell adress of object from adress of member
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2007, 05:04 AM
  5. Searching memory for an adress?
    By Blackroot in forum C++ Programming
    Replies: 12
    Last Post: 03-27-2007, 11:59 PM

Tags for this Thread