Notes
Notes - notes.io |
insertion
#include<stdio.h>
int main()
{
int n,i,k,temp,j;
printf("Enter the N:");
scanf("%d",&n);
int a[n];
printf("Array:");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int k=1;k<n;k++)
{
temp=a[k];
j=k-1;
while(j>=0 && a[j]>temp)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
printf("sorted array is: ");
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("n");
int l,r,target;
l=0,r=n-1;
printf("Enter the target element to search: ");
scanf("%d", &target);
int found=0;
while(l<r)
{
int mid_idx=(l+r)/2;
if(a[mid_idx]==target)
{
found=1;
break;
}
else if(target>a[mid_idx])
{
l=mid_idx+1;
}
else{
r=mid_idx-1;
}
}
if(found)
{
printf("Found element %d at index %dn", target, (l+r)/2);
}
else
{
printf("Not found %dn", target);
}
return 0;
}
*/
/*
selection
#include<stdio.h>
int main()
{
//insertion sort
int n, i, k, temp, j;
printf("Enter the N: ");
scanf("%d",&n);
int ar[n];
printf("Array: ");
for(int i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
for(int i=0;i<n-1;i++)
{
int min=i;
for(int j=i+1;j<n;j++)
{
if(ar[j]<ar[min])
{
min=j;
}
if(min!=i)
{
temp=ar[i];
ar[i]=ar[min];
ar[min]=temp;
}
}
}
printf("Sorted array is: ");
for(int i=0;i<n;i++)
{
printf("%d ",ar[i]);
}
return 0;
}
*/
/*
binary
#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
int ar[n];
for(int i=0;i<n;i++)
{
scanf("%d", &ar[i]);
}
int target;
scanf("%d", &target);
int l=0;
int r=n-1;
int found=0;
while(l<r)
{
int mid_idx=(l+r)/2;
if(ar[mid_idx]==target)
{
found=1;
break;
}
else if(target>ar[mid_idx])
{
//dan
l=mid_idx+1;
}
else{
//bam
r=mid_idx-1;
}
}
if(found)
{
printf("Found element %d at index %dn",target, (l+r)/2);
}
else
{
printf("Not found %dn",target);
}
return 0;
}
*/
/*
merging
#include <stdio.h>
#include <stdlib.h>
void MERGING(int A[], int R, int B[], int S, int C[]) {
int NA = 0, NB = 0, PTR = 0;
while (NA < R && NB < S) {
if (A[NA] < B[NB]) {
C[PTR] = A[NA];
NA++;
} else {
C[PTR] = B[NB];
NB++;
}
PTR++;
}
if (NA >= R) {
for (int K = 0; K < S - NB; K++) {
C[PTR + K] = B[NB + K];
}
} else {
for (int K = 0; K < R - NA; K++) {
C[PTR + K] = A[NA + K];
}
}
}
int main() {
int A[] = {1, 3, 5, 7};
int B[] = {2, 4, 6, 8};
int R = sizeof(A) / sizeof(A[0]);
int S = sizeof(B) / sizeof(B[0]);
int N = R + S;
int C[N];
MERGING(A, R, B, S, C);
printf("Merged List: ");
for (int i = 0; i < N; i++) {
printf("%d ", C[i]);
}
printf("n");
return 0;
}
*/
......Linked List......
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main()
{
struct Node* head = NULL,*newnode=NULL,*temp=NULL;
int n,value,i=0;
printf("Enter the number of nodes: ");
scanf("%d", &n);
while (i<n){
newnode = (struct Node*)malloc(sizeof(struct Node));
if (newnode == NULL) {
printf("Memory allocation failedn");
return 1;
}
printf("Enter value for node %d: ", i + 1);
scanf("%d", &value);
newnode->data = value;
newnode->next = NULL;
if (head == NULL) {
head = newnode; // First node
} else {
temp->next = newnode; // Append to the end
// Move temp to the new node
}
temp = newnode;
i++;
}
// Print the linked list
printf("Linked List: ");
temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULLn");
return 0;
}
.....Insert linked list...
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
// Create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Insert at beginning
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}
// Insert at end
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
// Insert at any position (0-based index)
void insertAtPosition(struct Node** head, int data, int position) {
if (position == 0) {
insertAtBeginning(head, data);
return;
}
struct Node* newNode = createNode(data);
struct Node* temp = *head;
for (int i = 0; i < position - 1; i++) {
if (temp == NULL) {
printf("Position out of bounds.n");
return;
}
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of bounds.n");
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
// Print the list
void printList(struct Node* head) {
printf("Linked List: ");
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULLn");
}
// Main menu-driven function
int main() {
struct Node* head = NULL;
int choice, data, position;
while (1) {
printf("n--- Linked List Menu ---n");
printf("1. Insert at beginningn");
printf("2. Insert at endn");
printf("3. Insert at positionn");
printf("4. Print listn");
printf("5. Exitn");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
printf("Enter data: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 3:
printf("Enter data: ");
scanf("%d", &data);
printf("Enter position (0-based index): ");
scanf("%d", &position);
insertAtPosition(&head, data, position);
break;
case 4:
printList(head);
break;
case 5:
printf("Exiting...n");
exit(0);
default:
printf("Invalid choice. Try again.n");
}
}
return 0;
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
