Professional Documents
Culture Documents
Practical - 07 OBJECTIVE - Write A C Program To Implement Operations On Singly
Practical - 07 OBJECTIVE - Write A C Program To Implement Operations On Singly
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);
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
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
PRACTICAL – 08
PROGRAM –
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
};
int main(){
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);
display(&f);
int num;
scanf("%d",&num);
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 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;
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 –
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
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>
printf("%d\n", s->data[i]);
}
}
}
int main() {
Stack s;
initialize(&s);
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
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>
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>
{
return front == NULL;
}
{
*rear = NULL;
}
int dequeued = temp->data;
free(temp);
return dequeued;
}
int main()
{
Node *front = NULL;
Node *rear = NULL;
NAME: SUMAN
COURSE: BCA PAGE NO. 47
SEC: C
SEMESTER: 2
ROLL NO: 2392304
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>
int main() {
Queue q;
initialize(&q);
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