You are on page 1of 19

DATA STRUCTURES AND ALGORITHMS

Data Structure

Is a means of organizing data in memory. Way in which data are stored for efficient search and retrieval.

Abstract data type(ADT)


Primitive types eg: int, float, char etc Structured types eg: struct, union, etc New data types can be made from primitive types or structured types-called ADT. egs: List, queue, set, stack, tree

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.

Linked List (cont)


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

Linked List implementation in C


struct node { int rollno; char name[10]; struct node *next; }; Here rollno and name are the information and next is the pointer to the next 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)

Double Linked List


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

Double Linked List

It is represented as

Double Linked List


struct node { int rollno; char name[10]; struct node *next; struct node *prev; };

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.

You might also like