Professional Documents
Culture Documents
Data Structure
Is a means of organizing data in memory. Way in which data are stored for efficient search and retrieval.
Data Structures
Simplest is one dimensional array. Drawback is consecutive memory locations are required for storing the elements of the array. If the elements are stored in nonconsecutive memory locations , they need to be linked together.
Data Structures(cont)
The new data structure is linked list. Linked list is a list whose order is given by links from one item to the next. It is represented as
ptr inf ptr
NULL
inf
Linked List
In the previous linked list, each node provides two details. One is the information and the other is a pointer to the next node. If the two nodes are node1 and node2, then the pointer of node1 is pointing to the address of node2.
Hence the linked list has the advantage that the memory need not be contiguous. The main drawback of linked list as compared to arrays is that extra memory is required for storing the pointer variable for each node.
Adding nodes
Adding at end
New node
Creating nodes(implementation)
struct node *new,*head,*temp; new=(struct node *)malloc(sizeof(struct node)); printf(Enter the name and rollno); scanf(%s %d,new->name,&new->rollno); if (head == NULL) //if creating the first node { head=new; new->next=NULL; temp=new; }
Implementation(cont)
else // if not the first node { temp->next=new; new->next=NULL; temp=new; }
Displaying nodes
if (head==NULL) printf(no nodes to display); for (disp=head;disp!=NULL;disp=disp->next) { printf(name is %s,disp->name); printf(rollno is %d,disp->rollno); }
Exercise
1. Write a C program to implement a linked list with the operations creating, modifying a node,counting the number of nodes,displaying the nodes. 2. Write a C program to perform the basic arithmetic operations on two complex numbers ( A complex number has a real part and an imaginary part)
It allows to traverse in both the directions Each node has three parts 1.information part 2.Link to the next node 3.Link to the previous node
It is represented as
Creation
if (head == NULL) //if creating the first node { head=new; new->next=NULL; new->prev=NULL; temp=new; }
Creation(cont.)
else // if not the first node { temp->next=new; new->prev=temp; new->next=NULL; temp=new; }
Exercise
Write a program in C for implementing double linked list basic operations like creation, deletion, modification, counting and displaying the nodes.