0% found this document useful (0 votes)
10 views23 pages

Data Structure

The document provides a series of C programs demonstrating array and linked list operations, including insertion and deletion of elements. It explains the definitions of arrays and linked lists, along with algorithms and code implementations for inserting and deleting nodes in linked lists and elements in arrays. Each section includes coding examples and outputs for clarity.

Uploaded by

harshkantiwal187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views23 pages

Data Structure

The document provides a series of C programs demonstrating array and linked list operations, including insertion and deletion of elements. It explains the definitions of arrays and linked lists, along with algorithms and code implementations for inserting and deleting nodes in linked lists and elements in arrays. Each section includes coding examples and outputs for clarity.

Uploaded by

harshkantiwal187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

AIM: Write a program for inserting an element into an array.

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];

int size, position, newValue;

printf("Enter the size of the array: ");

scanf("%d", &size);

printf("Enter the elements of the array:\n");

for (int i = 0; i < size; i++) {

scanf("%d", &arr[i]);

printf("Enter the position where you want to insert the element: ");

scanf("%d", &position);

if (position < 0 || position > size) {

printf("Invalid position! Position should be between 0 and %d\n", size);

return 1;

printf("Enter the value to insert: ");

scanf("%d", &newValue);

for (int i = size; i > position; i--)

{
arr[i] = arr[i - 1];

arr[position] = newValue;

size++;

printf("Updated array:\n");

for (int i = 0; i < size; i++) {

printf("%d ", arr[i]);

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.

We have to delete an array available at the kth position of LA.

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)

int i, n, index, arr[10];

printf("Enter the size of the array: ");

scanf("%d", &n);

printf("Enter the elements of the array: \n");

for (i = 0; i < n; i++)

printf("arr[%d] = ", i);

scanf("%d", &arr[i]);

printf("Enter the index of the element to be deleted: ");

scanf("%d", &index);

if (index >= n+1)

printf (" \n Deletion is not possible in the array.");

}
else

for (i = index; i < n - 1; i++)

arr[i] = arr[i + 1];

printf("The array after deleting the element is: ");

for (i = 0; i < n - 1; i++)

printf("%d ", arr[i]);

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.

An element in the list is called a node.

The Node have two part’s : Data and Next

 Data parts contain the information about the element.


 Next part contains a pointer to the next elements or a node 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.

CREATION OF A LINKED LIST


A node of a linked list is essentially a structure because its contains data of different types.
Information parts contains a pointer that can point to a node i.e. to itself or some other node such
structure are called self referential structure.

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>

#include<conio.h> #define TRUE 1

#define SIZE 10 struct link

int info; int next;

};

struct link node[SIZE]; int Getnode();

void Createlist(); void Freenode(int); void Display(); void Insert(int,int); void Delete(int); int p,
avail=0;

void main()

int ch=1,i,n,x; clrscr();

/Creation of available list/ for(i=0;i<SIZE-1;i++)

node[i].next=i+1; node[SIZE-1].next=-1; printf("\n Create a List:"); Createlist();

while(ch!=4)

printf("\n1-DISPLAY"); printf("\n2-INSERT");

printf("\n3-DELETE"); printf("\n4-QUIT");

printf("\n Enter your choice:"); scanf("%d",&ch);

switch(ch)
{

case 1 :

Display(); break; case 2:

printf("\n Node insertion:after which node:"); scanf("%d",&n);

p=n;

printf("\n Enter the item for insertion:"); scanf("%d",&x);

Insert(p,x); break;

case 3:

printf("\n Enter the node after which the node will be deleted:"); scanf("%d",&n); p=n;

Delete(p); break; case 4:

break; default:

printf("\n Wrong choice!Try again:"); }

int Getnode() { if (avail==-1) {

printf("\n Overflow:"); exit(0);

p=avail; avail=node[avail].next; return p;

void Freenode(int q)

node[q].next=avail; avail=q;

return;

void Createlist()

{
int x; char c;

p=Getnode();

printf("\n Enter an item to be inserted:"); scanf("%d", &x);

node[p].info=x ; node[p].next=-1; while(TRUE)

printf("\n Enter the choice(y/n):"); fflush(stdin);

c=getchar(); if(c=='y'||c=='Y')

printf("\n Enter an item to be inserted:"); scanf("%d",&x); Insert(p,x);

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;

} printf("\n%d\t%d\t%d:",p,node[p].info,node[p].next); } void Insert(int r,int x)

int q; if(r==-1)

printf("\n void insertion:"); return;

q=Getnode(); node[q].info=x; node[q].next=node[r].next; node[r].next=q;

return;
}

void Delete(int r)

int q;

if(r==-1||node[r].next==-1)

printf("\n void deletion:"); return;

q=node[r].next; node[r].next=node[q].next; Freenode(q);

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.

An element in the list is called a node.

The Node have two part’s : Data and Next

 Data parts contain the information about the element.


 Next part contains a pointer to the next elements or a node 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.

INSERTION IN A LINKED LIST


It involves following steps:

 Take a node store data into nodes.


 Search the location in the list where the node is to be inserted.
 Insert the node.
 Location where the node is to be inserted in a linked list can either be at the beginning
Or at any other position in the list.

ALGORITHM INSERT AT HEAD ()


{

Step

1. Take a node in ptr


2. Read data ( ptr )
3. if ( first = = NULL ) then
{
3.1 first = ptr;
3.2 NEXT ( first ) = NULL;
}

else

3.1 Next (ptr) = first;

3.2 first = ptr;

4. Stop
}

CODING
#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node*next;

}*head;

void createlist(int n);

void insertNodeAtBeginning(int data);

void displayList();

int main()
{

int n,data;

printf("enter the total number of nodes:");

scanf("%d",&n);

createlist(n);

printf("\ndata in the list\n");

displayList();

printf("\nenter data to insert at beginning of the list:");

scanf("%d",&data);

insertNodeAtBeginning(data);

printf("\n data in the list\n");

displayList();

return 0;

void createlist(int n)

struct node*newNode,*temp;

int data,i;

head=(struct node*)malloc(sizeof(struct node));

if(head==NULL)

printf("unable to allocate memory");

else

printf("enter the data of node 1:");

scanf("%d",&data);
head->data=data;

head->next=NULL;

temp=head;

for(i=2;i<=n;i++)

newNode=(struct node*)malloc(sizeof(struct node));

if(newNode==NULL)

printf("unable to allocate memory");

break;

else

printf("enter the data of node%d:",i);

scanf("%d",&data);

newNode->data=data;

newNode->next=NULL;

temp->next=newNode;

temp=temp->next;

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");

void insertNodeAtBeginning(int data)

struct node*newNode;
newNode=(struct node*)malloc(sizeof(struct node));

if(newNode==NULL)

printf("unable to allocate memory");

else

newNode->data=data;

newNode->next=head;

head=newNode;

printf("DATA INSERTED SUCCESSFULLY\n");

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.

An element in the list is called a node.

The Node have two part’s : Data and Next

 Data parts contain the information about the element.


 Next part contains a pointer to the next elements or a node 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.

DELETING A NODE FROM A LINKED LIST


It involves two steps:

1. Search the list for the node which is to be deleted.


2. Delete the node.

ALGORITHM DELNODE ()
{

1. If ( Data ( First ) = ‘val’ )


/ * check if the starting node is desired one * /

1.1 ptr = first ;


1.2 first = next ( first );
1.3 free ( ptr );
1.4 Stop
}

2. while ( ptr ! = NULL )


2.1 if ( Data ( ptr ) = ‘val’ )
{
Next ( Back ) = Next ( ptr );
Free ( ptr );
Break;
}
2.2 back = ptr ;
2.3 ptr = Next ( ptr );
}
}
3. Stop
}

CODING
#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node *next;

}*head;

void createList(int n);

void deleteFirstNode();

void displayList();

int main()
{

int n, choice;

printf("Enter the total number of nodes: ");

scanf("%d", &n);

createList(n);

printf("\nData in the list \n");

displaylist();

printf("\nPress 1 to delete first node: ");

scanf("%d", &choice);

if(choice == 1)

deleteFirstNode();

printf("\nData in the list \n");

displayList();

return 0;

void createList(int n)

struct node *newNode, *temp;

int data, i;

head = (struct node *)malloc(sizeof(struct node));

if (head == NULL)

printf("Unable to allocate memory.");

else

printf("Enter the data of node 1: ");


scanf("%d", &data);

head->data = data;

head->next = NULL;

temp = head;

for(i=2; i<=n; i++)

newNode = (struct node *)malloc(sizeof(struct node)

if(newNode == NULL)

printf("Unable to allocate memory.");

break;

else

printf("Enter the data of node %d: ", i);

scanf("%d", &data);

newNode->data = data;

newNode->next = NULL;

temp->next = newNode;

temp = temp->next;

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");

void deleteFirstNode()
{

struct node *toDelete;

if(head == NULL)

printf("List is already empty.");

else

toDelete = head;

head = head->next;

printf("\nData deleted = %d\n", toDelete->data);

free(toDelete);

printf("SUCCESSFULLY DELETED FIRST NODE FROM LIST\n");

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

You might also like