0% found this document useful (0 votes)
5 views38 pages

Practical - 07 OBJECTIVE - Write A C Program To Implement Operations On Singly

The document describes implementing various operations on doubly linked lists like insert at front, insert after, insert at end, and delete a node. It includes functions for these operations and a main function that tests them by adding some nodes to the list, displaying it, deleting a node and displaying it again.

Uploaded by

rocknau123
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)
5 views38 pages

Practical - 07 OBJECTIVE - Write A C Program To Implement Operations On Singly

The document describes implementing various operations on doubly linked lists like insert at front, insert after, insert at end, and delete a node. It includes functions for these operations and a main function that tests them by adding some nodes to the list, displaying it, deleting a node and displaying it again.

Uploaded by

rocknau123
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/ 38

NAME: SUMAN

PAGE NO. 18
COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392304

PRACTICAL – 07
OBJECTIVE -Write a C program to Implement operations on Singly
linked list.
PROGRAM –
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void append(struct node**);
void addatbeg(struct node**);
void addatend(struct node**);
void addafter(struct node**);
void display(struct node **);
int count(struct node**);
void beg_delete(struct node **);
void end_delete(struct node **);
void loc_delete(struct node **);

struct node{
int data;
struct node *next;
};
struct node *head=NULL;
int main(){
int loc;
append(&head);
addatbeg(&head);
NAME: SUMAN
COURSE: BCA PAGE NO. 19
SEC: C
SEMESTER: 2
ROLL NO: 2392304

append(&head);
addatend(&head);
addatbeg(&head);
addafter(&head);
addatend(&head);

printf("Number of nodes = %d\n",count(&head));


display (&head);
beg_delete(&head);
end_delete(&head);
loc_delete(&head);
printf("after deletion at beginning,end, and specified position
data :\n");

display (&head);
}
void append(struct node **q){
int num;
printf("enter data you want to insert :");

scanf("%d",&num);
if(*q==NULL){
struct node *temp=(struct node *)malloc(sizeof(struct
node));
temp->data=num;
temp->next=*q;
*q=temp;
}
Else{
NAME: SUMAN
COURSE: BCA
SEC: C PAGE NO. 20
SEMESTER: 2
ROLL NO: 2392304

struct node *p=*q;


while(p->next!=NULL){
p=p->next;
}
struct node *temp=(struct node*)malloc(sizeof(struct
node));
temp->data=num;
temp->next=p->next;
p->next=temp;
}
}
void addatbeg(struct node **q){
struct node *temp;
int num;
printf("enter data you want to add at beginning :");
scanf("%d",&num);
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
temp->next=*q;
*q=temp;
}
void addatend(struct node **q){
struct node *r,*p=*q;
int num;
printf("enter data you want to add at end : ");
scanf("%d",&num);
r=(struct node*)malloc(sizeof(struct node));
r->data=num;
while(p->next!=NULL){
NAME: SUMAN
COURSE: BCA PAGE NO. 21
SEC: C
SEMESTER: 2
ROLL NO: 2392304

p=p->next;
}
r->next=NULL;
p->next=r;
}
void addafter(struct node **q){
struct node *temp,*r;
temp=*q;
int num,loc;
printf("enter location for insertion of node : ");

scanf("%d",&loc);
printf("enter data you want to add at specific position : ");
scanf("%d",&num);
for(int i=1;i<loc;i++){
temp=temp->next;
if(temp==NULL){
printf("There are less no. of nodes\n");
return;
}
}
r=(struct node*)malloc(sizeof(struct node));
r->data=num;
r->next=temp->next;
temp->next=r;
}
void display(struct node **q){
struct node *ptr=head;
printf("data elements are : \n");
NAME: SUMAN
COURSE: BCA PAGE NO. 22
SEC: C
SEMESTER: 2
ROLL NO: 2392304

while(ptr->next!=NULL){
printf("%d\n",ptr->data);
ptr=ptr->next;
}
printf("%d\n",ptr->data);
}
int count(struct node **q){
int c=1;
struct node *d=head;
while(d->next!=NULL){
c++;
d=d->next;
}
return c;
}
void beg_delete(struct node **q){
struct node *temp=*q;
head=temp->next;
delete(temp);
}
void end_delete(struct node **q){
struct node *old,*temp=*q;
while(temp->next!=NULL){
old=temp;
temp=temp->next;
}
old->next=NULL;
delete(temp);
NAME: SUMAN PAGE NO. 23
COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392304

void loc_delete(struct node **q){


struct node *old,*temp=*q;
int loc;
printf("enter location of the node in which you want to delete
node : ");
scanf("%d",&loc);
for(int i=1;i<=loc;i++){
old=temp;
temp=temp->next;
if(temp==NULL){
printf("less number of nodes");
return;
}
}
old->next=temp->next;
delete(temp);
}
OUTPUT –
enter data you want to insert :45
enter data you want to add at beginning :2
enter data you want to insert :67
enter data you want to add at end : 8
enter data you want to add at beginning :35
enter location for insertion of node : 3
enter data you want to add at specific position : 57
enter data you want to add at end : 82
Number of nodes = 7
NAME: SUMAN
PAGE NO. 24
COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392304

data elements are :


35
2
45
57
67
8
82
enter location of the node in which you want to delete node : 2
after deletion at beginning,end, and specified position data :
data elements are :
2
45
67
8
NAME: SUMAN
COURSE: BCA PAGE NO. 25
SEC: C
SEMESTER: 2
ROLL NO: 2392304

PRACTICAL – 08

OBJECTIVE – Implement all operations on queue using circular linked


list.

PROGRAM –

#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node *link;

};

void add_cirq(struct node**,struct node**);

int del_cirq(struct node**,struct node**);

void display(struct node**);

int main(){

struct node *f,*r;

f=r=NULL;
NAME: SUMAN
COURSE: BCA PAGE NO. 26
SEC: C
SEMESTER: 2
ROLL NO: 2392304

int i;

add_cirq(&f,&r);

add_cirq(&f,&r);

add_cirq(&f,&r);

i=del_cirq(&f,&r);

printf("item deleted =%d\n",i);

display(&f);

void add_cirq(struct node**f,struct node**r){

struct node *q;

int num;

printf("enter number to add at link : ");

scanf("%d",&num);

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

q->data=num;

if(*f==NULL){

*f=q;

}
NAME: SUMAN
COURSE: BCA PAGE NO. 27
SEC: C
SEMESTER: 2
ROLL NO: 2392304

else{

(*r)->link=q;

*r=q;

(*r)->link=*f;

int del_cirq(struct node**f,struct node**r){

struct node *q;

int item;

if(*f==NULL){

printf("queue is empty");

else{

if(*f==*r){

item=(*f)->data;

free(*f);

*f=NULL;
NAME: SUMAN
COURSE: BCA PAGE NO. 28
SEC: C
SEMESTER: 2
ROLL NO: 2392304

*r=NULL;

else{

q=*f;

item=q->data;

*f=(*f)->link;

(*r)->link=*f;

free(q);

return (item);

return NULL;

void display(struct node **f){

struct node *q=*f,*p=NULL;

printf("data elements are :\n");

while(q!=p){
NAME: SUMAN
COURSE: BCA PAGE NO. 29
SEC: C
SEMESTER: 2
ROLL NO: 2392304

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

q=q->link;

p=*f;

OUTPUT –

enter number to add at link : 675

enter number to add at link : 46

enter number to add at link : 876

item deleted =675

data elements are :

46

876
NAME: SUMAN
COURSE: BCA PAGE NO. 30
SEC: C
SEMESTER: 2
ROLL NO: 2392304

PRACTICAL – 9
OBJECTIVE: Implement all the operations on doubly linked list.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
Struct Node* next;
Struct Node* prev;
};
Void insertFront(struct Node** head, int data) {
Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = (*head);
newNode->prev = NULL;
if ((*head) != NULL)
(*head)->prev = newNode;
(*head) = newNode;
}
Void insertAfter(struct Node* prev_node, int data) {
If (prev_node == NULL) {
Printf(“previous node cannot be null”);
Return;
}
Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = prev_node->next;
prev_node->next = newNode;
newNode->prev = prev_node;
NAME: SUMAN
COURSE: BCA PAGE NO. 31
SEC: C
SEMESTER: 2
ROLL NO: 2392304

if (newNode->next != NULL)
newNode->next->prev = newNode;
}
Void insertEnd(struct Node** head, int data) {
Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
struct Node* temp = *head;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
Return;
}
While (temp->next != NULL)
Temp = temp->next;
Temp->next = newNode;
newNode->prev = temp;
}
Void deleteNode(struct Node** head, struct Node* del_node) {
If (*head == NULL || del_node == NULL)
Return;
If (*head == del_node)
*head = del_node->next;
If (del_node->next != NULL)
Del_node->next->prev = del_node->prev;
If (del_node->prev != NULL)
Del_node->prev->next = del_node->next;
Free(del_node);
}
NAME: SUMAN
COURSE: BCA PAGE NO. 32
SEC: C
SEMESTER: 2
ROLL NO: 2392304

Void displayList(struct Node* node) {


Struct Node* last;
While (node != NULL) {
Printf(“%d->”, node->data);
Last = node;
Node = node->next;
}
If (node == NULL)
Printf(“NULL\n”);
}
Int main() {
Struct Node* head = NULL;
insertEnd(&head, 5);
insertFront(&head, 1);
insertFront(&head, 6);
insertEnd(&head, 9);
insertAfter(head, 11);
insertAfter(head->next, 15);
displayList(head);
deleteNode(&head, head->next->next->next->next->next);
displayList(head);
}

OUTPUT –
6->11->15->1->5->9->NULL
6->11->15->1->5->NULL
NAME: SUMAN
COURSE: BCA PAGE NO. 33
SEC: C
SEMESTER: 2
ROLL NO: 2392304

PRACTICAL – 10
OBJECTIVE: C program that implements a stack using an array.
PROGRAM
#include <stdio.h>
#include <stdlib.h>

#define MAX 100 // Maximum size of the stack

typedef struct Stack {


int data[MAX];
int top;
} Stack;

// Function to initialize the stack


void initialize(Stack *s) {
s->top = -1;
}

// Function to check if the stack is empty


int isEmpty(Stack *s) {
return s->top == -1;
}

// Function to check if the stack is full


int isFull(Stack *s) {
return s->top == MAX - 1;
NAME: SUMAN
COURSE: BCA PAGE NO. 34
SEC: C
SEMESTER: 2
ROLL NO: 2392304

// Function to push an element onto the stack


void push(Stack *s, int value) {
if (isFull(s)) {
printf("Stack overflow!\n");
} else {
s->data[++(s->top)] = value;
printf("%d pushed to stack\n", value);
}
}

// Function to pop an element from the stack


int pop(Stack *s) {
if (isEmpty(s)) {
printf("Stack underflow!\n");
return -1;
} else {
return s->data[(s->top)--];
}
}

// Function to display the elements of the stack


void display(Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty\n");
} else {
printf("Stack elements are:\n");
for (int i = s->top; i >= 0; i--) {
NAME: SUMAN
COURSE: BCA PAGE NO. 35
SEC: C
SEMESTER: 2
ROLL NO: 2392304

printf("%d\n", s->data[i]);
}
}
}

int main() {
Stack s;
initialize(&s);

int choice, value;


while (1) {
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to push: ");
scanf("%d", &value);
push(&s, value);
break;
case 2:
value = pop(&s);
if (value != -1) {
printf("%d popped from stack\n", value);
}
break;
case 3:
display(&s);
break;
NAME: SUMAN
COURSE: BCA PAGE NO. 36
SEC: C
SEMESTER: 2
ROLL NO: 2392304

case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}

return 0;
}

OUTPUT
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to push: 10
10 pushed to stack

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to push: 20
20 pushed to stack

1. Push
NAME: SUMAN
COURSE: BCA PAGE NO. 37
SEC: C
SEMESTER: 2
ROLL NO: 2392304

2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to push: 30
30 pushed to stack

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack elements are:
30
20
10

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
30 popped from stack

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
NAME: SUMAN
COURSE: BCA PAGE NO. 38
SEC: C
SEMESTER: 2
ROLL NO: 2392304

Stack elements are:


20
10

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 4
NAME: SUMAN
PAGE NO. 39
COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392304

PRACTICAL – 10(B)
OBJECTIVE: C program that implements a stack using a linked list.
PROGRAM
#include <stdio.h>
#include <stdlib.h>

// Node structure definition


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

// Function to create a new node


Node* createNode(int data) {
Node *newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation error\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to check if the stack is empty


int isEmpty(Node *top) {
return top == NULL;
}
NAME: SUMAN
PAGE NO. 40
COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392304

// Function to push an element onto the stack


void push(Node **top, int data) {
Node *newNode = createNode(data);
newNode->next = *top;
*top = newNode;
printf("%d pushed to stack\n", data);
}

// Function to pop an element from the stack


int pop(Node **top) {
if (isEmpty(*top)) {
printf("Stack underflow!\n");
return -1;
}
Node *temp = *top;
*top = (*top)->next;
int popped = temp->data;
free(temp);
return popped;
}

// Function to display the elements of the stack


void display(Node *top) {
if (isEmpty(top)) {
printf("Stack is empty\n");
return;
}
Node *temp = top;
NAME: SUMAN
COURSE: BCA PAGE NO. 41
SEC: C
SEMESTER: 2
ROLL NO: 2392304

while (temp != NULL) {


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

int main() {
Node *top = NULL;
int choice, value;

while (1) {
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the value to push: ");
scanf("%d", &value);
push(&top, value);
break;
case 2:
value = pop(&top);
if (value != -1) {
printf("%d popped from stack\n", value);
}
break;
case 3:
NAME: SUMAN
COURSE: BCA PAGE NO. 42
SEC: C
SEMESTER: 2
ROLL NO: 2392304

display(top);
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}

return 0;
}
OUTPUT
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to push: 10
10 pushed to stack

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to push: 20
20 pushed to stack

1. Push
NAME: SUMAN
COURSE: BCA PAGE NO. 43
SEC: C
SEMESTER: 2
ROLL NO: 2392304

2. Pop
3. Display
4. Exit
Enter your choice: 3
20 -> 10 -> NULL

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
20 popped from stack

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
10 -> NULL

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 4
NAME: SUMAN
COURSE: BCA PAGE NO. 44
SEC: C
SEMESTER: 2
ROLL NO: 2392304

PRACTICAL – 11(a)
OBJECTIVE: C program that implements QUEUE as a LINK LIST.
PROGRAM
#include <stdio.h>
#include <stdlib.h>

// Node structure definition


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

// Function to create a new node


Node* createNode(int data)
{
Node *newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation error\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to check if the queue is empty


int isEmpty(Node *front)
NAME: SUMAN
PAGE NO. 45
COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392304

{
return front == NULL;
}

// Function to enqueue an element to the queue


void enqueue(Node **front, Node **rear, int data)
{
Node *newNode = createNode(data);
if (*rear == NULL) {
*front = *rear = newNode;
}
else
{
(*rear)->next = newNode;
*rear = newNode;
}
printf("%d enqueued to queue\n", data);
}

// Function to dequeue an element from the queue


int dequeue(Node **front, Node **rear)
{
if (isEmpty(*front)) {
printf("Queue underflow!\n");
return -1;
}
Node *temp = *front;
*front = (*front)->next;
if (*front == NULL)
NAME: SUMAN
PAGE NO. 46
COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392304

{
*rear = NULL;
}
int dequeued = temp->data;
free(temp);
return dequeued;
}

// Function to display the elements of the queue


void display(Node *front)
{
if (isEmpty(front))
{
printf("Queue is empty\n");
return;
}
Node *temp = front;
while (temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main()
{
Node *front = NULL;
Node *rear = NULL;
NAME: SUMAN
COURSE: BCA PAGE NO. 47
SEC: C
SEMESTER: 2
ROLL NO: 2392304

int choice, value;

while (1)
{
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice)
{
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(&front, &rear, value);
break;
case 2:
value = dequeue(&front, &rear);
if (value != -1) {
printf("%d dequeued from queue\n", value);
}
break;
case 3:
display(front);
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
NAME: SUMAN PAGE NO. 48
COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392304

return 0;
}
OUTPUT
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the value to enqueue: 10
10 enqueued to queue

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the value to enqueue: 20
20 enqueued to queue

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
10 -> 20 -> NULL

1. Enqueue
NAME: SUMAN PAGE NO. 49
COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392304

2. Dequeue
3. Display
4. Exit
Enter your choice: 2
10 dequeued from queue

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
20 -> NULL

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
NAME: SUMAN
COURSE: BCA
PAGE NO. 50
SEC: C
SEMESTER: 2
ROLL NO: 2392304

PRACTICAL – 11(B)
OBJECTIVE: C program that implements QUEUE as an ARRAY.
PROGRAM
#include <stdio.h>
#include <stdlib.h>

#define MAX 100 // Maximum size of the queue

typedef struct Queue {


int data[MAX];
int front;
int rear;
} Queue;

// Function to initialize the queue


void initialize(Queue *q) {
q->front = -1;
q->rear = -1;
}

// Function to check if the queue is empty


int isEmpty(Queue *q) {
return q->front == -1;
}

// Function to check if the queue is full


int isFull(Queue *q) {
return q->rear == MAX - 1;
NAME: SUMAN PAGE NO. 51
COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392304

// Function to enqueue an element to the queue


void enqueue(Queue *q, int value) {
if (isFull(q)) {
printf("Queue overflow!\n");
return;
}
if (isEmpty(q)) {
q->front = 0;
}
q->data[++(q->rear)] = value;
printf("%d enqueued to queue\n", value);
}

// Function to dequeue an element from the queue


int dequeue(Queue *q) {
if (isEmpty(q)) {
printf("Queue underflow!\n");
return -1;
}
int dequeued = q->data[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1;
} else {
q->front++;
}
return dequeued;
}
NAME: SUMAN
COURSE: BCA PAGE NO. 52
SEC: C
SEMESTER: 2
ROLL NO: 2392304

// Function to display the elements of the queue


void display(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return;
}
for (int i = q->front; i <= q->rear; i++) {
printf("%d -> ", q->data[i]);
}
printf("NULL\n");
}

int main() {
Queue q;
initialize(&q);

int choice, value;


while (1) {
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
break;
NAME: SUMAN
COURSE: BCA PAGE NO. 53
SEC: C
SEMESTER: 2
ROLL NO: 2392304

case 2:
value = dequeue(&q);
if (value != -1) {
printf("%d dequeued from queue\n", value);
}
break;
case 3:
display(&q);
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}

return 0;
}

OUTPUT
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the value to enqueue: 10
10 enqueued to queue

1. Enqueue
NAME: SUMAN PAGE NO. 54
COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392304

2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the value to enqueue: 20
20 enqueued to queue

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
10 -> 20 -> NULL

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
10 dequeued from queue

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
20 -> NULL

1. Enqueue
NAME: SUMAN
PAGE NO. 55
COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392304

2. Dequeue
3. Display
4. Exit
Enter your choice: 4

You might also like