dsfile

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

CIRCULAR DOUBLY LINKED LIST:

To Create and Print All the Nodes


#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
struct node * nextptr;
}*stnode;
void ClListcreation(int n)
{ int i, num;
struct node *preptr, *newnode;
if(n >= 1)
{ stnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{ newnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node %d : ", i);
scanf("%d", &num);
newnode->num = num;
newnode->nextptr = NULL;
preptr->nextptr = newnode;
preptr = newnode;
}
preptr->nextptr = stnode;
}}
void displayClList()
{ struct node *tmp;
int n = 1;
if(stnode == NULL){
printf(" No data found in the List yet.");
}
else{
tmp = stnode;
printf("\n\n Data entered in the list are :\n");
do {printf(" %d,", tmp->num);
tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}}
int main()
{ int n;
stnode = NULL;
printf("\n\n Circular Linked List : Create and display a circular linked list
:\n");
printf("-----------------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
ClListcreation(n);
displayClList();
return 0;
}
OUTPUT:
Circular Linked List : Create and display a circular linked list :
-----------------------------------------------------------------------
Input the number of nodes : 4
Input data for node 1 : 2
Input data for node 2 : 4
Input data for node 3 : 8
Input data for node 4 : 16

Data entered in the list are :


2, 4, 8, 16
CIRCULAR DOUBLY LINKED LIST:
Insertion At First, At Last , At Specified Position
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Node* createNode(int x) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->next = newNode->prev = NULL;
return newNode;
}
struct Node* insertAtBeginning(struct Node* head, int newData) {
struct Node* newNode = createNode(newData);
if (!head) {
newNode->next = newNode->prev = newNode;
head = newNode;
} else {
struct Node* last = head->prev;
newNode->next = head;
newNode->prev = last;
head->prev = newNode;
last->next = newNode;
head = newNode;
}
return head;
}
struct Node* insertAtEnd(struct Node* head, int newData) {
struct Node* newNode = createNode(newData);
if (!head) {
newNode->next = newNode->prev = newNode;
head = newNode;
} else {
struct Node* last = head->prev;
newNode->next = head;
newNode->prev = last;
last->next = newNode;
head->prev = newNode;
}
return head;
}
struct Node* addNode(struct Node* head, int pos, int newData) {
struct Node* newNode = createNode(newData);
struct Node* curr = head;
int i;
for ( i = 1; i < pos - 1; i++) {
curr = curr->next;
if (curr == head) {
printf("Position out of range!\n");
return head;
}
}
newNode->next = curr->next;
newNode->prev = curr;
if (curr->next != NULL) {
curr->next->prev = newNode;
}
curr->next = newNode;
return head;
}
void printList(struct Node* head) {
if (!head) return;
struct Node* curr = head;
do {printf("%d ", curr->data);
curr = curr->next;
} while (curr != head);
printf("\n");
}
int main(){
struct Node* head = createNode(10);
head->next = createNode(20);
head->next->prev = head;
head->next->next = createNode(30);
head->next->next->prev = head->next;
head->next->next->next = head;
head->prev = head->next->next;

printf("List before insertion:\n");


printList(head);

head = insertAtBeginning(head, 5);


head = insertAtEnd(head, 50);
head = addNode(head, 5 , 40);
printf("List after insertion:\n");
printList(head);
return 0;
}
OUTPUT:
List before insertion:
10 20 30
List after insertion:
5 10 20 30 40 50
CIRCULAR DOUBLY LINKED LIST:
Deletion At First, At Last, At Specified Position
#include <stdio.h>
struct Node {
int data;
struct Node *prev;
struct Node *next;
};
struct Node *createNode(int data) {
struct Node* newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
struct Node *delHead(struct Node *head) {
if (head == NULL)
return NULL;
struct Node *temp = head;
head = head->next;
if (head != NULL)
head->prev = NULL;
free(temp);
return head;
}
struct Node* delPos(struct Node* head, int pos) {
if (head == NULL) {
return head;} // If the list is empty
struct Node* curr = head;
int i;
for (i = 1; curr != NULL && i < pos; ++i) {
curr = curr->next;} // Traverse to the node at the given position
if (curr == NULL) {
return head;} // If the position is out of range
if (curr->prev != NULL) {
curr->prev->next = curr->next;} // Update the previous node's next pointer
if (curr->next != NULL) {
curr->next->prev = curr->prev;} // Update the next node's prev pointer
if (head == curr) {
head = curr->next;} // If the node to be deleted is the head node
free(curr);
return head;
}
struct Node* delLast(struct Node *head) {
if (head == NULL)
return NULL;
if (head->next == NULL) {
free(head);
return NULL;
}
struct Node *curr = head;
while (curr->next != NULL)
curr = curr->next;
curr->prev->next = NULL;
free(curr);
return head;
}
void printList(struct Node *head) {
struct Node *curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
int main() {
struct Node *head = createNode(10);
head->next = createNode(20);
head->next->prev = head;
head->next->next = createNode(30);
head->next->next->prev = head->next;
head->next->next->next = createNode(40);
head->next->next->next->prev = head->next->next;
head->next->next->next->next = createNode(50);
head->next->next->next->next->prev = head->next->next->next;
printf("List before deletion:\n");
printList(head);
head = delHead(head);
head = delPos(head, 4);
head = delLast(head);
printf("List after deletion:\n");
printList(head);
return 0;
}
OUTPUT:
List before deletion:
10 20 30 40 50
List after deletion:
20 30
STACK
Implementation Using Arrays
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 100
typedef struct {
int arr[MAX_SIZE];
int top;
} Stack;
void initialize(Stack *stack) {
stack->top = -1;
}
bool isEmpty(Stack *stack) {
return stack->top == -1;
}
bool isFull(Stack *stack) {
return stack->top == MAX_SIZE - 1;
}
void push(Stack *stack, int value) {
if (isFull(stack)) {
printf("Stack Overflow\n");
return; }
stack->arr[++stack->top] = value;
printf("Pushed %d onto the stack\n", value);
}
int pop(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;}
int popped = stack->arr[stack->top];
stack->top--;
printf("Popped %d from the stack\n", popped);
return popped;}
int peek(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
}
return stack->arr[stack->top];
}
void display(Stack *stack) {
int i;
for ( i = stack->top; i > -1; i--) {
printf("%d ", stack->arr[i]);
}
printf("\n\n");
}
int main() {
Stack stack;
initialize(&stack);
push(&stack, 3);
printf("Top element: %d\n", peek(&stack));
push(&stack, 5);
printf("Top element: %d\n", peek(&stack));
push(&stack, 2);
printf("Top element: %d\n", peek(&stack));
push(&stack, 8);
printf("Top element: %d\n", peek(&stack));
isEmpty(&stack);
printf("\n Displaying element of stacks: ");
display(&stack);
while (!isEmpty(&stack)) {
printf("Top element: %d\n", peek(&stack));
pop(&stack);
}
return 0;
}
OUTPUT:
Pushed 3 onto the stack
Top element: 3
Pushed 5 onto the stack
Top element: 5
Pushed 2 onto the stack
Top element: 2
Pushed 8 onto the stack
Top element: 8

Displaying element of stacks: 8 2 5 3

Top element: 8
Popped 8 from the stack
Top element: 2
Popped 2 from the stack
Top element: 5
Popped 5 from the stack
Top element: 3
Popped 3 from the stack
STACK
Implementation Using Linked List
#include<stdio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node *next;
};
struct Node *head = NULL;
void push(int val)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = head;
head = newNode;
}
void pop()
{
struct Node *temp;
if(head == NULL)
printf("Stack is Empty\n");
else
{ printf("Poped element = %d\n", head->data);
temp = head;
head = head->next;
free(temp);
}
printf("\n");
}
void display()
{ struct Node *temp = head;
while(temp != NULL)
{ printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main()
{ push(10);
push(20);
push(30);
push(40);
printf("Linked List\n");
display();
pop();
printf("After the pop, the new linked list\n");
display();
pop();
printf("After the pop, the new linked list\n");
display();
return 0;
}
OUTPUT:
Linked List
40->30->20->10->NULL
Poped element = 40

After the pop, the new linked list


30->20->10->NULL
Poped element = 30

After the pop, the new linked list


20->10->NULL

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy