top of page
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#define size 100
int stack[size];
int top=-1;
void push(void);
void pop(void);
void peek_top(void);
void isEmpty(void);
void isFull(void);
void display(void);
void main() {
int c,e;
do{
switch(c)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek_top();
break;
case 4:
isEmpty();
break;
case 5:
isFull();
break;
case 6:
display();
break;
case 7:
printf("\n\n----Exit from program successfully----");
printf("\n--------------Thank you--------------");
exit(0);
}
printf("\n1 : Push\n2 : Pop\n3 : Peek\n4 : isEmpty\n5 : isFull\n6 : Display");
printf("\nEnter your choice : ");
scanf("%d",&c);
}while(c);
}
void push()
{
int v;
printf("\nEnter value : ");
scanf("%d",&v);
if(top==size-1)
{
printf("\nStack is full!!");
}
else
{
top++;
stack[top]=v;
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty");
}
else
{
for(i=0;i<=top;i++)
{
printf("--%d",stack[i]);
}
}
}
void pop()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
top--;
}
}
void peek_top()
{
printf("\nTop : %d",top);
printf("\nTop Value: %d",stack[top]);
}
void isEmpty()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is not empty!!");
}
}
void isFull()
{
if(top==size-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nStack is not full!!");
}
}
0 views0 comments
A doubly circular linked list is a type of linked list where each node has both a "next" and a "previous" pointer, allowing for traversal in both directions. Additionally, the last node in the list points back to the first node, forming a circle.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct DoublyCircularLinkedList {
struct Node* head;
int count;
};
struct Node* create_node(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
node->prev = NULL;
return node;
}
struct DoublyCircularLinkedList* create_list() {
struct DoublyCircularLinkedList* list = (struct DoublyCircularLinkedList*)malloc(sizeof(struct DoublyCircularLinkedList));
list->head = NULL;
list->count = 0;
return list;
}
void append(struct DoublyCircularLinkedList* list, int data) {
struct Node* node = create_node(data);
if (list->head == NULL) {
list->head = node;
node->next = node; // make the node point to itself
node->prev = node;
} else {
node->next = list->head;
node->prev = list->head->prev;
list->head->prev->next = node;
list->head->prev = node;
}
list->count++;
}
int remove_node(struct DoublyCircularLinkedList* list, int data) {
if (list->head == NULL) {
return 0;
}
struct Node* current = list->head;
do {
if (current->data == data) {
if (current == list->head) {
list->head = current->next;
}
current->prev->next = current->next;
current->next->prev = current->prev;
free(current);
list->count--;
return 1;
}
current = current->next;
} while (current != list->head);
return 0;
}
void print_list(struct DoublyCircularLinkedList* list) {
if (list->head == NULL) {
return;
}
struct Node* current = list->head;
do {
printf("%d ", current->data);
current = current->next;
} while (current != list->head);
printf("\n");
}
int main() {
struct DoublyCircularLinkedList* list = create_list();
append(list, 1);
append(list, 2);
append(list, 3);
print_list(list); // output: 1 2 3
remove_node(list, 2);
print_list(list); // output: 1 3
return 0;
}
2 views0 comments
bottom of page