Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about use of structs 1

Status
Not open for further replies.

jisoo23

Programmer
Jan 27, 2004
192
0
0
US
Hello all,

I'm normally a Java programmer so please bear with me. I'm trying to figure out how to create a singly linked list in C rather then Java. According to what I've read, it looks like a "struct" in C would be what I would use to create the nodes in a singly linked list. I have very very little knowledge in C and have no idea how this is implemented (I'm so used to OOP). Can a struct simply be called multiple times within the main() method to create multiple list nodes (one for each call)? Or am I thinking about this the wrong way?

Thanks,
Jisoo23
 
You will need to use a self-referential structure. Something similar to the following;

Code:
#include <stdlib.h>

struct linklist
{
   char field1[21];
   int  field2;
   struct linklist *next;
};

struct linklist *head;
struct linklist *tail;

main()
{
 struct linklist *p;

 head = (struct linklist *)malloc(sizeof(struct linklist));
 tail = head;
 strcpy(tail->field1,"First entry");
 tail->field2 = 1;
 tail->next =
        (struct linklist *)malloc (sizeof(struct linklist));
 tail = tail->next;
 strcpy(tail->field1,"Another entry");
 tail->field2 = 2934;
 tail->next = NULL;


 p = head;
 while (p != NULL)
 {
        printf("[%d] %s\n",p->field2,p->field2);
        p = p->next;
 }
}

 
Another example:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>


/*simple linked list example*/
typedef struct data {
int data;
struct data *nxt;
} dtype;


dtype *initnode(void);
void printlist(dtype *);


int main(int argc, char **argv) {
int y = 0,p;
dtype *one, *two, *three = NULL;


         if (argc < 2) {printf("Must specify number of nodes for list\n"); return -1;}
         p = atoi(argv[1]);
		 printf("Allocating %d elements for list\n",p);
         srand(time(NULL));
         if ( (one = initnode()) ==  NULL) {
              printf("Cannot allocate memory for list head\n");
              return -1;

         }
         two = one;
         /*work*/
         while (y < p) {three = initnode(); two->data = (y + rand() % 100); two->nxt = three; two = three; y++;}
         printlist(one);
return 0;
}




void printlist(dtype *hd) {
int p = 0;
dtype *ptr = hd;


              while (ptr != NULL) {
                    printf("List node at place[%d], data = %d, address = %p\n",p++,ptr->data,ptr);
                    ptr = ptr->nxt;
               }
}



dtype *initnode(void) {
dtype *new = NULL;

            if ( (new = malloc(sizeof(dtype))) == NULL) {return NULL;}
            return new;
}

 
A struct is just a class where all members are public.

Just wondering whether you have tried creating a singly linked list in Java without using container classes. If you went through that exercise, creating one in C is no different except for memory allocation (new/malloc) and pointers/references.
 
Unfortunately I've only created singly linked lists with container classes =/ But what marsd has shown me gives me a very good idea on how to go about this.

Thanks a bunch!
Jisoo23
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top