Creating, printing and free linked lists source codeThis snippet submitted by Syed Rafey Husain on 2005-11-22. It has been viewed 2521 times.Rating of 5.3 with 6 votes
struct node
{
int i;
struct node *pNext;
};
typedef struct node Node;
Node* createList()
{
Node *ph, *pc = 0;
ph = (Node *) malloc(sizeof(Node));
ph->i = 0;
ph->pNext = 0;
pc = ph;
for (int i=1; i<=10; i++)
{
pc->pNext = (Node *) malloc(sizeof(Node));
pc = pc->pNext;
pc->i = i;
pc->pNext = 0;
}
return ph;
}
void printList(Node *ph)
{
Node *pc=ph;
while(pc)
{
printf("%d\n", pc->i);
pc = pc->pNext;
}
}
void freeList(Node *ph)
{
Node *pn, *pc=ph;
while(pc)
{
pn = pc->pNext;
free(pc);
pc=pn;
}
}
More C and C++ source code snippets Add a snippet! |