Data Structures Module 1
Data Structures Module 1
Module-1
1. Array: Array is a sequence of n items of the same data type that are stored
contiguously in computer memory and made accessible by specifying an index. The
index usually varies from 0 to n-1. Each element in array can be accessed randomly.
Strings are special type of arrays in which each element is a character. Strings
composed of zeroes and ones are called binary strings or bit strings.
2. Linked List: Linked list is a sequence of zero or more elements called nodes. Each
node is comprised of a data component and a pointer component. The pointer points
to the adjacent element. In a singly linked list, each node has a pointer to the next
element. In a doubly linked list, each node has a pointer to the next node as well as a
pointer to the previous node. To access a particular node in a singly linked list, we
have to start with the first node and traverse the pointer chain until the particular node
is reached. A doubly linked list can be traversed in both directions, from beginning to
end or end to beginning.
3. Stack: A stack is a type of list in which insertion and deletion can be done only at one
end. This end is called top. Insertion of an element into a stack is called push
operation and deletion of an element from the stack is called pop operation. Since,
push and pop can be done only at the top, the stack works in a Last-in-First-out
fashion. Thus stack is also called a LIFO data structure.
4. Queue: A queue is a list from which elements are deleted from one end called the
front and new elements are added to the other end called the rear. Thus, a queue
operates in a first-in-First-Out fashion. Queue is also called a FIFO data structure. A
priority queue is a queue in which each element in the queue has a priority associated
with it, and an element with the highest priority will be deleted first.
What is an Array?
Array is a sequence of n items of the same data type that are stored contiguously in
computer memory and made accessible by specifying an index. The index usually varies
from 0 to n-1. Each element in array can be accessed randomly.
What is a linear array?
A linear array is a list of a finite number n of homogenous data elements such that the
elements of the array are referenced by an index. Indexes are also called subscripts. Most
computer languages supports 0-based indexing where as some computer languages support
1- based indexing. In 0-based indexing, index values varies from 0 to n-1. In 1-based
indexing, index values varies from 1 to n. The elements of a linear array are stored in
successive memory locations.
Linked List
A linked list is a linear data structure, in which the elements are not stored at contiguous
memory locations. Each element in a linked list are known as nodes. The nodes in a linked
list are linked using pointers. Each node stores the data and the address of the next node.
The last node in a linked list points to a special pointer called NULL. We use a pointer
variable to point to the first node in the list. Starting with this pointer, we can traverse the
entire linked list by following the pointer links of each node.
Random data structure: Any element can Sequential data structure: Each node can
be directly accessed by referring to its be accessed only after traversing through
index. Time taken to access any element is all previous nodes. Time taken to access a
independent of its position. Can be used node depends on its position (Max time for
for both sequential and random access last node, minimum time for first one).
applications (e.g. mark processing or bank Suitable only for sequential access
balance enquiry) applications (e.g. mark processing)
No memory overhead Array of 10 int takes Memory overhead (one pointer per node)
104=40 bytes Array of 10 char takes Linked list of 10 int takes 10(4 + 41= 80
10.1=10 bytes bytes Linked list of 10 char takes 1041 +
4) = 50 bytes
Insertion and deletion of array elements Insertion and deletion of list nodes
requires all succeeding elements to be requires only the preceding and
shifted down or up. succeeding nodes to be modified.
struct Node {
int data;
struct Node* next;
};
A linked list is represented by a pointer to the first node of the linked list. The first node is
called the head of the linked list. If the linked list is empty, then the value of the head points
to NULL. To create a linked list, first we dynamically allocate memory for the head node.
Consecutive nodes are created by dynamically allocating memory to the *next pointer of
each node. The *next pointer of last node will point to NULL.
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct node{
int data;
struct node *next;
};
struct node *first,*current;
int temp;
void main()
{
clrscr();
first=(struct node *)malloc(sizeof(struct node));
current=first;
printf("Enter data for the nodes. Enter 0 to stop\n");
while(current!=NULL)
{
scanf("%d",&temp);
current->data=temp;
if(temp==0)
current->next=NULL;
else
current->next=(struct node *)malloc(sizeof(struct node));
current=current->next;
}
current=first;
while(current->next!=NULL)
{
printf("%d\n",current->data);
current=current->next;
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node{
int data;
struct node *next;
};
struct node *first,*current;
void main()
{
int count,search;
clrscr();
first=(struct node *)malloc(sizeof(struct node));
current=first;
printf("Enter the data. enter 0 to stop\n");
while(1)
{
scanf("%d",¤t->data);
if(current->data==0)
{
current->next=NULL;
break;
}
current->next=(struct node *)malloc(sizeof(struct node));
current=current->next;
}
printf("Enter the number to serach\n");
scanf("%d",&search);
count=0;
current=first;
while(current->next!=NULL)
{
count++;
if(current->data==search)
{
printf("Found at Node No:%d\n",count);
}
current=current->next;
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node{
int data;
struct node *next;
};
struct node *first,*current,*prev;
int a;
void main()
{
clrscr();
first=(struct node *)malloc(sizeof(struct node));
current=first;
printf("Enter the data. 0 to stop\n");
while(1)
{
scanf("%d",¤t->data);
if(current->data==0)
{
current->next=NULL;
break;
}
else
{
current->next=(struct node *)malloc(sizeof(struct node));
current=current->next;
}
}
printf("Linked List is \n");
current=first;
while(current->next!=NULL)
{
printf("%d\n",current->data);
current=current->next;
}
printf("Enter the data to delete\n");
scanf("%d",&a);
current=first;
while(current!=NULL && current->data!=a)
{
prev=current;
current=current->next;
}
if(current!=NULL)
prev->next=current->next;
printf("Linked List is \n");
current=first;
while(current->next!=NULL)
{
printf("%d\n",current->data);
current=current->next;
}
getch();
}
Explain header linked lists
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node{
struct node *prev;
int data;
struct node *next;
};
struct node *first,*current,*temp,*last;
int a,d;
void main()
{
clrscr();
first=(struct node *)malloc(sizeof(struct node));
first->prev=NULL;
current=first;
printf("Enter data. 0 to stop\n");
while(1)
{
scanf("%d",¤t->data);
if(current->data==0)
{
current->next=NULL;
last=current;
break;
}
else
{
current->next=(struct node *)malloc(sizeof(struct node));
current->next->prev=current;
current=current->next;
}
}
printf("Forward Traversal\n");
current=first;
while(current->next!=NULL)
{
printf("%d\n",current->data);
current=current->next;
}
printf("Backward Traversal\n");
current=last->prev;
while(current!=NULL)
{
printf("%d\n",current->data);
current=current->prev;
}
getch();
}
**********