0% found this document useful (0 votes)
20 views

C

The document provides code examples for various operations on arrays and linked lists in C programming language. Specifically, it includes code to: 1) Insert and delete elements from an array, and search for an element in an array. 2) Sort elements of an array using insertion sort, quicksort, and bubble sort. 3) Traverse the nodes of a linked list. 4) Analyze the time complexity of adding elements to an array. The code examples demonstrate how to perform common data structures operations like insertion, deletion, searching and sorting on arrays and traversal on linked lists.

Uploaded by

Sahil Soni
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)
20 views

C

The document provides code examples for various operations on arrays and linked lists in C programming language. Specifically, it includes code to: 1) Insert and delete elements from an array, and search for an element in an array. 2) Sort elements of an array using insertion sort, quicksort, and bubble sort. 3) Traverse the nodes of a linked list. 4) Analyze the time complexity of adding elements to an array. The code examples demonstrate how to perform common data structures operations like insertion, deletion, searching and sorting on arrays and traversal on linked lists.

Uploaded by

Sahil Soni
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/ 41

1.

To insert elements in an array

Code

#include<stdio.h>
#include<conio.h>
void main()
{
int m[10],ins,i,n,pos;
clrscr();
printf("How many elements ?");
scanf("%d",&n);
printf("\nEnter %d elements: \n",n);
for(i=0;i<n;i++)
{
printf("\n");
scanf("%d",&m[i]);
}
printf("\nEnter number to be inserted:");
scanf("\n%d",&ins);
printf("\nEnter position:");
scanf("\n%d",&pos);
for(i=n;i>pos;i--)
{
m[i]=m[i-1];
}
m[pos]=ins;
n++;
printf("\nArray after insertion\n");
for(i=0;i<n;i++)
{
printf("\n%d\n",m[i]);
}
getch();
}

Output:
How many elements:5
Enter 5 elementes:
1
2
5
6
4
Enter number to be inserted:3
Enter position:2
Array after insertion
1
2
3
5
6
4
2. To delete Element of array

Code:

#include<stdio.h>
#include<conio.h>
void main()
{
int m[10],del,i,j,n;
clrscr();
printf("How many elements ?");
scanf("%d",&n);
printf("\nEnter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&m[i]);
}
printf("\nEnter number for delete:");
scanf("%d",&del);
for(i=0;i<n;i++)
{
if(m[i]==del)break;
}
for(j=i;j<n;j++)
{
m[j]=m[j+1];
}
n--;
printf("\nAfter deletion\n");
for(i=0;i<n;i++)
{
printf("%d\n",m[i]);
}
getch();
}

Output:
How many elements?5
Enter 5 elements
1
20
10
30
60
Enter element for delete
20
After deletion
1
10
30
60
3. To search an Element in array

Code:

#include <stdio.h>
#include <conio.h>

int main()
{
int num[10], i, beg, end, mid, pos = -1, value;
clrscr();

printf( " Enter Ten Numbers in Ascending Order::: \n" );


for( i=0 ; i<10 ; i++ )
{
scanf( "%d", &num[i] );
}

printf( " Enter the number to be searched ::: " );


scanf( "%d", &value );

beg = 0;
end = 10 - 1;
while(beg <= end)
{
mid = (beg + end) / 2;
if( value == num[mid] )
{
pos = mid + 1;
break;
}
else if ( value >= num[mid] )
beg = mid + 1;
else
end = mid - 1;
}

if( pos == -1 )
{
printf( "\n The element %d not found.", value );
}
else
{
printf( "\n The position of %d is ::: %d" , value, pos );
}

getch();
return 0;
}

Output:

Enter Ten Numbers in Ascending Order:::


1 5 13 24 25 36 47 58 69 100
Enter the number to be searched ::: 36

The position of 36 is ::: 6


4. Write a program to find time complexity of an algorithm for addition of elements in array

Code:

#include <stdio.h>

int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10;
for (i = 0; i < 10; i++)
arr[i] = i + 1;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");

x = 50;
pos = 5;

n++;

for (i = n - 1; i >= pos; i--)


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

arr[pos - 1] = x;

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


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

return 0;
}

Output
1 2 3 4 5 6 7 8 9 10
1 2 3 4 50 5 6 7 8 9 10
Time Complexity: O(N), Where N is the number of elements in the array
Auxiliary Space: O(1)
5. Write a program to search an Element in array using linear search

Code:

#include <stdio.h>

int main()
{
int array[50], search, c, n;

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


scanf("%d",&n);

printf("Enter %d integer(s)\n", n);

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


scanf("%d", &array[c]);

printf("Enter the number to search\n");


scanf("%d", &search);

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


{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);

return 0;
}

Output:

Enter the number of elements in array 5


Enter 5 integer(s)
10 23 78 34 21

Enter the number to search 78


78 is present at location 3.
6. Write a program to search an Element in array using binary search

Code;

#include <stdio.h>
#include <conio.h>

int main()
{
int num[10], i, beg, end, mid, pos = -1, value;
clrscr();

printf( " Enter Ten Numbers in Ascending Order::: \n" );


for( i=0 ; i<10 ; i++ )
{
scanf( "%d", &num[i] );
}

printf( " Enter the number to be searched ::: " );


scanf( "%d", &value );

beg = 0;
end = 10 - 1;
while(beg <= end)
{
mid = (beg + end) / 2;
if( value == num[mid] )
{
pos = mid + 1;
break;
}
else if ( value >= num[mid] )
beg = mid + 1;
else
end = mid - 1;
}

if( pos == -1 )
{
printf( "\n The element %d not found.", value );
}
else
{
printf( "\n The position of %d is ::: %d" , value, pos );
}

getch();
return 0;
}

Output:

Enter Ten Numbers in Ascending Order:::


1 5 13 24 25 36 47 58 69 100
Enter the number to be searched ::: 36

The position of 36 is ::: 6


7. Write a program to sort an Element using insert sorting

Code:

#include <stdio.h>

int main() {

int n, array[50], c, d, t;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++) {

scanf("%d", &array[c]);

for (c = 1; c <= n - 1; c++) {


d = c;

while (d > 0 && array[d] < array[d - 1]) {

t = array[d];
array[d] = array[d - 1];
array[d - 1] = t;

d--;

}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++) {

printf("%d\n", array[c]);

return 0;
}

Output:

Enter number of elements 5


Enter 5 integers
20
90
0
-23
78
Sorted list in ascending order:
-23
0
20
78
90
8. Write a program to sort the element using quicksort

Code:

#include <stdio.h>

int main() {

int n, array[50], c, d, t;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++) {

scanf("%d", &array[c]);

for (c = 1; c <= n - 1; c++) {


d = c;

while (d > 0 && array[d] < array[d - 1]) {

t = array[d];
array[d] = array[d - 1];
array[d - 1] = t;

d--;

}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++) {

printf("%d\n", array[c]);

return 0;
}

Output:

Enter number of elements 5


Enter 5 integers
20
90
0
-23
78
Sorted list in ascending order:
-23
0
20
78
90
9. Write a program to sort an Element using bubble sort

Code:

#include <stdio.h>

int main() {

int array[50], n, i, j, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

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


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

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

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

if (array[j] > array[j + 1]) /* For decreasing order use < */


{
swap = array[j];
array[j] = array[j + 1];
array[j + 1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

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


{
printf("%d\n", array[i]);
}

return 0;

Output:
Enter number of elements 5
Enter 5 integers
20
90
0
-23
78
Sorted list in ascending order:
-23
0
20
78
90
10. Write a program to traverse a linked list

Code:

#include<stdio.h>
#include<stdlib.h>
void create(int);
void traverse();
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\n1.Append List\n2.Traverse\n3.Exit\n4.Enter your choice?");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item\n");
scanf("%d",&item);
create(item);
break;
case 2:
traverse();
break;
case 3:
exit(0);
break;
default:
printf("\nPlease enter valid choice\n");
}

}while(choice != 3);
}
void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}

}
void traverse()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Empty list..");
}
else
{
printf("printing values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}

Output :
1.Append List
2.Traverse
3.Exit
4.Enter your choice?1

Enter the item


23

Node inserted

1.Append List
2.Traverse
3.Exit
4.Enter your choice?1

Enter the item


233

Node inserted

1.Append List
2.Traverse
3.Exit
4.Enter your choice?2
printing values . . . . .

233
23
11. Write a program to insert an Element in a linked list at beginning

Code:

#include <stdio.h>
#include <stdlib.h>

struct node
{
struct node *prev;
int n;
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;

void insert1();
void insert2();
void insert3();
void traversebeg();
void traverseend(int);
void sort();
void search();
void update();
void delete();

int count = 0;

void main()
{
int ch;

h = NULL;
temp = temp1 = NULL;

printf(" 1 - Insert at beginning");


printf(" 2 - Insert at end");
printf(" 3 - Insert at position i");
printf(" 4 - Delete at i");
printf(" 5 - Display from beginning");
printf(" 6 - Display from end");
printf(" 7 - Search for element");
printf(" 8 - Sort the list");
printf(" 9 - Update an element");
printf(" 10 - Exit");
while (1)
{
printf(" Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert1();
break;
case 2:
insert2();
break;
case 3:
insert3();
break;
case 4:
delete();

break;
case 5:
traversebeg(); break;
case 6:

temp2 = h;

if (temp2 == NULL)
printf(" Error : List empty to display ");
else
{
printf(" Reverse order of linked list is : ");
traverseend(temp2->n);
}
break;
case 7:
search();
break;
case 8:
sort();
break;
case 9:
update();
break;
case 10:
exit(0);
default:
printf(" Wrong choice menu");
}
}
}

void create()
{
int data;

temp =(struct node *)malloc(1*sizeof(struct node));


temp->prev = NULL;
temp->next = NULL;
printf(" Enter value to node : ");
scanf("%d", &data);
temp->n = data;
count++;
}

void insert1()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}

void insert2()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}

void insert3()
{
int pos, i = 2;

printf(" Enter position to be inserted : ");


scanf("%d", &pos);
temp2 = h;

if ((pos < 1) || (pos >= count + 1))


{
printf(" Position out of range to insert");
return;
}
if ((h == NULL) && (pos != 1))
{
printf(" Empty list cannot insert other than 1st position");
return;
}
if ((h == NULL) && (pos == 1))
{
create();
h = temp;
temp1 = h;
return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
create();
temp->prev = temp2;
temp->next = temp2->next;
temp2->next->prev = temp;
temp2->next = temp;
}
}

void delete()
{
int i = 1, pos;

printf(" Enter position to be deleted : ");


scanf("%d", &pos);
temp2 = h;

if ((pos < 1) || (pos >= count + 1))


{
printf(" Error : Position out of range to delete");
return;
}
if (h == NULL)
{
printf(" Error : Empty list no elements to delete");
return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
if (i == 1)
{
if (temp2->next == NULL)
{
printf("Node deleted from list");
free(temp2);
temp2 = h = NULL;
return;
}
}
if (temp2->next == NULL)
{
temp2->prev->next = NULL;
free(temp2);
printf("Node deleted from list");
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next;
if (i == 1)
h = temp2->next;
printf(" Node deleted");
free(temp2);
}
count--;
}

void traversebeg()
{
temp2 = h;

if (temp2 == NULL)
{
printf("List empty to display ");
return;
}
printf(" Linked list elements from begining : ");

while (temp2->next != NULL)


{
printf(" %d ", temp2->n);
temp2 = temp2->next;
}
printf(" %d ", temp2->n);
}

void traverseend(int i)
{
if (temp2 != NULL)
{
i = temp2->n;

temp2 = temp2->next;
traverseend(i);
printf(" %d ", i);
}
}

void search()
{
int data, count = 0;
temp2 = h;

if (temp2 == NULL)
{
printf(" Error : List empty to search for data");
return;
}
printf(" Enter value to search : ");
scanf("%d", &data);
while (temp2 != NULL)
{
if (temp2->n == data)
{
printf(" Data found in %d position",count + 1);
return;
}
else
temp2 = temp2->next;
count++;
}
printf(" Error : %d not found in list", data);
}

void update()
{
int data, data1;

printf(" Enter node data to be updated : ");


scanf("%d", &data);
printf(" Enter new data : ");
scanf("%d", &data1);
temp2 = h;
if (temp2 == NULL)
{
printf(" Error : List empty no node to update");
return;
}
while (temp2 != NULL)
{
if (temp2->n == data)
{

temp2->n = data1;
traversebeg();
return;
}
else
temp2 = temp2->next;

printf(" Error : %d not found in list to update", data);


}

void sort()
{
int i, j, x;

temp2 = h;
temp4 = h;

if (temp2 == NULL)
{
printf(" List empty to sort");
return;
}

for (temp2 = h; temp2 != NULL; temp2 = temp2->next)


{
for (temp4 = temp2->next; temp4 != NULL; temp4 = temp4->next)
{
if (temp2->n > temp4->n)
{
x = temp2->n;
temp2->n = temp4->n;

temp4->n = x;
}
}
}
traversebeg();
}

Output:

1 - Insert at beginning
2 - Insert at end
3 - Insert at position i
4 - Delete at i
5 - Display from beginning
6 - Display from end
7 - Search for element
8 - Sort the list
9 - Update an element
10 - Exit
Enter choice : 1

Enter value to node : 10

Enter choice : 2

Enter value to node : 50

Enter choice : 4

Enter position to be deleted : 1

Node deleted
Enter choice : 1

Enter value to node : 34

Enter choice : 3

Enter position to be inserted : 2


Enter value to node : 13

Enter choice : 4

Enter position to be deleted : 4

Error : Position out of range to delete


Enter choice : 1

Enter value to node : 15

Enter choice : 1

Enter value to node : 67

Enter choice : 3

Enter position to be inserted : 2

Enter value to node : 34

Enter choice : 4

Enter position to be deleted : 3

Node deleted
Enter choice : 7

Enter value to search : 15

Error : 15 not found in list


Enter choice : 8

Linked list elements from begining : 13 34 34 50 67


Enter choice : 9

Enter node data to be updated : 45

Enter new data : 89

Error : 45 not found in list to update


Enter choice : 9

Enter node data to be updated : 50


Enter new data : 90
Enter choice : 5

Linked list elements from begining : 13 34 34 90 67


Enter choice : 6

Reverse order of linked list is : 67 90 34 34 13


Enter choice : 7

Enter value to search : 90

Data found in 4 position


Enter choice : 8

Linked list elements from begining : 13 34 34 67 90


Enter choice : 7

Enter value to search : 90

Data found in 5 position


Enter choice : 9

Enter node data to be updated : 34

Enter new data : 56

Linked list elements from begining : 13 56 34 67 90


Enter choice : 10
12. Write a program to insert an Element in a linked list at the end

Code:

#include<stdio.h>
#include<stdlib.h>
void lastinsert(int);
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
lastinsert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void lastinsert(int item)
{
struct node *ptr = (struct node*)malloc(sizeof(struct node));
struct node *temp;
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");

}
}
}

Output

Enter the item which you want to insert?


12

Node inserted
Press 0 to insert more ?
0

Enter the item which you want to insert?


23

Node inserted
Press 0 to insert more ?
2
13. Write a program of stack implementation using array

Code:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 50

void push(int i);


int pop(void);

int *tos, *p1, stack[SIZE];

int main(void)
{
int value;

tos = stack; /* tos points to the top of stack */


p1 = stack; /* initialize p1 */

do {
printf("Enter value: ");
scanf("%d", &value);

if(value != 0) push(value);
else printf("value on top is %d\n", pop());

} while(value != -1);

return 0;
}

void push(int i)
{
p1++;
if(p1 == (tos+SIZE)) {
printf("Stack Overflow.\n");
exit(1);
}
*p1 = i;
}

int pop(void)
{
if(p1 == tos) {
printf("Stack Underflow.\n");
exit(1);
}
p1--;
return *(p1+1);
}
14. Write a program stack implementation of using linked list

Code:

#include <stdio.h>
#include <stdlib.h>

struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;

int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();

int count = 0;

void main()
{
int no, ch, e;

printf(" 1 - Push");
printf(" 2 - Pop");
printf(" 3 - Top");
printf(" 4 - Empty");
printf(" 5 - Exit");
printf(" 6 - Dipslay");
printf(" 7 - Stack Count");
printf(" 8 - Destroy stack");

create();

while (1)
{
printf(" Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;

case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf(" Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}

void create()
{
top = NULL;
}

void stack_count()
{
printf(" No. of elements in stack : %d", count);
}

void push(int data)


{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}

void display()
{
top1 = top;

if (top1 == NULL)
{
printf("Stack is empty");
return;
}

while (top1 != NULL)


{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
void pop()
{
top1 = top;
if (top1 == NULL)
{
printf(" Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf(" Popped value : %d", top->info);
free(top);
top = top1;
count--;
}

int topelement()
{
return(top->info);
}

void empty()
{
if (top == NULL)
printf(" Stack is empty");
else
printf(" Stack is not empty with %d elements", count);
}

void destroy()
{
top1 = top;

while (top1 != NULL)


{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;
printf(" All stack elements destroyed");
count = 0;
}

Output:

1 - Push
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Dipslay
7 - Stack Count
8 - Destroy stack
Enter choice : 1
Enter data : 56

Enter choice : 1
Enter data : 80

Enter choice : 2

Popped value : 80
Enter choice : 3

Top element : 56

Enter choice : 1
Enter data : 78

Enter choice : 1
Enter data : 90

Enter choice : 6
90 78 56
Enter choice : 7

No. of elements in stack : 3


Enter choice : 8

All stack elements destroyed


Enter choice : 4
Stack is empty
Enter choice : 5

You might also like