0% found this document useful (0 votes)
9 views30 pages

ADSA File

The document contains source code for three programs that implement different types of linked lists: circular linked list, doubly linked list, and doubly circular linked list. Each program includes functions for inserting, deleting, searching, and displaying nodes, along with a menu-driven interface for user interaction. The code is structured with proper function prototypes and includes error handling for memory allocation and list operations.

Uploaded by

nandinidahiya35
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)
9 views30 pages

ADSA File

The document contains source code for three programs that implement different types of linked lists: circular linked list, doubly linked list, and doubly circular linked list. Each program includes functions for inserting, deleting, searching, and displaying nodes, along with a menu-driven interface for user interaction. The code is structured with proper function prototypes and includes error handling for memory allocation and list operations.

Uploaded by

nandinidahiya35
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/ 30

Roll no.

-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 ();


void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void
random_delete();
void display(); void
search(); void main
()
{ int choice =0;
while(choice !=
7)
{ printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from
last\n5.Search for an element\n6.Show\n7.Exit\
n"); printf("\nEnter your choice?\n");
scanf("\n%d",&choice); switch(choice)
{ case 1:
beginsert();
break; case 2:
lastinsert();
break; case 3:
begin_delete()
; break; case
4:
last_delete();
break; case 5:
search();
break; case 6:
display();
break;
case 7: exit(0); break; default:
printf("Please enter valid
choice..");
}

CSE DEPARTMENT 2023-24


Roll no.-25098

}
} 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-

CSE DEPARTMENT 2023-24


Roll no.-25098

>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()

CSE DEPARTMENT 2023-24


Roll no.-25098

{ struct node *ptr, *preptr;


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)
{ 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)

CSE DEPARTMENT 2023-24


Roll no.-25098

{ printf("Item not found\n");


}
}

void display()
{ struct node *ptr;
ptr=head; if(head
== NULL)
{ printf("\nnothing to print");
} else
{ printf("\n printing values ... \n");

while(ptr -> next != head)


{

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


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

}
OUTPUT

CSE DEPARTMENT 2023-24


Roll no.-25098

CSE DEPARTMENT 2023-24


Roll no.-25098

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(){

if (start == NULL) { printf("\


nList is empty\n"); return;
}

struct node* temp; temp = start;


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

void insertAtFront(){ int data; struct node* temp;


temp = (struct node*)malloc(sizeof(struct
node)); printf("\nEnter number to be inserted:
");
scanf("%d", &data);
temp->data = data;
temp->prev =
NULL; temp->next
= start; start = temp;
}

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;

CSE DEPARTMENT 2023-24


Roll no.-25098

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;

printf("\nEnter position : ");


scanf("%d", &pos); printf("\nEnter
number to be inserted: ");
scanf("%d", &data); newnode-
>data = data;
temp = start;

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++; }

CSE DEPARTMENT 2023-24


Roll no.-25098

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 {

CSE DEPARTMENT 2023-24


Roll no.-25098

printf("\nEnter position : ");


scanf("%d", &pos);

if (pos == 1) {
position = start;
start = start->next;
if (start != NULL) {
start->prev = NULL;
}
free(position);
return;
}

while (i < pos - 1) {


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

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

CSE DEPARTMENT 2023-24


Roll no.-25098

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:
exit(1);
break;
default: printf("Incorrect Choice. Try
Again \n"); continue;
}}
return
0;
}
OUTPUT

CSE DEPARTMENT 2023-24


Roll no.-25098

CSE DEPARTMENT 2023-24


Roll no.-25098

Program 3
AIM: Write a Program to implement Doubly Circular Linked List with all operations.
Source Code:
#include<stdio.h>
#include<stdlib.h>

// structure of the node


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

// global declaration of head node


struct node* head = NULL;

// 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;

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


{ printf("\n------ Circular Doubly 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");

CSE DEPARTMENT 2023-24


Roll no.-25098

printf("\n8. Print list from end");


printf("\n9. Search a node data");
printf("\n10. Update a node data");
printf("\n11. Sort the list"); printf("\
n12. Exit");

printf("\nEnter your choice: ");


scanf("%d", &user_choice);

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

case 2: printf("\nInserting a node at


end"); data = get_data();
insert_end(data);
break;

case 3: printf("\nInserting a node at the given


position"); data = get_data(); position =
get_position(); insert_mid(position, data);
break;

case 4: printf("\nDeleting a node from


beginning\n"); delete_begin();
break;

case 5: printf("\nDeleting a node from


end\n"); delete_end(); break;

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

case 7: printf("\nPrinting the list from


beginning\n\n"); display(); break;

case 8: printf("\nPrinting the list from


end\n\n");
if (head == NULL)
{ printf("\n\tList is Empty!\n");
} else
{ display_reverse(head);

CSE DEPARTMENT 2023-24


Roll no.-25098

}
break;

case 9: printf("\nSearching the node


data"); data = get_data(); if
(search(data) == 1) {
printf("\n\tNode Found\n");
} else { printf("\n\tNode Not
Found\n");
}
break;

case 10: printf("\nUpdating the node


data"); data = get_data(); position
= get_position(); update(position,
data); break;

case 11: sort(); printf("\nList


was sorted\n"); break;
case 12: printf("\nProgram was
terminated\n\n"); return 0;

default:
printf("\n\tInvalid Choice\n");
}

printf("\nDo you want to continue? (Y/N) : ");


fflush(stdin); scanf(" %c",
&user_active);
}

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 =

CSE DEPARTMENT 2023-24


Roll no.-25098

NULL; new_node->prev
= NULL;

return new_node;
}

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


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

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;
}
}

// inserts a new node at the end


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

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;

CSE DEPARTMENT 2023-24


Roll no.-25098

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;

while (++i <= position)


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

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;
}

struct node* temp = head; head-


>prev->next = head->next; head-

CSE DEPARTMENT 2023-24


Roll no.-25098

>next->prev = head->prev; head =


head->next;

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;
}

CSE DEPARTMENT 2023-24


Roll no.-25098

} int search(int
key)
{ if (head == NULL) {
printf("\n Not Found \n");
return 0;
}

struct node* temp = head;


do
{ if (temp->data == key) {
return 1; } temp
= temp->next;
} while (temp != head);

return 0;
}

// updates the data of the given node position


void update(int position, int new_value)
{ if (head == NULL) {
printf("\n List is Empty \n");
return;
} else if (position <= 0 || position > list_size())
{ printf("\nInvalid position\n"); return;
}

struct node* temp = head;


int i = 0;

while (++i < position) {


temp = temp->next;
}
temp->data = new_value;
}
// sorts the linked list data using insertion sort
void sort()
{ if (head == NULL) {
printf("\nList is Empty\
n"); return; } struct node*
temp1 = head; struct node*
temp2 = head;
int key = 0, value;

do {
temp2 = temp1->next;

CSE DEPARTMENT 2023-24


Roll no.-25098

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 the list


void display()
{ if (head == NULL) {
printf("\nList is empty!\n");
return;
}

struct node* temp = head;


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

// display the list from end to start void


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

display_reverse(temp->next);
printf("%d ", temp->data);
}

// calculate the size of the list


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

CSE DEPARTMENT 2023-24


Roll no.-25098

struct node* temp = head;


int count = 0;

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

CSE DEPARTMENT 2023-24


Roll no.-25098

CSE DEPARTMENT 2023-24


Roll no.-25098

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);

printf("Enter the age:\n");


scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);

printf("Enter the salary:\n");


scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n",
salary);
fclose(fptr);
}
OUTPUT

CSE DEPARTMENT 2023-24


Roll no.-25098

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;

void create(); void


inorder(struct Node *); void
preorder(struct Node *); void
postorder(struct Node *);
struct Node *getNode();
void insert(struct Node *,struct Node *);
void main()
{ int choice; char
ch; do
{ printf("\n1. Create a tree"); printf("\n2.
Inorder Traversing"); printf("\n3.
PreOrder Traversing"); printf("\n4.
PostOrder Traversing"); printf("\n
Enter your Choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
create(); break;
case 2:
inorder(root); break;
case 3:
preorder(root); break;
case 4:
postorder(root)
; break; default:
printf("Sorry Bad Choice");
}
printf("Do you want to continue (y/n)");
ch=getche();

CSE DEPARTMENT 2023-24


Roll no.-25098

}while(ch=='y'||ch=='Y');
}

void inorder(struct Node *ptr)


{ if(ptr!=NULL)
{ inorder(ptr->left); printf("|
%d|",ptr->info);
inorder(ptr->right);
}
}
void preorder(struct Node *ptr)
{

}
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()

CSE DEPARTMENT 2023-24


Roll no.-25098

{ struct Node *ptr; ptr=getNode();


printf("Enter the Data in
Node");
scanf("%d",&ptr->info);

if(root==NULL)
{
root=ptr;
} else
{
insert(root,ptr);
}
}

CSE DEPARTMENT 2023-24


Roll no.-25098

OUTPUT

Program 6

CSE DEPARTMENT 2023-24


Roll no.-25098

AIM: Write a Program to read the contents of a file.


Source Code:

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

FILE * fPtr;

char ch;

fPtr = fopen("test.txt", "r");

if(fPtr == NULL)
{ printf("Unable to open file.\
n");
printf("Please check whether file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}

printf("File opened successfully. Reading file contents character by character. \n\


n");

do
{ ch = fgetc(fPtr);
putchar(ch); }
while(ch != EOF);
fclose(fPtr);

return 0;
}
OUTPUT

CSE DEPARTMENT 2023-24


Roll no.-25098

CSE DEPARTMENT 2023-24


Roll no.-25098

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

CSE DEPARTMENT 2023-24

You might also like