Dsa
C Code For Liked List and its Operations Using Arrays in Data Structure
#include<stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node *next;
};
void Traversal(struct Node *ptr){
while(ptr != NULL){
printf("%d -> ",ptr->data);
ptr = ptr->next;
}
}
// insert at begining
struct Node * insertAtFirst(struct Node *head,int data){
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->next = head;
ptr->data = data;
return ptr;
}
// insert at end
struct Node * insertAtEnd(struct Node *head,int data){
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
struct Node *p = head;
ptr->data = data;
while(p->next != NULL){
p = p->next;
}
p->next = ptr;
ptr->next = NULL;
return head;
}
// insert in between
struct Node * InsertAtIndex(struct Node *head,int data,int index){
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
struct Node *p = head;
int i = 0;
while(i != index-1){
p = p->next;
i++;
}
ptr->data = data;
ptr->next = p->next;
return head;
}
// insert in between
struct Node * InsertAfterNode(struct Node *prevnode,struct Node *head,int data){
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->data = data;
ptr->next = prevnode->next;
prevnode->next = ptr;
return head;
}
// delete the first node
struct Node *DeleteFirstNode(struct Node *head){
struct Node *ptr = head;
head = head->next;
free(ptr);
return head;
}
// Deleting the element at a given index
struct Node *DeleteAtIndex(struct Node *head,int index){
struct Node *p = head;
struct Node *q = head->next;
for(int i=0; i<index-1; i++){
p = p->next;
q = q->next;
}
p->next = q->next;
free(q);
return head;
}
// delete last element
struct Node *DeleteAtLast(struct Node *head){
struct Node *p = head;
struct Node *q = head->next;
while(q->next != NULL){
p = p->next;
q = q->next;
}
p->next = NULL;
free(q);
return head;
}
int main(){
struct Node *head;
struct Node *secondnode;
struct Node *thirdnode;
head = (struct Node *)malloc(sizeof(struct Node));
secondnode = (struct Node *)malloc(sizeof(struct Node));
thirdnode = (struct Node *)malloc(sizeof(struct Node));
head->data = 10;
head->next = secondnode;
secondnode->data = 50;
secondnode->next = thirdnode;
thirdnode->data = 100;
thirdnode->next = NULL;
printf("data:- ");
Traversal(head);
// head = DeleteFirstNode(head);
printf("delete at last");
head = DeleteAtLast(head);
Traversal(head);
// printf("insert at first:- ");
// head = insertAtFirst(head,56);
// Traversal(head);
// printf("insert in begining:- ");
// head = InsertAtIndex(head,200,1);
// Traversal(head);
// printf("insert at end:- ");
// head = insertAtEnd(head,60);
// Traversal(head);
// printf("insert after node:- ");
// head = InsertAfterNode(secondnode,head,90);
// Traversal(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10 // Define queue size
// Define queue structure
struct queue {
int size;
int front;
int rear;
int items[MAX_SIZE];
};
// Function to initialize the queue
void MakeEmptyQueue(struct queue *q) {
q->front = -1;
q->rear = -1;
printf("Queue is initialized.\n");
}
// Check if the queue is full
int IsFull(struct queue *q) {
return (q->rear == q->size - 1);
}
// Check if the queue is empty
int IsEmpty(struct queue *q) {
return (q->front == q->rear);
}
// Function to add items to the queue
void additems(struct queue *q, int value) {
if (IsFull(q)) {
printf("Queue is full, cannot add item %d.\n", value);
} else {
q->rear = q->rear + 1;
q->items[q->rear] = value;
printf("Added %d to queue.\n", value);
}
}
// Function to remove items from the queue
void deleteitems(struct queue *q) {
if (IsEmpty(q)) {
printf("Queue is empty, cannot delete items.\n");
} else {
q->front = q->front + 1;
printf("Deleted item: %d\n", q->items[q->front]);
}
}
// Function to traverse and display queue items
void TraversingItem(struct queue *q) {
if (IsEmpty(q)) {
printf("Queue is empty.\n");
} else {
printf("Queue elements: ");
for (int i = q->front + 1; i <= q->rear; i++) {
printf("%d ", q->items[i]);
}
printf("\n");
}
}
int main() {
struct queue q;
q.size = MAX_SIZE;
MakeEmptyQueue(&q);
additems(&q, 10);
additems(&q, 20);
additems(&q, 30);
TraversingItem(&q);
deleteitems(&q);
TraversingItem(&q);
return 0;
}
Comments
Post a Comment