unidirectional linked list pointer problem


 
Thread Tools Search this Thread
Top Forums Programming unidirectional linked list pointer problem
# 1  
Old 01-30-2012
unidirectional linked list pointer problem

I am trying to test some operations on a directed list. However, the declaration of a pointer is giving me trouble.

I seem to have done something incorrectly because I get an error:
"listtest.c:29: warning: 'p' may be used uninitialized in this function"

Can anyone help?


This is my code so far.

Code:
int main(int argc, char **argv){

dlist *list = dlist_empty();/*create a new empty list*/
dlist_position p;
char *element[2];
int i;


    /*test if list is empty*/    

if(!(dlist_empty())){
    printf(" list is not empty");
    return 0;
     
}
else {

        /*test to insert elements*/
        for (i = 0; i<3; i++) {
            element[i] = malloc(sizeof(char)*3);
            sprintf(element[i], "%d", i+1);
        }
        
        printf("build directed list \n");
        p = dlist_insert(list, p, element[0]);
        p = dlist_insert(list, p, element[1]);
        p = dlist_insert(list, p, element[2]);

        /*test to inspect elements*/        
        printf("first element: %s\n", 
           (char *)dlist_inspect(list, dlist_first(list)));

        /*test if element is first*/
        
        /*test if element is last*/

        /*test to remove elements*/

    }/*end if else */
    
return 0;
}

# 2  
Old 01-30-2012
I'm only guessing without line numbers, but I'd bet the compiler is complaining about this line:

Code:
 p = dlist_insert(list, p, element[0]);

Because p is, indeed, uninitialized when being passed as the second argument to dlist_insert.

That said, not knowing what dlist_insert is supposed to take, that's about the best I can offer.
# 3  
Old 01-30-2012
Thank you for you answer!!

Last edited by bluetxxth; 01-31-2012 at 03:24 AM..
# 4  
Old 01-30-2012
I'm not sure what you want the "dlist_position" to be, but what you're doing in that insert won't work at all. How you have it coded right now is very likely to crash when executing newPosition->next = p->next as the first time it's called p is NULL (likely; possibly some random value depending on compiler/optimizations, etc) and you can not execute p->next through the NULL pointer.

I would assume you want "dlist_position" to be a node in the list (maybe consider calling it dlist_node?). If that's the case, you'll have to special case when you want the new data to be inserted as the first position in the list.

I suggest allowing dlist_insert to special case when p == NULL, handling it correctly by linking the new position to the head of l. Then the very first call:

Code:
p = dlist_insert(list, p, element[0]);

becomes:
Code:
p = dlist_insert(list, NULL, element[0]);

Having no idea what the structure for dlist is, however, we can't help any more.

edit: also, dlist_position newPosition=malloc(sizeof(element)); looks odd -- I can only hope that dlist_position is just typedef'd as a pointer to an element...but, again, not enough code to tell.

Last edited by DreamWarrior; 01-30-2012 at 07:06 PM..
# 5  
Old 01-30-2012
yes that is exactly what I want. I am going to try to do what you mentioned, althought I am not sure if I understood the part of the special case.

"....have to special case when you want the node to be inserted as the first position in the list. Maybe, in that case, you should pass the p variable as NULL and then check for NULL in dlist_insert and handle it correctly by linking it to the head of l...."

Could you elaborate a bit on that please? Thank you again!
# 6  
Old 01-30-2012
Quote:
Originally Posted by bluetxxth
yes that is exactly what I want. I am going to try to do what you mentioned, althought I am not sure if I understood the part of the special case.

"....have to special case when you want the node to be inserted as the first position in the list. Maybe, in that case, you should pass the p variable as NULL and then check for NULL in dlist_insert and handle it correctly by linking it to the head of l...."

Could you elaborate a bit on that please? Thank you again!
Not really, because I have no idea what the dlist structure looks like. But, I'd guess you have a head pointer in dlist. So:
Code:
if (p == NULL)
{
  newPosition->next = l->head;
  l->head = newPosition;
}
else
{
  newPosition->next=p->next;
  p->next=newPosition;
}

I'm also curious why you're returning p and not newPosition; I'd figure you should probably return the node you just inserted...no?

At this point, I think this may be the extent to which I can assist. At this very basic level, it smacks of your being in an algorithms class. At which point, I'm wondering if this isn't homework. I don't want to be a jerk, but if you're going to succeed in this industry, you will eventually need to have these basics figured out. So, before I assist any more, I want to see a little more thought put in by yourself. You can get there....
# 7  
Old 01-31-2012
Hi, thanks for your help, and excuse me for being so vague, it was late for me yesterday and I just hit the wall... I am just trying to learn how to use this data structure. I will post a more thoughtful question next time.

BR
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to reverse a linked list by traversing only once.?

Program to reverse a linked list by traversing only once. (1 Reply)
Discussion started by: VSSajjan
1 Replies

2. Programming

How to delete the last node in a linked list.?

How to delete the last node in a single linked list given only the pointer to last node ? Head node will not be given. (5 Replies)
Discussion started by: VSSajjan
5 Replies

3. Programming

Help with linked list.

#include<stdio.h> #include<stdlib.h> struct LinkedList { int val; struct LinkedList *next; }node; /*Creating a structure variable*/ typedef struct LinkedList Node; Node *start = NULL; int create(int i) { Node *temp = NULL; if (start == NULL) ... (5 Replies)
Discussion started by: prinsh
5 Replies

4. Programming

How to check if something exists in linked list in C?

i have a linked list set up like typedef struct client_list { char *client_name; int client_socket_fd; struct client_list *next; } client; client *client_list=NULL; before adding to the list i check if it already exists, only if it does not then i add if (client_list==NULL... (1 Reply)
Discussion started by: omega666
1 Replies

5. Programming

Help with linked list in C

i have this code typedef struct client_list { char *client_name; struct client_list * next; int client_socket_fd; } client; client *current, *head; head = NULL; char *h="test"; add_client(current, h, head, &client_socket_fd); ... (24 Replies)
Discussion started by: omega666
24 Replies

6. Programming

I need C++ Code for single linked list

I need C++ Code for single linked list With operations as 1)insert at any position 2)delete any 3)change the data of any position (2 Replies)
Discussion started by: girija
2 Replies

7. Programming

shared memory with linked list??

is this possible, if so plz please share with me.. Correct English please, not Cyber-/Leetspeak (11 Replies)
Discussion started by: vijay_manpage
11 Replies

8. Programming

linked list node with pointer to struct

Suppose to have: struct Tstudent { string name, surname; int matriculation_num; }; struct Tnode { Tstudent* student; Tnodo* next; } L;I want to deference that "student" pointer. For example, I tried with: *(L->student).matriculation_numbut it not worked, as terminal... (4 Replies)
Discussion started by: Luke Bonham
4 Replies

9. UNIX for Dummies Questions & Answers

List linked files

A perl script that displays the list of files which have multiple links..! ls -l shows number of links in a field. (0 Replies)
Discussion started by: aadi_uni
0 Replies

10. Programming

Reverse single linked list

Can any one help me in reversing the single linked list and at the same time i want to print the reversed links. (2 Replies)
Discussion started by: dhanamurthy
2 Replies
Login or Register to Ask a Question