ADSA File
ADSA File
-25098
Program 1
AIM: Write a Program to implement circular Linked List with all operations.
Source Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{ int data;
struct node *next;
};
struct node *head;
}
} void
beginsert()
{ struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
} else
{ printf("\nEnter the node data?");
scanf("%d",&item); ptr -> data
= item;
if(head == NULL)
{ head = ptr; ptr ->
next = head;
}
else
{ temp = head; while(temp-
>next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr; }
printf("\nnode inserted\n");
}
} void
lastinsert()
{ struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
} else
{ pri
ntf("\
nEnt
er
Data
?");
scanf
("%d
",&it
em);
ptr-
>data
=
item;
if(head == NULL)
{ head = ptr; ptr ->
next = head;
}
else
{ temp = head; while(temp ->
next != head)
{ temp = temp -> next;
} temp -> next =
ptr; ptr -> next =
head;
}
printf("\nnode inserted\n");
}
void begin_delete()
{ struct node *ptr;
if(head ==
NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next == head)
{
head = NULL;
free(head); printf("\nnode
deleted\n");
}
else
{ ptr = head; while(ptr ->
next != head) ptr = ptr ->
next;
ptr->next = head->next;
free(head); head = ptr-
>next; printf("\nnode
deleted\n");
}
}
void last_delete()
} else
{ ptr = head; while(ptr -
>next != head)
{ preptr=ptr; ptr
= ptr->next;
}
preptr->next = ptr ->
next; free(ptr); printf("\
nnode deleted\n");
}
}
void search()
{ struct node *ptr; int
item,i=0,flag=1;
ptr = head; if(ptr
== NULL)
{ printf("\nEmpty List\n");
} else
{ printf("\nEnter item which you want to search?\
n");
scanf("%d",&item);
if(head ->data == item)
{
printf("item found at location
%d",i+1); flag=0; } else {
while (ptr->next != head)
{ if(ptr->data == item)
{ printf("item found at location %d
",i+1); flag=0; break; } else
{ flag=1; } i++;
ptr = ptr ->
next;
} } if(flag !
= 0)
void display()
{ struct node *ptr;
ptr=head; if(head
== NULL)
{ printf("\nnothing to print");
} else
{ printf("\n printing values ... \n");
}
OUTPUT
Program 2
AIM: Write a Program to implement doubly Linked List with all operations.
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node
{ int data;
struct node *prev, *next;
}; struct node* start =
NULL; void traverse(){
void insertAtEnd()
{ int data;
struct node *temp, *trav;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->data = data;
temp->next =
NULL; trav = start;
if (start == NULL) {
start = temp;
}
else {
while (trav->next != NULL)
trav = trav->next;
temp->prev = trav; trav-
>next = temp;
}
}
void insertAtPosition(){
int data, pos, i = 1; struct node *temp,
*newnode; newnode =
malloc(sizeof(struct node)); newnode-
>next = NULL; newnode->prev =
NULL;
if (start == NULL) {
start = newnode; newnode-
>prev = NULL;
newnode->next = NULL;
}
else if (pos == 1) {
newnode->next = start; newnode-
>next->prev = newnode;
newnode->prev = NULL; start =
newnode;
}
else {
while (i < pos - 1) {
temp = temp-
>next; i++; }
newnode->next = temp->next;
newnode->prev = temp; temp-
>next = newnode;
temp->next->prev = newnode;
}
}
void deleteFirst()
{ struct node*
temp; if (start ==
NULL)
printf("\nList is empty\n");
else {
temp = start; start
= start->next; if
(start != NULL)
start->prev = NULL;
free(temp);
}
}
void deleteEnd()
{ struct node*
temp; if (start ==
NULL)
printf("\nList is empty\n");
temp = start; while (temp-
>next != NULL) temp =
temp->next;
if (start->next == NULL)
start = NULL;
else {
temp->prev->next = NULL;
free(temp);
}
}
void deletePosition(){
int pos, i = 1; struct node
*temp, *position;
temp = start;
if (start == NULL)
printf("\nList is empty\n");
else {
if (pos == 1) {
position = start;
start = start->next;
if (start != NULL) {
start->prev = NULL;
}
free(position);
return;
}
position = temp->next; if
(position->next != NULL)
position->next->prev = temp;
temp->next = position->next;
free(position);
}
}
int main()
{ int
choice;
while (1) {
printf("\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 exit\n"); printf("\nEnter
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:
exit(1);
break;
default: printf("Incorrect Choice. Try
Again \n"); continue;
}}
return
0;
}
OUTPUT
Program 3
AIM: Write a Program to implement Doubly Circular Linked List with all operations.
Source Code:
#include<stdio.h>
#include<stdlib.h>
// function prototyping
struct node* create(int);
void insert_begin(int);
void insert_end(int);
void insert_mid(int,
int); void
delete_begin(); void
delete_end(); void
delete_mid(); int
search(int); void
update(int, int); void
sort(); int list_size();
void display();
void display_reverse(struct node*);
int get_data();
int get_position();
int main()
{ char user_active = 'Y';
int user_choice;
int data, position;
printf("\n \n");
switch(user_choice)
{ case 1: printf("\nInserting a node at
beginning"); data = get_data();
insert_begin(data); break;
case 6:
printf("\nDelete a node from given position\
n"); position = get_position();
delete_mid(position); break;
}
break;
default:
printf("\n\tInvalid Choice\n");
}
return 0;
}
// creates a new node 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; new_node->prev
= NULL;
return new_node;
}
if (new_node)
{
// if list is empty
if (head == NULL)
{
new_node->next =
new_node; new_node->prev
= new_node; head =
new_node; return;
}
head->prev->next =
new_node; new_node->prev =
head->prev; new_node->next
= head; head->prev =
new_node;
head = new_node;
}
}
if (new_node)
{ if (head == NULL)
{
new_node->next =
new_node; new_node->prev
= new_node; head =
new_node; return;
}
head->prev->next =
new_node; new_node->prev =
head->prev; new_node->next
= head;
head->prev = new_node;
}
}
void insert_mid(int position, int data)
{
if (position <= 0)
{ printf("\nInvalid Position\n");
} else if (head == NULL && position > 1) { printf("\
nInvalid Position\n");
} else if (head != NULL && position > list_size()) { printf("\
nInvalid Position\n");
} else if (position == 1)
{ insert_begin(data);
} else { struct node *new_node =
create(data);
if (new_node != NULL) {
struct node *temp = head, *prev = NULL;
int i = 1;
new_node->next = temp;
}
}
}
void delete_begin()
{ if (head == NULL) {
printf("\nList is Empty\n");
return;
} else if (head->next == head)
{ free(head);
head = NULL;
return;
}
free(temp);
temp = NULL;
} void
delete_end()
{ if (head == NULL) {
printf("\nList is Empty\n");
return;
} else if (head->next == head)
{ free(head);
head = NULL;
return; } struct node* last_node =
head->prev;
last_node->prev->next = head;
head->prev = last_node->prev;
free(last_node);
last_node = NULL;
}
void delete_mid(int position)
{ if (position <= 0) {
printf("\n Invalid Position \n");
}
else if (position > list_size()) {
printf("\n Invalid position \n");
} else if (position ==
1) {
delete_begin(); } else if
(position == list_size()) {
delete_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;
temp->next->prev = prev;
free(temp);
temp = NULL;
}
} int search(int
key)
{ if (head == NULL) {
printf("\n Not Found \n");
return 0;
}
return 0;
}
do {
temp2 = temp1->next;
while(temp2 != head)
{ if (temp1->data > temp2-
>data)
{ value = temp1->data;
temp1->data = temp2-
>data; temp2->data =
value;
}
temp2 = temp2->next;
} temp1 = temp1-
>next;
}while (temp1->next != head);
display_reverse(temp->next);
printf("%d ", temp->data);
}
do {
count += 1; temp =
temp->next;
} while (temp != head);
return count;
}
int get_data()
{ int data; printf("\n\nEnter
Data: "); scanf("%d",
&data);
return data;
}
int get_position()
{ int position; printf("\n\nEnter
Position: "); scanf("%d",
&position);
return position;
}
OUTPUT
Program 4
AIM: Write a Program to create a File and write some data into it.
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr; char name[20];
int age; float salary; fptr =
fopen("emp.txt", "w");
if (fptr == NULL)
{ printf("File does not exist.\n");
return; } printf("Enter the
name:\n"); scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
Program 5
AIM: Write a Program to create a Binary tree and implements and print its pre order, post
order and in-order traversal.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{ int info; struct Node
*left; struct Node
*right;
};
struct Node *root=NULL;
}while(ch=='y'||ch=='Y');
}
}
void postorder(struct Node *ptr)
{
}
struct Node *getNode()
{ struct Node *temp;
temp=(struct Node*)malloc(sizeof(struct Node));
temp->left=NULL; temp-
>right=NULL; return
temp;
}
void insert(struct Node *root,struct Node *New)
{ char ch;
printf("Where to insert new Node of %d (L/R)",root-
>info); ch=getche(); if(ch=='L'||ch=='l')
{
if(root->left==NULL)
root->left=New;
else
insert(root->left,New);
} else
{ if(root->right==NULL)
root->right=New;
else
insert(root->right,New);
void create()
if(root==NULL)
{
root=ptr;
} else
{
insert(root,ptr);
}
}
OUTPUT
Program 6
#include <stdio.h>
#include
<stdlib.h> int
main()
{
FILE * fPtr;
char ch;
if(fPtr == NULL)
{ printf("Unable to open file.\
n");
printf("Please check whether file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}
do
{ ch = fgetc(fPtr);
putchar(ch); }
while(ch != EOF);
fclose(fPtr);
return 0;
}
OUTPUT
Program 7
AIM: Write a Program to copy the contents of one file to another file.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{ char ch, fileName1[20],
fileName2[20]; FILE *fs, *ft;
printf("Enter Source File Name (with extension):
"); gets(fileName1); fs = fopen(fileName1, "r");
if(fs == NULL)
{ printf("\nError in Opening the file, %s",
fileName1); getch(); return 0; }
printf("Enter Target File Name (with extension):
"); gets(fileName2); ft = fopen(fileName2, "w");
if(ft == NULL)
{ printf("\nError in Opening the file, %s",
fileName2); getch(); return 0; } ch = fgetc(fs);
while(ch != EOF)
{ fputc(ch, ft);
ch =
fgetc(fs);
}
printf("\nFile copied successfully.");
fclose(fs);
fclose(ft);
getch();
return 0;
}
OUTPUT