Data Structure
Data Structure
DEFINITION:-
ARRAY
An array can be defined as a finite collection of homogeneous element.
This means that an array can store either all integers, all floating point number, all characters or any
other complex datatype but all of same type.
The individual elements within the array can be designated by an index. An index is also called a
subscript.
Array Elements
For Example:-
Array 2 4 8 9 5 3
0 1 2 3 5 6 Array Indexes
INSERTION
An element can be inserted in an array at a particular location.
ALGORITHM
1. Start
2. Create an array of a desire datatype and size
3. Initialize a variable i as 0
4. Enter the element at ith index of the array
5. Increment i by 1
6. Repeat steps 4 and 5 until the end of the array
7. Stop
CODING
#include <stdio.h>
int main()
int arr[5];
scanf("%d", &size);
scanf("%d", &arr[i]);
printf("Enter the position where you want to insert the element: ");
scanf("%d", &position);
return 1;
scanf("%d", &newValue);
{
arr[i] = arr[i - 1];
arr[position] = newValue;
size++;
printf("Updated array:\n");
return 0;
OUTPUT
AIM: Write a program to delete an element from an array.
DEFINITION:-
ARRAY
An array can be defined as an finite collection of homogeneous element.
This means that an array can store either all integers, all floating point number, all characters or
any other complex datatype but all of same type.
The individual elements within the array can be designated by an index. An index is also called a
subscript.
For Example: -
DELETION
It is the operation that removes an element from a given location of a list.
ALGORITHM
LA is a linear array with an elements and K is a positive integer such that K<=N.
1. Start
2. Set J=K
3. Repeat steps 4 and 5 while J<N
4. Set LA [J] = LA[J+1]
5. Set J = J+1
6. Set N = N-1
7. Stop
CODING
#include <stdio.h>
int main(void)
scanf("%d", &n);
scanf("%d", &arr[i]);
scanf("%d", &index);
}
else
return 0;
OUTPUT
AIM: Write a program in C for implementation of linked list using array.
DEFINITION:-
LINKED LIST
Link structures are a completely different way to represent a list of items. Each elements of the list
is made to point to the next element in the list.
A list consisting four names i.e. A, B, C and D is represented using link structures such a list is
called linked list or a linear linked list.
ALGORITHM
1. Create a class node which has two attributes data and next. Next is a pointer to the next
node in the linked list.
2. Create another class which has two attributes head and tail.
3. Add node function will add a new node to the list.
a) Create a new node.
b) It first checks, whether the head = NULL which means the list is empty.
c) If the list is empty, both head and tail will point to the newly added node.
d) List ≠ empty the new node will be added to end of the list such that tails next will
point to a newly added node. This new node will become the new tail of the list.
4. Repeat step 2 and 3 till whole of list is constructed.
5. Point tail = NULL
6. Stop
CODING
#include<stdio.h>
};
void Createlist(); void Freenode(int); void Display(); void Insert(int,int); void Delete(int); int p,
avail=0;
void main()
while(ch!=4)
printf("\n1-DISPLAY"); printf("\n2-INSERT");
printf("\n3-DELETE"); printf("\n4-QUIT");
switch(ch)
{
case 1 :
p=n;
Insert(p,x); break;
case 3:
printf("\n Enter the node after which the node will be deleted:"); scanf("%d",&n); p=n;
break; default:
void Freenode(int q)
node[q].next=avail; avail=q;
return;
void Createlist()
{
int x; char c;
p=Getnode();
c=getchar(); if(c=='y'||c=='Y')
node[p].next= -1;
else return;
void Display()
{ p=0;
while(node[p].next!=-1)
{ printf("\n%d\t%d\t%d:",p,node[p].info,node[p].next); p=node[p].next;
int q; if(r==-1)
return;
}
void Delete(int r)
int q;
if(r==-1||node[r].next==-1)
return;
OUTPUT
AIM: Write a program in C to insert a node at the beginning of Singly Linked List.
DEFINITION
LINKED LIST
Link structures are a completely different way to represent a list of items. Each elements of the list
is made to point to the next element in the list.
A list consisting four names i.e. A, B, C and D is represented using link structures such a list is
called linked list or a linear linked list.
Step
else
4. Stop
}
CODING
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
struct node*next;
}*head;
void displayList();
int main()
{
int n,data;
scanf("%d",&n);
createlist(n);
displayList();
scanf("%d",&data);
insertNodeAtBeginning(data);
displayList();
return 0;
void createlist(int n)
struct node*newNode,*temp;
int data,i;
if(head==NULL)
else
scanf("%d",&data);
head->data=data;
head->next=NULL;
temp=head;
for(i=2;i<=n;i++)
if(newNode==NULL)
break;
else
scanf("%d",&data);
newNode->data=data;
newNode->next=NULL;
temp->next=newNode;
temp=temp->next;
struct node*newNode;
newNode=(struct node*)malloc(sizeof(struct node));
if(newNode==NULL)
else
newNode->data=data;
newNode->next=head;
head=newNode;
void displayList()
struct node*temp;
if(head==NULL)
printf("list is empty");
else
temp=head;
while(temp!=NULL)
printf("data=%d\n",temp->data);
temp=temp->next;
}
OUTPUT
AIM: Write a program in C to delete a first node from singly linked list.
DEFINITION
LINKED LIST
Link structures are a completely different way to represent a list of items. Each elements of the list
is made to point to the next element in the list.
A list consisting four names i.e. A, B, C and D is represented using link structures such a list is
called linked list or a linear linked list.
ALGORITHM DELNODE ()
{
CODING
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
}*head;
void deleteFirstNode();
void displayList();
int main()
{
int n, choice;
scanf("%d", &n);
createList(n);
displaylist();
scanf("%d", &choice);
if(choice == 1)
deleteFirstNode();
displayList();
return 0;
void createList(int n)
int data, i;
if (head == NULL)
else
head->data = data;
head->next = NULL;
temp = head;
if(newNode == NULL)
break;
else
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
void deleteFirstNode()
{
if(head == NULL)
else
toDelete = head;
head = head->next;
free(toDelete);
void displayList()
if(head == NULL)
printf("List is empty.");
else
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
OUTPUT