Programming Practice in C
Recursion: (2: 2:2025)
1)Decimal to Binary convertion
#include <stdio.h>
int convertor(int n){
if(n==0) return 0;
convertor(n/2);
printf("%d",n%2);
}
int main() {
int n;
printf("Enter a number ");
scanf("%d",&n);
convertor(n);
return 0;
}
Output:
Enter a number 8
1000
2)GCD of two numbers
#include <stdio.h>
int gcd(int a,int b){
if(b==0) return a;
return gcd(b, a%b);
}
int main() {
int n,m;
printf("Enter two numbers ");
scanf("%d %d",&n,&m);
printf("The GCD of a numbers %d %d is %d",n,m,gcd(n,m));
return 0;
}
Enter two numbers 6 2
The GCD of a numbers 6 2 is 2
3) Factorial of a number
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
Output:
Enter a number: 5
Factorial of 5 is 120
4)Fabinacci Series
#include <stdio.h>
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
Output:
Enter the number of terms: 5
Fibonacci Series: 0 1 1 2 3
5)Reverse of a string
#include <stdio.h>
#include <string.h>
void reverseString(char str[], int start, int end) {
if (start >= end) return;
char temp = str[start];
str[start] = str[end];
str[end] = temp;
reverseString(str, start + 1, end - 1);
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
reverseString(str, 0, strlen(str) - 1);
printf("Reversed string: %s\n", str);
return 0;
}
Output:
Enter a string: Ramalakshmi
Reversed string: imhskalamaR
6)Sum of Digits
#include <stdio.h>
int sumOfDigits(int n) {
if (n == 0) return 0;
return (n % 10) + sumOfDigits(n / 10);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Sum of digits: %d\n", sumOfDigits(num));
return 0;
}
Output:
Enter a number: 12345
Sum of digits: 15
7)Power of a number
#include <stdio.h>
int power(int base, int exp) {
if (exp == 0) return 1;
return base * power(base, exp - 1);
}
int main() {
int base, exp;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exp);
printf("%d^%d = %d\n", base, exp, power(base, exp));
return 0;
}
Output:
Enter base and exponent: 2 4
2^4 = 16
Array: (5th feb)
1)Max element in the array
#include <stdio.h>
int main() {
int arr[5],i;
printf("Enter 5 elements ");
for(i=0;i<5;i++){
scanf("%d",&arr[i]);
}
int max=arr[0];
for(i=0;i<5;i++){
if(arr[i]>max){
max=arr[i];
}
}
printf("The largest element in the array is %d",max);
return 0;
}
Output:
Enter 5 elements 5 1 26 45 2
The largest element in the array is 45
2)Smallest element in the array
#include <stdio.h>
int main() {
int arr[5],i;
printf("Enter 5 elements ");
for(i=0;i<5;i++){
scanf("%d",&arr[i]);
}
int min=arr[0];
for(i=0;i<5;i++){
if(arr[i]<min){
min=arr[i];
}
}
printf("The smallest element in the array is %d",min);
return 0;
}
Output:
Enter 5 elements 55 78 3 5 0
The smallest element in the array is 0
3)Sum of the elements in the array
#include <stdio.h>
int main() {
int arr[5],i;
printf("Enter 5 elements ");
for(i=0;i<5;i++){
scanf("%d",&arr[i]);
}
int sum=0;
for(i=0;i<5;i++){
sum+=arr[i];
}
printf("The sum of the element in the array is %d",sum);
return 0;
}
Output:
Enter 5 elements 5 8 3 2 9
The sum of the element in the array is 27
4)Reverse the array
#include <stdio.h>
int main() {
int arr[5],i,a[5];
printf("Enter 5 elements ");
for(i=0;i<5;i++){
scanf("%d",&arr[i]);
}
printf("Reversed array");
for(i=4;i>=0;i--){
printf("%d ",arr[i]);
}
}
Output:
Enter 5 elements 1 3 5 7 9
Reversed array 9 7 5 3 1
5)Search for an element
#include <stdio.h>
int main() {
int arr[5],i,search,found=0;
printf("Enter 5 elements ");
for(i=0;i<5;i++){
scanf("%d",&arr[i]);
}
printf("Enter the element you want to search ");
scanf("%d",&search);
for(i=0;i<5;i++){
if(arr[i]==search){
printf("The element %d is precent %d in the location",search,i+1);
found=1;
}
}
if(!found){
printf("Element not present");
}
return 0;
}
Output:
Enter 5 elements 4 9 8 7 6
Enter the element you want to search 7
The element 7 is precent 4 in the location
6)Array sorting
#include <stdio.h>
int main() {
int arr[5],i,search,found=0,j;
printf("Enter 5 elements ");
for(i=0;i<5;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<5-1;i++){
for(j=0;j<5-i-1;j++){
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("Sorted array ");
for(i=0;i<5;i++){
printf("%d ",arr[i]);
}
return 0;
}
Output:
Enter 5 elements 9 7 3 5 6
Sorted array 3 5 6 7 9
7)Merge array
#include <stdio.h>
int main() {
int arr1[5]={3,6,1,2,7},arr2[5]={15,8,16,9,10},merge[10],i,j;
for(i=0;i<5;i++){
merge[i]=arr1[i];
}
for(j=0;j<5;j++){
merge[i+j]=arr2[j];
}
printf("Merged array ");
for(i=0;i<10;i++){
printf("%d ",merge[i]);
}
return 0;
}
Output:
Merged array 3 6 1 2 7 15 8 16 9 10
8)Second largest element in the array
#include <stdio.h>
int main() {
int arr[7],i,j;
printf("Enter the 7 elements of a array ");
for(i=0;i<7;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<7-1;i++){
for(j=0;j<7-i-1;j++){
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("The second largest element in the array is %d",arr[5]);
return 0;
}
Output:
Enter the 7 elements of a array 55 6 5 50 9 4 6
The second largest element in the array is 50
9)Most Frequent element in the array
#include <stdio.h>
int main() {
int arr[7],i,j,maxcount=0,frequent,count;
printf("Enter the 7 elements of a array ");
for(i=0;i<7;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<7;i++){
count=0;
for(j=0;j<7;j++){
if(arr[i]==arr[j]){
count++;
}
}
if(count>maxcount){
maxcount=count;
frequent=arr[i];
}
}
printf("The most frequent element is %d and it occured %d times in the
array",frequent,maxcount);
return 0;
}
Output:
Enter the 7 elements of a array 2 1 2 1 2 1 2
The most frequent element is 2 and it occured 4 times in the array
10)Remove the duplicates from an array
#include <stdio.h>
int main() {
int arr[10]={1,2,1,2,1,2,1,2,1,2},out,in,count;
printf("After removing the duplicates ");
for(out=0;out<10;out++){
count=0;
for(in=out+1;in<10;in++){
if(arr[out]==arr[in]){
count++;
}
}
if(count==0){
printf("%d ",arr[out]);
}
}
return 0;
}
Output:
After removing the duplicates 1 2
11)Rotate an array to left by K position
#include <stdio.h>
int rotateArray(int arr[],int n, int pos){
int i,temp[n];
for(i=0;i<pos;i++){
temp[i]=arr[i];
}
for(i=0;i<n-pos;i++){
arr[i]=arr[pos+i];
}
for(i=0;i<pos;i++){
arr[n-pos-i]=temp[i];
}
}
int main() {
int arr[20],n,pos=3,i;
printf("Enter the number of elements in the array ");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
rotateArray(arr,n,pos);
for(i=0;i<n;i++){
printf("%d ",arr[i]);
}
return 0;
}
Output:
Enter the number of elements in the array 8
12345678
45632178
12)Intersection of two array
#include <stdio.h>
int main() {
int arr1[20],arr2[20],i,j,n;
printf("Enter the number of elements in the array ");
scanf("%d",&n);
printf("Enter array 1 elements ");
for(i=0;i<n;i++){
scanf("%d",&arr1[i]);
}
printf("Enter array 2 elements ");
for(i=0;i<n;i++){
scanf("%d",&arr2[i]);
}
printf("Intersection of the array is ");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(arr1[i]==arr2[j]){
printf("%d ",arr1[i]);
break;
}
}
}
return 0;
}
Output:
Enter the number of elements in the array 7
Enter array 1 elements 1 3 5 7 9 11 13
Enter array 2 elements 1 2 3 4 5 6 7
Intersection of the array is 1 3 5 7
13)Missing elements in the array
#include <stdio.h>
int missingElement( int arr[],int n){
int sum=0;
int total=(n*(n+1))/2;
for(int i=0;i<n;i++){
sum+=arr[i];
}
return total-sum;
}
int main() {
int arr[5]={1,2,3,5},i,n=5;
printf("The missing element is %d ",missingElement(arr,n));
return 0;
}
Output:
The missing element is 4
14)Merge two sorted array
#include <stdio.h>
void mergeArray(int arr1[], int arr2[], int n1, int n2, int merged[]){
int i=0, j=0,k=0;
while(i<n1 && j<n2){
if(arr1[i]<arr2[j]){
merged[k++]=arr1[i++];
}
else{
merged[k++]=arr2[j++];
}
}
while(i<n1){
merged[k++]=arr1[i++];
}
while(j<n2){
merged[k++]=arr2[j++];
}
}
int main() {
int arr1[4]={1,3,5,7},arr2[4]={2,4,6,8};
int n1=sizeof(arr1)/sizeof(arr1[0]);
int n2=sizeof(arr2)/sizeof(arr2[0]);
int merged[n1+n2];
int m=n1+n2;
mergeArray(arr1, arr2, n1, n2, merged);
printf("Merged sorted array ");
for(int i=0;i<m;i++){
printf("%d ",merged[i]);
}
return 0;
}
Output:
Merged sorted array 1 2 3 4 5 6 7 8
18th feb
String
1)Length of the string
#include <stdio.h>
int main() {
int length=0;
char str[50];
printf("Enter the string ");
scanf("%s",&str);
while(str[length]!='\0'){
length++;
}
printf("The length of the string is %d",length);
return 0;
}
Output:
Enter the string Ramalakshmi
The length of the string is 11
2)Check the string is palindrome or not
#include <stdio.h>
#include<string.h>
int main() {
char str[30];
printf("Enter the string ");
scanf("%s",&str);
char str1[30];
strcpy(str1,str);
int len=strlen(str);
int start=0,end=len-1;
while(start < end){
char temp=str[start];
str[start]=str[end];
str[end]=temp;
start++;
end--;
}
if(strcmp(str,str1)==0){
printf("The string is palindrome");
}
else{
printf("The string is not palindrome");
}
return 0;
}
Output:
Enter the string madam
The string is palindrome
3)Count the vowels and Consonents in the string
#include <stdio.h>
#include<string.h>
int main() {
char vowels[5]={'a','e','i','o','u'};
char str[30];
int Vcount=0,Ccount=0;
printf("Enter the string ");
scanf("%s",&str);
for(int i=0;i<strlen(str);i++){
for(int j=0;j<5;j++){
if(str[i]==vowels[j]){
Vcount++;
}
}
}
int len=strlen(str);
Ccount=len-Vcount;
printf("The vowels count is %d\n",Vcount);
printf("The Consonents count is %d",Ccount);
return 0;
}
Output:
Enter the string mango
The vowels count is 2
The Consonents count is 3
4)Convert to uppercase and lowercase
#include <stdio.h>
#include<string.h>
int main() {
char str[30];
printf("Enter the string ");
scanf("%s",&str);
for(int i=0;str[i]!='\0';i++){
if(str[i]>='a' && str[i]<='z'){
str[i]=str[i]-32;
}
}
printf("Uppercase: %s\n",str);
for(int i=0;str[i]!='\0';i++){
if(str[i]>='A' && str[i]<='Z'){
str[i]=str[i]+32;
}
}
printf("Lowercase: %s\n",str);
return 0;
}
Output:
Enter the string ram
Uppercase: RAM
Lowercase: ram
5)Concatenation of two string
#include <stdio.h>
#include<string.h>
int main() {
char str[30], con[60], str1[30];
printf("Enter two string ");
scanf("%s%s",&str,&str1);
int len=strlen(str);
for(int i=0;str[i]!='\0';i++){
con[i]=str[i];
}
for(int i=0;str1[i]!='\0';i++){
con[len]=str1[i];
len++;
}
printf("Concatenation of two string is %s ",con);
return 0;
}
Output:
Enter two string rama
lakshmi
Concatenation of two string is ramalakshmi
6) Check two strings are same
#include <stdio.h>
#include<string.h>
int main() {
char str[30], str1[30];
printf("Enter two string ");
scanf("%s%s",&str,&str1);
int flag=1,i=0;
while(str[i]!='\0' && str1[i]!='\0'){
if(str[i]!=str1[i]){
flag=0;
break;
}
i++;
}
if(str[i]!='\0' || str1[i]!='\0'){
flag=0;
}
if(flag){
printf("The two strings are same");
}
else{
printf("The two strings are not same");
}
return 0;
}
Output:
Enter two string ram
ram
The two strings are same
7) Reverse the string
#include <stdio.h>
#include<string.h>
int main() {
char str[30];
printf("Enter two string ");
scanf("%s",&str);
int start=0,end=strlen(str)-1;
while(start < end){
char temp=str[start];
str[start]=str[end];
str[end]=temp;
start++;
end--;
}
printf("The reverse of the string is %s",str);
return 0;
}
Output:
Enter two string ramalakshmi
The reverse of the string is imhskalamar
8)Remove the spaces in the string
#include <stdio.h>
int main() {
char str[50],str1[50];
int j=0;
printf("enter a string: ");
scanf("%[^\n]s",&str);
for(int i=0;str[i]!='\0';i++){
if(str[i]!=' '){
str1[j]=str[i];
j++;
}
}
str1[j]='\0';
printf("String after removing the spaces %s",str1);
return 0;
}
Output:
enter a string: hi hellohi hello
String after removing the spaces hihello
9)Count number of words in the sentence
#include <stdio.h>
int main() {
char str[50];
int count=0;
printf("enter a string: ");
scanf("%[^\n]s",&str);
for(int i=0;str[i]!='\0';i++){
if(str[i]==' '){
count++;
}
}
printf("The number of words in the string is %d",count+1);
return 0;
}
Output:
enter a string: Hi hello welcome to the great karikalan magic show
The number of words in the string is 9
10)Find the first non repeating character in the string
#include <stdio.h>
int main() {
char str[50];
int count,flag=0,i,j;
printf("enter a string: ");
scanf("%[^\n]s",&str);
for(i=0;str[i]!='\0';i++){
count=0;
for(j=0;str[j]!='\0';j++){
if(str[i]==str[j]){
count++;
}
}
if(count==1){
printf("The first non repeting charector in the string is %c",str[i]);
flag=1;
break;
}
}
if(flag==0){
printf("There is no first non repeating charecter in the string");
}
return 0;
}
Output:
enter a string: ramalakshmi
The first non repeting charector in the string is r
11) Find the substring present in the string or not
#include <stdio.h>
int main() {
char str[100],sub[50];
int i,j,found=0;
printf("Enter the string and substring ");
scanf("%s %s",&str,&sub);
for(i=0;str[i]!='\0';i++){
j=0;
while(str[i+j]!='\0' && sub[j]!='\0' && str[i+j]==sub[j]){
j++;
}
if(sub[j]=='\0'){
found=1;
break;
}
}
if(found){
printf("The substring is present in the string");
}
else{
printf("The substring is not present in the string");
}
return 0;
}
Output:
Enter the string and substring ramalakshmi hmi
The substring is present in the string
12)Remove the duplicate characters from the string
#include <stdio.h>
int main() {
char str[100],sub[100];
int i,j,k=0;
printf("Enter the string ");
scanf("%s",&str);
for(i=0;str[i]!='\0';i++){
isDup=0;
for(j=0;j<k;j++){
if(str[i]==str[j]){
isDup=1;
break;
}
}
if(!isDup){
sub[k]=str[i];
k++;
}
}
sub[k]='\0';
printf("The string after removing the duplicates is %s",sub);
return 0;
}
Output:
Enter the string ramalakshmi
The string after removing the duplicates is ramlkshi
Linked List
Traverse a linked list
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
struct Node* second = (struct Node*)malloc(sizeof(struct Node));
struct Node* third = (struct Node*)malloc(sizeof(struct Node));
head->data = 10;
head->next = second;
second->data = 20;
second->next = third;
third->data = 30;
third->next = NULL;
printList(head);
free(head);
free(second);
free(third);
return 0;
}
Output:
10 -> 20 -> 30 -> NULL
Insert at the beginning in the linked list
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* insertAtBeginning(struct Node* head, int newData) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = newData;
newNode->next = head;
return newNode;
}
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
head = insertAtBeginning(head, 30);
head = insertAtBeginning(head, 20);
head = insertAtBeginning(head, 10);
printList(head);
return 0;
}
Output:
10 -> 20 -> 30 -> NULL
1. REMOVE DUPLICATES IN AN ARRAY
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cout << "enter the size of array ";
cin >> n;
vector<int> arr(n);
unordered_set<int> seen;
for(int &num:arr){
cin >> num;
}
for(int num:arr){
if(seen.insert(num).second){
cout << num << " ";
}
}
return 0;
}
2. FIND Kth LARGEST AND Kth SMALLEST ELEMENT IN ARRAY
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,k;
cout << "enter the size of array ";
cin >> n;
vector<int> arr(n);
for(int &num:arr){
cin >> num;
}
cout << "Enter the value of k ";
cin >> k;
sort(arr.begin(),arr.end());
for(int num:arr){
cout << num;
}
cout << endl;
cout << "Kth largest element is " << arr[n-k] << endl;
cout << "Kth smallest element is " << arr[k-1];
return 0;
}
3. Move all Zeroes to the End of the Array
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,pos=0;
cout << "enter the size of array ";
cin >> n;
vector<int> arr(n);
for(int &num:arr){
cin >> num;
}
for(int i=0;i<n;i++){
if(arr[i]!=0){
swap(arr[i], arr[pos]);
pos++;
}
}
for(int n2:arr){
cout << n2;
}
return 0;
}
4. Rotate Array by N Elements
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[6]={2,3,4,5,6,7};
int n=1;
int size=sizeof(arr)/sizeof(arr[0]);
rotate(arr, arr+n, arr+size);
for(int num:arr){
cout << num << " ";
}
return 0;
}
5. Sort the array in reverse order
#include <bits/stdc++.h>
using namespace std;
int main() {
vector <int> arr;
arr={2,3,4,5,6,7};
int size=arr.size();
sort(arr.begin(), arr.end());
reverse(arr.begin(), arr.end());
for(int num:arr){
cout << num <<" ";
}
}
6. Max count of zero and one
#include <bits/stdc++.h>
using namespace std;
void maxone(vector<int> & arr, int size){
int maxcount=0, currentcount=0,i;
for(i=0;i<size;i++){
if(arr[i]==1){
currentcount++;
maxcount=max(maxcount,currentcount);
}
else{
currentcount=0;
}
}
cout << "max one " << maxcount;
}
void maxzero(vector<int> & arr, int size){
int maxcount=0, currentcount=0,i;
for(i=0;i<size;i++){
if(arr[i]==0){
currentcount++;
maxcount=max(maxcount,currentcount);
}
else{
currentcount=0;
}
}
cout << "max zero " << maxcount;
}
int main() {
vector <int> arr;
arr={1,1,1,1,0,0,1,0,0,0};
int size=arr.size();
maxzero(arr, size);
maxone(arr, size);
}
7. Sort array in wave form
#include <bits/stdc++.h>
using namespace std;
int main() {
vector <int> arr;
arr={3,5,2,6,1,7,8,9};
int size=arr.size();
sort(arr.begin(), arr.end());
for(int num:arr){
cout << num <<" ";
}
cout << endl;
for(int i=0;i<size;i+=2){
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
for(int num:arr){
cout << num <<" ";
}
return 0;
}
8. Pattern prints
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,i,j;
cin >> n ;
char ch='A';
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
cout << ch <<" ";
}
ch++;
cout << endl;
}
return 0;
}
2
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,i,j,num=1;
cin >> n ;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
cout << num <<" ";
num++;
}
cout << endl;
}
return 0;
}
3. #include <bits/stdc++.h>
using namespace std;
int main() {
int n,i,j,num=1;
cin >> n ;
for(i=1;i<=n;i++){
for(j=1;j<=n-i;j++){
cout <<" ";
}
int num=i;
for(j=1;j<=i;j++){
cout << num<<" ";
num++;
}
num-=2;
for(j=1;j<i;j++){
cout << num << " ";
num--;
}
cout << endl;
}
return 0;
}
4) #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
cout << " ";
}
for (int j = 0; j < (2 * (n - i) - 1); j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
5 #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
cout << " ";
}
for (int j = 0; j < n; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
6 #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2 * i; j++) {
cout << " ";
}
for (int j = 0; j < n - i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
7 #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == n - 1)
cout << "* ";
else
cout << " ";
}
cout << endl;
}
return 0;
}
Linked list
1. Insert and display1
#include <bits/stdc++.h>
using namespace std;
struct node{
int data;
node* next;
};
void insert(node*& head, int data){
node* newnode=new node();
newnode->data=data;
newnode->next=NULL;
if(head==NULL){
head=newnode;
return;
}
node *temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newnode;
}
void display(node * head){
node * temp=head;
while(temp!=NULL){
cout << temp->data << "->";
temp=temp->next;
}
}
int main() {
node * head=NULL;
insert(head,10);
insert(head,20);
insert(head,30);
insert(head,40);
cout << "The linked list elements are ";
display(head);
return 0;
}
Output:
The linked list elements are 10->20->30->40->
2. Insert at beginning
#include <bits/stdc++.h>
using namespace std;
struct node{
int data;
node* next;
};
void insertBegin(node*& head, int data){
node* newnode=new node();
newnode->data=data;
newnode->next=head;
head=newnode;
}
void display(node * head){
node * temp=head;
while(temp!=NULL){
cout << temp->data << "->";
temp=temp->next;
}
}
int main() {
node * head=NULL;
insertBegin(head,10);
insertBegin(head,20);
insertBegin(head,30);
insertBegin(head,40);
cout << "The linked list elements are ";
display(head);
return 0;
}
Output:
The linked list elements are 40->30->20->10->