Skip to main content

Posts

Showing posts from April, 2011

Program to create a Linked List in C

An array is a commonly used data structure in most of the languages. Because it is simple, it needs O(1) time for accessing elements. It is also compact. But an array has a serious drawback - it can not grow or shrink. You need to estimate the array size and define it during compile time. This drawback is not present a linked list. A linked list is a data structure which can grow or shrink dynamically.  A linked list has nodes each of which contain  contain  data and a link to next node . These nodes are dynamically allocated structures. If you need more nodes, you just need to allocate memory for these and link these nodes to the existing list. The nodes of a linked list have to be defined as self-referential structures in C. That is structures with data members and one member which is a pointer to the structure of same type.  This pointer will work as a link to next node. struct node { int data; struct node * next; //pointer to another node }; typedef struct