Write a program to insert an element at end of an
array.
#include <stdio.h>
void main()
{
int position, i, n, value,ch, arr[100];
printf("C Program to insert element at end of Array\n");
printf("First enter number of elements you want in Array\n");
scanf("%d", &n);
arr[n];
for(i = 0; i < n; i++)
{
printf("Please give value for index %d : ",i);
scanf("%d",&arr[i]);
}
printf("Let's Insert Element at end \n ");
printf("Please give a number to insert at end \n");
scanf("%d", &value);
arr[n] = value;
printf("Element %d is inserted at %d index \n",value,n);
printf("New Array is \n ");
for(i = 0; i < n+1; i++)
{
printf("%d \t",arr[i]);
}
}
Output:
C program to insert elements at end of Array
First enter number of elements you want in Array
5
Please give value for index 0 : 1
Please give value for index 1 : 2
Please give value for index 2 : 3
Please give value for index 3 : 4
Please give value for index 4 : 5
Lets Insert Element at end
Please give a number to insert at end 6
Element 6 is inserted at 5 index
New array is
1 2 3 4 5 6
Write a program to insert an element in a array at a given
position
#include <stdio.h>
int main()
int array[50], position, c, n, value;
printf("Enter number of elements in the array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++);
scanf("%d", &array[c]);
printf("Please enter the location where you want to insert an new
element\n");
scanf("%d", &position);
printf("Please enter the value\n");
scanf("%d", &value);
for (c = n - 1; c >= position - 1; c--)
array[c+1] = array[c];
array[position-1] = value;
printf("Resultant array is\n");
for (c = 0; c <= n; c++)
printf("%d\n", array[c]);
return 0;
}
Output:
Enter number of elements in array
Enter 5 elements
2
3
1
5
6
Enter the location where you wish to insert an element
3
Enter the value to insert
8
Resultant array is
2
3
8
1
5
6
Write a program to delete an element at end of array.
#include <stdio.h>
void main()
{
int position, i, n, value,ch;
printf("C Program to delete element at end of Array\n");
printf("First enter number of elements you want in Array\n");
scanf("%d", &n);
int arr[n];
for(i = 0; i < n; i++)
{
printf("Please give value for index %d : ",i);
scanf("%d",&arr[i]);
}
value=arr[n-1]; //assigning last value in value variable
printf("Element %d is deleting at %d index \n",value,n-1);
n=n-1;//here decreasing value to reduce size of array
printf("New Array after deleting element at end \n ");
for(i = 0; i < n; i++)
{
printf("%d \t",arr[i]);
}
}
Output:
Please give value for index 3 : 4
Please give value for index
Element 5 is deleting at 4 index
New array after deleting element at end
1 2 3 4
Write a program to delete an element in the array at given
position
#include <stdio.h>
int main()
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.\n");
else
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
return 0;
}
Output:
Enter number of elements in array
3
Enter 3 elements
1
2
3
Enter the location where you wish to delete element
2
Resultant array is
1
3
Write a program to merge two one dimensional array
elements.
#include <stdio.h>
input : arr ( array of integer ), size
to read ONE-D integer array from standard input device (keyboard).
void readArray(int arr[], int size)
{
int i = 0;
printf("\nEnter elements : \n");
for (i = 0; i < size; i++) {
printf("Enter arr[%d] : ", i);
scanf("%d", &arr[i]);
}
}
input : arr ( array of integer ), size
to display ONE-D integer array on standard output device (moniter).
void printArray(int arr[], int size)
{
int i = 0;
printf("\nElements are : ");
for (i = 0; i < size; i++) {
printf("\n\tarr[%d] : %d", i, arr[i]);
}
printf("\n");
}
void merge(int arr1[], int size1, int arr2[], int size2, int arr3[])
{
int i = 0, j = 0;
for (i = 0; i < size1; i++)
arr3[i] = arr1[i];
for (i = 5, j = 0; i < size2 + 5; i++, j++)
arr3[i] = arr2[j];
}
int main()
{
int arr1[5];
int arr2[5];
int arr3[10];
readArray(arr1, 5);
readArray(arr2, 5);
merge(arr1, 5, arr2, 5, arr3);
printArray(arr3, 10);
return 0;
}
Output:
Enter elements :
Enter arr[0] : 12
Enter arr[1] : 23
Enter arr[2] : 34
Enter arr[3] : 45
Enter arr[4] : 56
Enter elements :
Enter arr[0] : 11
Enter arr[1] : 22
Enter arr[2] : 33
Enter arr[3] : 44
Enter arr[4] : 55
Elements are :
arr[0] : 12
arr[1] : 23
arr[2] : 34
arr[3] : 45
arr[4] : 56
arr[5] : 11
arr[6] : 22
arr[7] : 33
arr[8] : 44
arr[9] : 55
Write a program for implementing a single linked list
#include<stdio.h>
#include<stdlib.h>
//Represent a node of singly linked list
struct node{
int data;
struct node*next;
};
structnode*head,*tail=NULL;
void addNode(intdata){
struct node*newNode=(structnode*)malloc(sizeof(struct node));
newNode->data=data;
newNode->next=NULL;
//Checks if the list is empty
if(head==NULL){
head=newNode;
tail=newNode;
}
else
{
tail->next=newNode;
tail=newNode;
}
}
void display(){
struct node *current=head;
if(head==NULL){
printf("List is empty\n");
return;
}
printf("Nodesof singly linked list:\n");
while(current != NULL) {
printf("%d",current->data);
current=current->next;
}
printf("\n");
int main()
{
addNode(1);
addNode(2);
addNode(3);
addNode(4);
display();
return 0;
}
Output:
Nodes of single linked list:
1 2 3 4
Write a program for implementing a circular linked list.
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
struct node * nextptr;
}*stnode;
void ClListcreation(int n);
void displayClList();
int main()
{
int n;
stnode = NULL;
printf("\n\n Circular Linked List : Create and display a circular linked list :\n");
printf("-----------------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
ClListcreation(n);
displayClList();
return 0;
}
void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;
if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node %d : ", i);
scanf("%d", &num);
newnode->num = num;
newnode->nextptr = NULL; // next address of new node set as NULL
preptr->nextptr = newnode; // previous node is linking with new node
preptr = newnode; // previous node is advanced
}
preptr->nextptr = stnode; //last node is linking with first node
}
}
void displayClList()
{
struct node *tmp;
int n = 1;
if(stnode == NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp = stnode;
printf("\n\n Data entered in the list are :\n");
do {
printf(" Data %d = %d\n", n, tmp->num);
tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}
Output: Circular Linked List : Create and display a circular linked list :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8
Data entered in the list are :
Data 2 = 5