0% found this document useful (0 votes)
19 views17 pages

Data Structures

The document contains multiple C programs demonstrating array manipulation, including calculating the size and traversing an array, inserting elements into an array, and creating a singly linked list with various operations such as insertion, deletion, and traversal. It provides detailed code examples for each functionality, including user input handling and memory management. Additionally, it includes a menu-driven program for linked list operations, allowing users to perform various actions like finding the maximum, calculating the mean, sorting, and reversing the list.

Uploaded by

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

Data Structures

The document contains multiple C programs demonstrating array manipulation, including calculating the size and traversing an array, inserting elements into an array, and creating a singly linked list with various operations such as insertion, deletion, and traversal. It provides detailed code examples for each functionality, including user input handling and memory management. Additionally, it includes a menu-driven program for linked list operations, allowing users to perform various actions like finding the maximum, calculating the mean, sorting, and reversing the list.

Uploaded by

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

Program to calculate size of array and traversal

#include<stdio.h>
int main(){

int L=0;
int i;

int arr[] = {2,4,6,8,9,4,4,5,6,6,3,4,6,8};

printf("Size of int array:%d \n",sizeof(arr));


printf("Size of 1 int value :%d \n",sizeof(int));

//Calculate length of the array ( Number of elements)

L = sizeof(arr)/sizeof(int);

//display array length

printf("So, array length is:%d",L );

printf("\nElements of the array are:\n");


for(i = 0; i<L; i++) {
printf("\narr[%d] = %d", i, arr[i]);
}

return 0;
}

Program to insert element in array


https://www.w3schools.in/c-programming/examples/insert-element-in-array

#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] = array[c-1];

array[position-1] = value;

printf("Resultant array is\n");

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


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

return 0;
}

Program to create Singly Linked Lists with N nodes


https://www.w3schools.in/data-structures/linked-list

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

struct node
{
int num; //Data of the node
struct node *nextptr; //Address of the next node
}*stnode;

void createNodeList(int n); // function to create the list


void displayList(); // function to display the list

int main()
{
int n;
printf("\n\n Linked List : To create and display Singly Linked List :\n");
printf("-------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct node));

if(stnode == NULL) //check whether the fnnode is NULL and if so no memory


allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard

printf(" Input data for node 1 : ");


scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL; // links the address field to NULL
tmp = stnode;
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);

fnNode->num = num; // links the num field of fnNode with num


fnNode->nextptr = NULL; // links the address field of fnNode with NULL
tmp->nextptr = fnNode; // links previous node i.e. tmp to the fnNode
tmp = tmp->nextptr;
}
}
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" List is empty.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num); // prints the data of current node
tmp = tmp->nextptr; // advances the position of current node
}
}
}
createNodeList()
displayList();
Linked List Menu Driven Program

// C program for the all operations in


// the Singly Linked List
#include <stdio.h>
#include <stdlib.h>
// Linked List Node
struct node {
int info;
struct node* link;
};
struct node* start = NULL;

// Function to create list with n nodes initially


void createList()
{
if (start == NULL) {
int n;
printf("\nEnter the number of nodes: ");
scanf("%d", &n);
if (n != 0) {
int data;
struct node* newnode;
struct node* temp;
newnode = malloc(sizeof(struct node));
start = newnode;
temp = start;
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
start->info = data;

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


newnode = malloc(sizeof(struct node));
temp->link = newnode;
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
newnode->info = data;
temp = temp->link;
}
}
printf("\nThe list is created\n");
}
else
printf("\nThe list is already created\n");
}

// Function to traverse the linked list


void traverse()
{
struct node* temp;

// List is empty
if (start == NULL)
printf("\nList is empty\n");

// Else print the LL


else {
temp = start;
while (temp != NULL) {
printf("Data = %d\n", temp->info);
temp = temp->link;
}
}
}

// Function to insert at the front


// of the linked list
void insertAtFront()
{
int data;
struct node* temp;
temp = malloc(sizeof(struct node));
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
temp->info = data;

// Pointer of temp will be


// assigned to start
temp->link = start;
start = temp;
}

// Function to insert at the end of


// the linked list
void insertAtEnd()
{
int data;
struct node *temp, *head;
temp = malloc(sizeof(struct node));

// Enter the number


printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);

// Changes links
temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL) {
head = head->link;
}
head->link = temp;
}
// Function to insert at any specified
// position in the linked list
void insertAtPosition()
{
struct node *temp, *newnode;
int pos, data, i = 1;
newnode = malloc(sizeof(struct node));

// Enter the position and data


printf("\nEnter position and data :");
scanf("%d %d", &pos, &data);

// Change Links
temp = start;
newnode->info = data;
newnode->link = 0;
while (i < pos - 1) {
temp = temp->link;
i++;
}
newnode->link = temp->link;
temp->link = newnode;
}

// Function to delete from the front


// of the linked list
void deleteFirst()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
start = start->link;
free(temp);
}
}

// Function to delete from the end


// of the linked list
void deleteEnd()
{
struct node *temp, *prevnode;
if (start == NULL)
printf("\nList is Empty\n");
else {
temp = start;
while (temp->link != 0) {
prevnode = temp;
temp = temp->link;
}
free(temp);
prevnode->link = 0;
}
}

// Function to delete from any specified


// position from the linked list
void deletePosition()
{
struct node *temp, *position;
int i = 1, pos;

// If LL is empty
if (start == NULL)
printf("\nList is empty\n");

// Otherwise
else {
printf("\nEnter index : ");

// Position to be deleted
scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = start;

// Traverse till position


while (i < pos - 1) {
temp = temp->link;
i++;
}

// Change Links
position = temp->link;
temp->link = position->link;

// Free memory
free(position);
}
}

// Function to find the maximum element


// in the linked list
void maximum()
{
int a[10];
int i;
struct node* temp;

// If LL is empty
if (start == NULL)
printf("\nList is empty\n");

// Otherwise
else {
temp = start;
int max = temp->info;

// Traverse LL and update the


// maximum element
while (temp != NULL) {

// Update the maximum


// element
if (max < temp->info)
max = temp->info;
temp = temp->link;
}
printf("\nMaximum number "
"is : %d ",
max);
}
}

// Function to find the mean of the


// elements in the linked list
void mean()
{
int a[10];
int i;
struct node* temp;
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");

// Otherwise
else {
temp = start;

// Stores the sum and count of


// element in the LL
int sum = 0, count = 0;
float m;

// Traverse the LL
while (temp != NULL) {

// Update the sum


sum = sum + temp->info;
temp = temp->link;
count++;
}

// Find the mean


m = sum / count;

// Print the mean value


printf("\nMean is %f ", m);
}
}

// Function to sort the linked list


// in ascending order
void sort()
{
struct node* current = start;
struct node* index = NULL;
int temp;

// If LL is empty
if (start == NULL) {
return;
}

// Else
else {

// Traverse the LL
while (current != NULL) {
index = current->link;

// Traverse the LL nestedly


// and find the minimum
// element
while (index != NULL) {

// Swap with it the value


// at current
if (current->info > index->info) {
temp = current->info;
current->info = index->info;
index->info = temp;
}
index = index->link;
}

// Update the current


current = current->link;
}
}
}

// Function to reverse the linked list


void reverseLL()
{
struct node *t1, *t2, *temp;
t1 = t2 = NULL;

// If LL is empty
if (start == NULL)
printf("List is empty\n");

// Else
else {

// Traverse the LL
while (start != NULL) {

// reversing of points
t2 = start->link;
start->link = t1;
t1 = start;
start = t2;
}
start = t1;

// New head Node


temp = start;

printf("Reversed linked "


"list is : ");

// Print the LL
while (temp != NULL) {
printf("%d ", temp->info);
temp = temp->link;
}
}
}

// Driver Code
int main()
{
int choice;
while (1) {

printf("\n\t1 To see list\n");


printf("\t2 For insertion at"
" starting\n");
printf("\t3 For insertion at"
" end\n");
printf("\t4 For insertion at "
"any position\n");
printf("\t5 For deletion of "
"first element\n");
printf("\t6 For deletion of "
"last element\n");
printf("\t7 For deletion of "
"element at any position\n");
printf("\t8 To find maximum among"
" the elements\n");
printf("\t9 To find mean of "
"the elements\n");
printf("\t10 To sort element\n");
printf("\t11 To reverse the "
"linked list\n");
printf("\t12 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);

switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
maximum();
break;
case 9:
mean();
break;
case 10:
sort();
break;
case 11:
reverseLL();
break;
case 12:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}

You might also like