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

Data Structures With C Record

Uploaded by

dhanushadas2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Data Structures With C Record

Uploaded by

dhanushadas2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Program 1:

Write a C program for searching an item using Binary search .


#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
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]);
printf("Enter value to find\n");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key)
{
printf("%d found at location %d\n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list\n", key);
return 0;
}
Output : 1

enter no. of elements


5
enter the integers
23
45
78
89
56
enter value to search
78
78 is found at location 2
--------------------------------
Process exited after 11.79 seconds with return value 0
Press any key to continue . . .

Output : 2

enter no. of elements


5
enter the integers
56
45
78
89
15
enter value to search
2
elemnt is not found
--------------------------------
Process exited after 8.83 seconds with return value 19
Press any key to continue . . .
Program 2 :
Write a c program for sorting using insertion sort

#include <stdio.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int i;
int n = sizeof(arr) ;
insertionSort(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}

Output :

sorting order using insertion


5 6 11 12 13

--------------------------------
Process exited after 0.371 seconds with return value 0
Press any key to continue . . .
Program 3 :
Write a C prgram for single linked list with insertion and deletion at front

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

struct Node
{
int data;
struct Node *next;
};

void deleteStart (struct Node **head)


{
struct Node *temp = *head;

if (*head == NULL)
{
printf ("Linked List Empty, nothing to delete");
return;
}

*head = (*head)->next;

printf ("\n%d deleted\n", temp->data);


free (temp);
}

void insertStart (struct Node **head, int data)


{

struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));

newNode->data = data;

newNode->next = *head;

*head = newNode;
printf ("\n%d Inserted\n", newNode->data);
}

void display (struct Node *node)


{
printf ("\nLinked List: ");

while (node != NULL)


{
printf ("%d ", node->data);
node = node->next;
}
printf ("\n");
}

int main ()
{
struct Node *head = NULL;
insertStart (&head, 100);
insertStart (&head, 80);
insertStart (&head, 60);
insertStart (&head, 40);
insertStart (&head, 20);

display (head);
deleteStart (&head);
deleteStart (&head);
display (head);
return 0;
}

Output :
inserted elements :

100 Inserted

80 Inserted

60 Inserted

40 Inserted

20 Inserted

Linked List: 20 40 60 80 100

20 deleted

40 deleted

Linked List: 60 80 100

--------------------------------
Process exited after 0.3316 seconds with return value 0
Press any key to continue . . .
Program 4 :

Write a C program to reverse linked list iteratively

#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node* next;
};
struct node* head = NULL;
struct node* create_node(int);
void insert_begin(int);
void reverse_list();
void print();
int main()
{
insert_begin(10);
insert_begin(90);
insert_begin(31);
insert_begin(78);
insert_begin(99);
printf("Linked List before reversed: \n");
print();
reverse_list();
printf("\nLinked List after reversed: \n");
print();
return 0;
}
struct node* create_node(int data)
{
struct node* new_node = (struct node*) malloc (sizeof(struct node));
if (new_node == NULL)
{
printf("Memory can't be allocated for new node");
return NULL;
}
else
{
new_node -> data = data;
new_node -> next = NULL;
return new_node;
}
}

void insert_begin(int data)


{
struct node* new_node = create_node(data);
if (new_node != NULL)
{
new_node -> next = head;
head = new_node;
}
}
void reverse_list()
{
if (head == NULL)
{
return;
}
struct node* temp = head;
struct node* new_head = NULL;

while (temp != NULL)


{
struct node* new_node = create_node(temp->data);
new_node->next = new_head;
new_head = new_node;
temp = temp->next;
}

head = new_head;
}
void print()
{
struct node* temp = head;
while (temp != NULL)
{
printf("%d --> ", temp->data);
temp = temp->next;
}
printf("NULL \n");
}

Output :

Linked List before reversed:


99 --> 78 --> 31 --> 90 --> 10 --> NULL

Linked List after reversed:


10 --> 90 --> 31 --> 78 --> 99 --> NULL

--------------------------------
Process exited after 0.2673 seconds with return value 0
Press any key to continue . . .
Program 5 :

Write a C program for polynomial addition

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

struct Node {
int coef;
int exp;
struct Node* next;
};

typedef struct Node Node;

void insert(Node** poly, int coef, int exp) {


Node* temp = (Node*) malloc(sizeof(Node));
temp->coef = coef;
temp->exp = exp;
temp->next = NULL;

if (*poly == NULL) {
*poly = temp;
return;
}

Node* current = *poly;

while (current->next != NULL) {


current = current->next;
}

current->next = temp;
}

void print(Node* poly) {


if (poly == NULL) {
printf("0\n");
return;
}

Node* current = poly;

while (current != NULL) {


printf("%dx^%d", current->coef, current->exp);
if (current->next != NULL) {
printf(" + ");
}
current = current->next;
}
printf("\n");
}

Node* add(Node* poly1, Node* poly2) {


Node* result = NULL;

while (poly1 != NULL && poly2 != NULL) {


if (poly1->exp == poly2->exp) {
insert(&result, poly1->coef + poly2->coef, poly1->exp);
poly1 = poly1->next;
poly2 = poly2->next;
} else if (poly1->exp > poly2->exp) {
insert(&result, poly1->coef, poly1->exp);
poly1 = poly1->next;
} else {
insert(&result, poly2->coef, poly2->exp);
poly2 = poly2->next;
}
}

while (poly1 != NULL) {


insert(&result, poly1->coef, poly1->exp);
poly1 = poly1->next;
}

while (poly2 != NULL) {


insert(&result, poly2->coef, poly2->exp);
poly2 = poly2->next;
}

return result;
}

int main() {
Node* poly1 = NULL;
insert(&poly1, 5, 4);
insert(&poly1, 3, 2);
insert(&poly1, 1, 0);

Node* poly2 = NULL;


insert(&poly2, 4, 4);
insert(&poly2, 2, 2);
insert(&poly2, 1, 1);

printf("First polynomial: ");


print(poly1);

printf("Second polynomial: ");


print(poly2);

Node* result = add(poly1, poly2);


printf("Result: ");
print(result);

return 0;
}

Output :

First polynomial: 5x^4 + 3x^2 + 1x^0


Second polynomial: 4x^4 + 2x^2 + 1x^1
Result: 9x^4 + 5x^2 + 1x^1 + 1x^0

--------------------------------
Process exited after 0.2379 seconds with return value 0
Press any key to continue . . .

Program 6:
C program to implement all functions used in Doubly Linked List

#include <stdio.h>
#include <stdlib.h>
int i = 0;

typedef struct node


{
int key;
struct node* prev;
struct node* next;

} node;

node* head = NULL;


node* first = NULL;
node* temp = NULL;
node* tail = NULL;

void addnode(int k)
{

node* ptr
= (node*)malloc(sizeof(node));

ptr->key = k;

ptr->next = NULL;
ptr->prev = NULL;

if (head == NULL)
{
head = ptr;
first = head;
tail = head;
}
else
{
temp = ptr;
first->next = temp;
temp->prev = first;
first = temp;
tail = temp;
}

i++;
}

void traverse()
{
node* ptr = head;
while (ptr != NULL)
{

printf("%d ", ptr->key);


ptr = ptr->next;
}

printf("\n");
}

void insertatbegin(int k)
{

node* ptr= (node*)malloc(sizeof(node));


ptr->key = k;

ptr->next = NULL;
ptr->prev = NULL;

if (head == NULL)
{
first = ptr;
first = head;
tail = head;
}

else
{
temp = ptr;
temp->next = head;
head->prev = temp;
head = temp;
}
i++;
}

void insertatend(int k)
{

node* ptr= (node*)malloc(sizeof(node));


ptr->key = k;

ptr->next = NULL;
ptr->prev = NULL;

if (head == NULL)
{
first = ptr;
first = head;
tail = head;
}

else
{
temp = ptr;
temp->prev = tail;
tail->next = temp;
tail = temp;
}
i++;
}

void insertatpos(int k, int pos)


{
if (pos < 1 || pos > i + 1)
{
printf("Please enter a"" valid position\n");
}
else if (pos == 1)
{
insertatbegin(k);
}

else if (pos == i + 1)
{
insertatend(k);
}

else
{
node* src = head;

while (pos--)
{
src = src->next;
}

node **da, **ba;


node* ptr= (node*)malloc(sizeof(node));
ptr->next = NULL;
ptr->prev = NULL;
ptr->key = k;
ba = &src;
da = &(src->prev);
ptr->next = (*ba);
ptr->prev = (*da);
(*da)->next = ptr;
(*ba)->prev = ptr;
i++;
}
}
void delatbegin()
{

head = head->next;
i--;
}

void delatend()
{
// Mode tail to the prev and
// decrease length by 1
tail = tail->prev;
tail->next = NULL;
i--;
}

void delatpos(int pos)


{
if (pos < 1 || pos > i + 1)
{
printf("Please enter a"" valid position\n");
}
else if (pos == 1)
{
delatbegin();
}
else if (pos == i)
{
delatend();
}
else
{
node* src = head;
pos--;
while (pos--)
{
src = src->next;
}
node **pre, **aft;
pre = &(src->prev);
aft = &(src->next);
(*pre)->next = (*aft);
(*aft)->prev = (*pre);
i--;
}
}
int main()
{
addnode(2);
addnode(4);
addnode(9);
addnode(1);
addnode(21);
addnode(22);

printf("Linked List: ");


traverse();

printf("\n");
insertatbegin(1);
printf("Linked List after"" inserting 1 ""at beginning: ");
traverse();

insertatend(0);
printf("Linked List after ""inserting 0 at end: ");
traverse();
insertatpos(44, 3);
printf("Linked List after ""inserting 44 ""after 3rd Node: ");
traverse();

printf("\n");
delatbegin();
printf("Linked List after ""deleting node ""at beginning: ");
traverse();
delatend();
printf("Linked List after ""deleting node at end: ");
traverse();

printf("Linked List after ""deleting node ""at position 5: ");


delatpos(5);
traverse();
return 0;
}

Output :

Linked List: 2 4 9 1 21 22

Linked List after inserting 1 at beginning: 1 2 4 9 1 21 22


Linked List after inserting 0 at end: 1 2 4 9 1 21 22 0
Linked List after inserting 44 after 3rd Node: 1 2 4 44 9 1 21 22 0

Linked List after deleting node at beginning: 2 4 44 9 1 21 22 0


Linked List after deleting node at end: 2 4 44 9 1 21 22
Linked List after deleting node at position 5: 2 4 44 9 21 22

--------------------------------
Process exited after 0.3 seconds with return value 0
Press any key to continue . . .

Program 7 :
Write a C program for circula linked list with operations

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

struct node
{
int data;
struct node *next;
};

struct node *head;

// function prototyping
struct node *create(int);
void insert_at_begin(int);
void insert_at_end(int);
void insert_at_position(int, int);
void delete_at_begin();
void delete_at_end();
void delete_at_position(int);
void search(int);
void update(int, int);
void print_list();
void print_list_reverse();
int size_of_list();
int getData();
int getPosition();
int main()
{
char user_active = 'Y';
int user_choice;
int data, position;

while(user_active == 'Y' || user_active == 'y')


{

printf("\n\n------ Circular Singly Linked List -------\n");


printf("\n1. Insert a node at beginning");
printf("\n2. Insert a node at end");
printf("\n3. Insert a node at given position");
printf("\n\n4. Delete a node from beginning");
printf("\n5. Delete a node from end");
printf("\n6. Delete a node from given position");
printf("\n\n7. Print list from beginning");
printf("\n8. Print list from end");
printf("\n9. Search a node data");
printf("\n10. Update a node data");
printf("\n11. Exit");
printf("\n\n------------------------------\n");

printf("\nEnter your choice: ");


scanf("%d", &user_choice);

printf("\n------------------------------\n");
switch(user_choice)
{
case 1:
printf("\nInserting a node at beginning");
data = getData();
insert_at_begin(data);
break;

case 2:
printf("\nInserting a node at end");
data = getData();
insert_at_end(data);
break;

case 3:
printf("\nInserting a node at the given position");
data = getData();
position = getPosition();
insert_at_position(data, position);
break;

case 4:
printf("\nDeleting a node from beginning\n");
delete_at_begin();
break;

case 5:
printf("\nDeleting a node from end\n");
delete_at_end();
break;

case 6:
printf("\nDelete a node from given position\n");
position = getPosition();
delete_at_position(position);
break;

case 7:
printf("\nPrinting the list from beginning\n\n");
print_list();
break;

case 8:
printf("\nPrinting the list from end\n\n");
if (head == NULL) {
printf("\n\tList is Empty!\n");
} else {
print_list_reverse(head);
}
break;

case 9:
printf("\nSearching the node data");
data = getData();
search(data);
break;

case 10:
printf("\nUpdating the node data");
data = getData();
position = getPosition();
update(position, data);
break;

case 11:
printf("\nProgram was terminated\n\n");
return 0;

default:
printf("\n\t Invalid Choice\n");
}

printf("\n...............................\n");
printf("\nDo you want to continue? (Y/N) : ");
fflush(stdin);
scanf(" %c", &user_active);
}

return 0;
}

struct node *create(int data)


{
struct node *new_node = (struct node *)malloc(sizeof(struct node));

if (new_node == NULL)
{
printf("\nMemory can't be allocated.\n");
return NULL;
}

new_node->data = data;
new_node->next = NULL;

return new_node;
}

// function to insert a new node at the beginning of the list


void insert_at_begin(int data)
{
struct node *new_node = create(data);

if (new_node != NULL)
{
struct node *last = head;

// if the list is empty


if (head == NULL)
{
head = new_node;
new_node->next = head;
return;
}

// traverse to the end node


while (last->next != head)
{
last = last->next;
}

// update the last node to the new node


last->next = new_node;

// update the next pointer of the new node to the head node
new_node->next = head;
// update the head of the list to new node
head = new_node;
}
}

// function to insert a new node at the end of the list


void insert_at_end(int data)
{
struct node *new_node = create(data);

if (new_node != NULL)
{

// if the list is empty


if (head == NULL)
{
head = new_node;
new_node->next = head;
return;
}

struct node *last = head;

// traverse to the end node


while (last->next != head)
{
last = last->next;
}

// update the last node to the new node


last->next = new_node;

// update the next pointer of the new node to the head node
new_node->next = head;
}
}

// function to insert a new node at the given position


void insert_at_position(int position, int data)
{
// checking if the position is valid or not
if (position <= 0)
{
printf("\nInvalid Position\n");
}
else if (head == NULL && position > 1)
{
printf("\nInvalid Position\n");
}
else if (head != NULL && position > size_of_list())
{
printf("\nInvalid Position\n");
}
else if (position == 1)
{
insert_at_begin(data);
}
else
{
struct node *new_node = create(data);

if (new_node != NULL)
{
struct node *temp = head, *prev = NULL;
// Since, temp is already pointing to first node
// then count will be start at second node
int i = 1;

// traverse the list to the given position


while (++i <= position)
{
prev = temp;
temp = temp->next;
}

// update the prev node to the new noe


prev->next = new_node;

// update the new node to the temp (position node)


new_node->next = temp;
}
}
}

// function to delete a node from the beginning of the list


void delete_at_begin()
{
// check where the list is empty or not
if (head == NULL)
{
printf("\n List is Empty! \n");
return;
}

// traverse to the end of the list


struct node *last = head;
struct node *temp = head;

// if only one node in the list


if (last->next == head)
{
free(last);
head = NULL;
return;
}

// traverse to the last node


while (last->next != head)
{
last = last->next;
}

head = head->next;
last->next = head;

free(temp);
temp = NULL;
}

// function to delete a node from the end of the list


void delete_at_end()
{
// check where the list is empty or not
if (head == NULL)
{
printf("\n List is Empty! \n");
return;
}

// traverse to the end of the list


struct node *prev = head;
struct node *temp = head->next;

// if only one node in the list


if (prev->next == head)
{
free(prev);
head = NULL;
return;
}

while (temp->next != head)


{
prev = temp;
temp = temp->next;
}

prev->next = head;

free(temp);
temp = NULL;
}
// function to delete a node from the given position
void delete_at_position(int position)
{
if (position <= 0)
{
printf("\n Invalid Position \n");
}
else if (position > size_of_list())
{
printf("\n Invalid position \n");
}
else if (position == 1)
{
delete_at_begin();
}
else if (position == size_of_list())
{
delete_at_end();
}
else
{
struct node *temp = head;
struct node *prev = NULL;
int i = 1;

while (i < position)


{
prev = temp;
temp = temp->next;
i += 1;
}

prev->next = temp->next;
free(temp);
temp = NULL;
}
}

// print the node values


void print_list()
{
struct node *temp = head;

if (head == NULL)
{
printf("\n List is Empty! \n");
return;
}

printf("\n");
do
{
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);

printf("\n");
}

// print the node values recursively


void print_list_reverse(struct node* temp)
{
if (temp->next == head)
{
printf("%d ", temp->data);
return;
}
print_list_reverse(temp->next);
printf("%d ", temp->data);
}

// search a data into the list


void search(int key)
{
struct node* temp = head;

do
{
if (temp->data == key)
{
printf("\n\t Data Found\n");
return;
}
temp = temp->next;
}while (temp->next != head);
printf("\n\tData not Found\n");
}

// function to update a node


void update(int position, int new_data)
{
if (position <= 0 || position > size_of_list())
{
printf("\n Invalid position\n");
return;
}

struct node* temp = head;


int i = 0;

while (i <= position)


{
temp = temp->next;
i += 1;
}

temp->data = new_data;
}

// function to calculate the size of the list


int size_of_list()
{
if (head == NULL)
{
return 0;
}

struct node *temp = head;


int count = 1;

while (temp->next != head)


{
count += 1;
temp = temp->next;
}
return count;
}

int getData()
{
int data;
printf("\n\nEnter Data: ");
scanf("%d", &data);

return data;
}

int getPosition()
{
int pos;
printf("\n\nEnter Position: ");
scanf("%d", &pos);

return pos;
}

Output :

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 1

------------------------------

Inserting a node at beginning

Enter Data: 1

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 1

------------------------------

Inserting a node at beginning


Enter Data: 2

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 1

------------------------------

Inserting a node at beginning

Enter Data: 3

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 7

------------------------------

Printing the list from beginning

321

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 2

------------------------------

Inserting a node at end

Enter Data: 12

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------


1. Insert a node at beginning
2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 2

------------------------------

Inserting a node at end

Enter Data: 23

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 7

------------------------------
Printing the list from beginning

3 2 1 12 23

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 3

------------------------------

Inserting a node at the given position

Enter Data: 25

Enter Position: 2

Invalid Position

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 7

------------------------------

Printing the list from beginning

3 2 1 12 23

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 10

------------------------------

Updating the node data


Enter Data: 122

Enter Position: 5

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 7

------------------------------

Printing the list from beginning

3 122 1 12 23

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 4

------------------------------

Deleting a node from beginning

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 7

------------------------------

Printing the list from beginning

122 1 12 23

...............................

Do you want to continue? (Y/N) : y


------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 9

------------------------------

Searching the node data

Enter Data: 3

Data not Found

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit
------------------------------

Enter your choice: 7

------------------------------

Printing the list from beginning

122 1 12 23

...............................

Do you want to continue? (Y/N) : y

------ Circular Singly Linked List -------

1. Insert a node at beginning


2. Insert a node at end
3. Insert a node at given position

4. Delete a node from beginning


5. Delete a node from end
6. Delete a node from given position

7. Print list from beginning


8. Print list from end
9. Search a node data
10. Update a node data
11. Exit

------------------------------

Enter your choice: 9

------------------------------

Searching the node data

Enter Data: 3

Data not Found

...............................

Do you want to continue? (Y/N) : n


Process exited after 288.8 seconds with return value 0
Press any key to continue . .
Program 8 :

Write a c program for stacks using linked list

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

struct Node
{
int data;
struct Node *next;
};

struct Node *head = NULL;

void push(int val)


{
//create new node
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = val;

//make the new node points to the head node


newNode->next = head;

//make the new node as head node


//so that head will always point the last inserted data
head = newNode;
}

void pop()
{
//temp is used to free the head node
struct Node *temp;

if(head == NULL)
printf("Stack is Empty\n");
else
{
printf("Poped element = %d\n", head->data);

//backup the head node


temp = head;

//make the head node points to the next node.


//logically removing the node
head = head->next;

//free the poped element's memory


free(temp);
}
}

//print the linked list


void display()
{
struct Node *temp = head;

//iterate the entire linked list and print the data


while(temp != NULL)
{
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main()
{
push(10);
push(20);
push(30);
printf("Linked List\n");
display();
pop();
printf("After the pop, the new linked list\n");
display();
pop();
printf("After the pop, the new linked list\n");
display();

return 0;
}

Output:

Linked List
30->20->10->NULL
Poped element = 30
After the pop, the new linked list
20->10->NULL
Poped element = 20
After the pop, the new linked list
10->NULL

--------------------------------
Process exited after 0.2331 seconds with return value 0
Press any key to continue . . .
Program 9 :

Write a program to evaluate a postfix expression using a stack.

#include<stdio.h>
int stack[20];
int top = -1;

void push(int x)
{
stack[++top] = x;
}

int pop()
{
return stack[top--];
}

int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}

Output:

Enter the expression :: 4*5/-6*25

The result of expression 4*5/-6*25 = 5

--------------------------------
Process exited after 12.52 seconds with return value 0
Press any key to continue . . .

Program 10:
Write a C program for queues using linked list

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n");
printf("\
n=================================================================\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}

Output:
*************************Main Menu*****************************

=================================================================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1


Enter value?
12

*************************Main Menu*****************************

=================================================================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter value?
23

*************************Main Menu*****************************

=================================================================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter value?
56

*************************Main Menu*****************************

=================================================================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?3

printing values .....

12

23

56

*************************Main Menu*****************************
=================================================================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?2

*************************Main Menu*****************************

=================================================================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?3

printing values .....

23

56

*************************Main Menu*****************************

=================================================================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ? 4

--------------------------------
Process exited after 118.9 seconds with return value 0
Press any key to continue . . .

Program 11:
Write a Program to use a stack to and evaluate an infix expression and convert it to postfix.

#include<stdio.h>
#include<ctype.h>

char stack[100];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}

int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;

while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c ",pop());
}return 0;
}

Output:

Enter the expression : a+b*c/*

abc*/*+
--------------------------------
Process exited after 13.95 seconds with return value 0
Press any key to continue . . .

Program 12:
Write a C program for tree traversal

// Tree traversal in C

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

struct node {
int item;
struct node* left;
struct node* right;
};

// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}

// preorderTraversal traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}

// postorderTraversal traversal
void postorderTraversal(struct node* root)
{
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}

// Create a new Node


struct node* createNode(value)
{
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}

struct node* insertLeft(struct node* root, int value)


{
root->left = createNode(value);
return root->left;
}

struct node* insertRight(struct node* root, int value)


{
root->right = createNode(value);
return root->right;
}
int main()
{
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);

insertLeft(root->left, 5);
insertRight(root->left, 6);

printf("Inorder traversal \n");


inorderTraversal(root);

printf("\nPreorder traversal \n");


preorderTraversal(root);

printf("\nPostorder traversal \n");


postorderTraversal(root);
}

Output :

Inorder traversal
5 ->12 ->6 ->1 ->9 ->
Preorder traversal
1 ->12 ->5 ->6 ->9 ->
Postorder traversal
5 ->6 ->12 ->9 ->1 ->
--------------------------------
Process exited after 0.6781 seconds with return value 4
Press any key to continue . . .

You might also like