Thursday, May 31, 2012

What is Data Structure in C Programming


A data structure is a group of items ( datas ) organized in many different ways ( logical or mathematical ),in which each item is identified by it's own identifier.Each of the datas are known as a member of the data structure.Most often we face situations in programming where data is dynamic in nature , that is the number of data items keep changing during the program execution.For example , consider the program for processing the list of customers of a company.The list grows when new customer names are added and shrinks when names are deleted.When the list grows it needs more memory space to be allocated to accommodate the additional datas.In the reverse case the memory space is to be deleted.This type of situation can be handled more efficiently using data structure in conjunction with dynamic memory management techniques.Data structures provide flexibility in adding , deleting or rearranging data items at run time.Dynamic memory management technique permits us to allocate additional memory space or to release unwanted space at run time.In other words data structure is a collection of data elements whose organization is characterized by assessing operations that are used to store and retrieve the individual data elements,the implementation of the composite data members in an abstract data type.An abstract data type can be defined as a data type whose properties are specified independently of any particular implementation.

The data structures in c are classified in the following categories :-
• Linear data structures
• Nonlinear data structures

In the linear data structure , data can be processed one by one sequentially.Linear data structures contains the following types of data structures -
1. Array
2. Linked list
3. Stack and Queues
A data structure in which insertion and deletion of datas is not possible in a linear fashion is called a nonlinear data structure.Example :-
1. Tree
A list is a set of items organized sequentially. An array is an example of a list.In an array , the sequential organization of datas is provided implicitly by it's index.We use array index for accessing or manipulating array elements.The dis-advantage of using an array to store data elements is that the size of the array is pre-defined.A programmer does not have any control on defining the size of the array at run time.
To overcome this problem, 'C' provides the concept of linked lists.
In a linked list, the elements of the list are stored somewhere in the memory rather that in contiguous blocks. All the elements of the list are referred to by the link between them.A linked list is a sequence of data elements in which each element in the sequence points to it's successor.In an array the index refers the address of the location while in linked list every node contains a pointer which holds the address of the next node.A linked list is defined as collection of nodes .Each nodes has two parts :-

• Information
• Next address (pointer to the next node)

The information field contains the actual element of the list.The next address field contains the address of the next node in the list.Such an address which is used to access the address of a particular node is called a pointer.Another pointer variable which points the structure is called structure pointer or external pointer.The entire linked list is accessed by an external pointer that points the first node in the list.The next address field of the last node in a linked list contains null.
This null is used to signal the end of a linked list.
The following figure illustrates a linked list.

info stands for information part and next stands for next address.
Building a Linked list
To create a linked list , following steps are required :-
Declare a structure that defines the list elements.
Within the structure body declare the variable(s) of each node that will contain informations and the pointer of each node.
Declare the external pointer and the node(s) of that structure type.
A node may contain any number of information fields.



#include < stdio.h >
#include < conio.h >
void main()
{
struct name
{
char name[50];
char add[50];
long ph_no;
};
struct name n = {"****", "Burdwan", 255};
struct name *sp;
sp = &n;
clrscr();
printf("The value stored in the node 'n' is :\n");
printf(" Name = %s \n", n.name);
printf(" Address = %s \n", n.add);
printf("Phone number = %ld \n", n.ph_no);
printf("The value stored in the node 'n' , using structure pointer is :\n");
printf(" Name = %s \n", sp->name);
/* -> is known as indirect or pointer member selector while dot (.) is known as direct member selector*/
printf(" Address = %s \n", sp->add);
printf("Phone number = %ld \n", sp->ph_no);
getch();
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner