Heres a good start.....
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Rec
{
char *name;
int prc;
int col;
}Record;
typedef struct node
{
Record r;
struct node *previous;
struct node *next;
}Node;
typedef struct list
{
Node *head;
Node *tail;
}List;
List *CreateList()
{
List *l = calloc(1, sizeof(List));
if(l == NULL)
return NULL;
else
{
l->head = NULL;
l->tail = NULL;
return l;
}
}
Node *CreateNode(char *name, int Prc, int Col)
{
Node *n = calloc(1, sizeof(Node));
if(n == NULL)
return NULL;
else
{
n->next = NULL;
n->previous = NULL;
if(name == NULL)
{
free(n);
return NULL;
}
else
{
//You may use strdup here.
/*n->r.name = strdup(name);*/
//Init.
n->r.name = malloc((strlen(name) + 1) * sizeof(char));
strncpy(n->r.name, name, strlen(name));
n->r.name[strlen(name)] = '\0';
n->r.prc = Prc;
n->r.col = Col;
//Return the node.
return n;
}
}
}
//Add a new node to the end of the list
int AddNode(List *l, char *Nname, int Prc, int Col)
{
if(l == NULL)
return -1;
if(Nname == NULL)
return -2;
Node *n = CreateNode(Nname, Prc, Col);
if(n)
{
if(l->head == NULL)
l->head = l->tail = n;
else
{
n->previous = l->tail;
l->tail->next = n;
l->tail = n;
}
return 0;
}
else
return -3;
}
void PrintList(List *l)
{
Node *temp = l->head;
printf("Printing List:\n");
while(temp)
{
printf("%s\t%d\t%d\n", temp->r.name, temp->r.prc, temp->r.col);
temp = temp->next;
}
}
int main(int argc, char *argv[])
{
List *l = CreateList();
AddNode(l, "Nick", 24,45);
AddNode(l, "Tom", 26,85);
AddNode(l, "Snow", 56,42);
PrintList(l);
system("PAUSE");
return 0;
}