0% found this document useful (0 votes)
18 views24 pages

Answers

The document contains multiple C code snippets for data structures and algorithms, including circular linked lists, stack operations, and checking balanced parentheses. Key functionalities include creating nodes, inserting elements, displaying lists, reversing lists, and checking for balanced expressions. The code demonstrates various operations on circular linked lists and stack implementations with error handling for overflow and underflow conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views24 pages

Answers

The document contains multiple C code snippets for data structures and algorithms, including circular linked lists, stack operations, and checking balanced parentheses. Key functionalities include creating nodes, inserting elements, displaying lists, reversing lists, and checking for balanced expressions. The code demonstrates various operations on circular linked lists and stack implementations with error handling for overflow and underflow conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

#include <stdio.

h>
#include <stdlib.h>

struct Node { int


data; struct
Node* next;

struct Node* createNode(int data)


{ struct Node* newNode = (struct
Node*)malloc(sizeof(struct
Node)); newNode->data = data;
newNode->next = NULL; return
newNode;
void insertEnd(struct Node** head,
int data) { struct Node* newNode =
createNode(data); if (*head == NULL)
{ *head = newNode; newNode->next
= *head;
} else { struct Node* temp =
*head; while (temp->next !=
*head) { temp = temp-
>next;

temp->next = newNode;
newNode->next = *head;

int calculateLength(struct Node*


head) { if (head NULL) { return 0;
int count = 1 struct Node*
temp = head; while (temp-
>next != head) { count++;
temp = temp->next;
return count;

void displayList(struct Node* head)


{ if (head == NULL) { printf("List
is empty.\n"); return;

struct Node* temp = head; do


{ printf("%d -> temp->data);
temp = temp->next;
} while (temp != head);
printf("(head)\n");

// Main function int main() {


struct Node* head = NULL;
insertEnd(&head,
1);
insertEnd(&head,
2);
insertEnd(&head,
3);
insertEnd(&head,
4);
insertEnd(&head,
5);

displayList(head);

int len = calculateLength(head);


printf(" circular linked list length: %d\
n", len);
return 0;
2.

#include <stdio.h>
#include <stdlib.h>

typedef struct node


SCI; struct node { int
data;
SCI* addr;

SCI* create_node_scl(int d) { scl


*new_node = malloc(sizeof(scl));
new node->data = d; new node-
>addr = NULL; return new node;
void show(scl *head) { if
(head == NULL)
{ printf("List is empty.\
n"); return;
scl *ptr = head; printf("Single
circular linked list:\n"); do
{ printf("%d -> ptr->data); ptr = ptr-
>addr; } while (ptr != head);
printf("(head)\n");

void reverse(scl **head_ref)


{ if (*head_ref == NULL)
{ return;

scl *prev = NULL;


scl *current = *head ref;
scl *next = NULL; scl
*head = *head ref;

do { next = current-
>addr; current->addr =
prev; prev = current;
current = next; } while
(current != head);

head->addr = prev;
*head_ref = prev;

int main() { scl


*head = NULL; scl
*ptr = NULL; int
n, val;
printf("Enter number of nodes: ");
scanf("%d", &n);

for (int i = 0; i < n; i++) { printf("Enter data


for node%d: i + 1); scanf("%d", &val);
scl *new_node = create_node_scl(val);
if (i 0) { head = new node; ptr =
new_node;
} else { ptr->addr =
new_node; ptr =
new_node;

if (ptr != NULL) { ptr-


>addr = head;
show(head);

printf("Reversing the circular linked list...


\n");
reverse(&head);

show(head);

return 0;
3.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX SIZE 100


char int
top = -1;

void push(char data) { if (top


MAX_SIZE - 1)
{ printf("OverfIow stack!\
n"); return;

top++; stack[top]
= data;
char pop() { if (top == -1)
{ printf("Empty stack!\
n"); return
char data =
stack[top]; top--;
return data;

int is_matching_pair(char char 1,


char char2) { if (charl '(I && char2 ==
return 1;
I I
} else if (charl '[ && char2 ]) { return
1;
} else if (charl '{I && char2 }){
return
1; } else
{ return 0;
int isBaI(char* text) {
int i; for (i = 0; i < strlen(text); i++) { if
(text[i] == '(l Il text[i] '[I Il text[i]

push(text[i]);
} else if (text[i] == l)' Il text[i] I] ' Il
I
text[i] } ) { if (top -1) { return 0; // If no
opening bracket is present
} else if (!
is_matching_pair(pop(), text[i]))
{ return 0; // If closing bracket doesn't
match the last opening bracket

if (top == -1) { return 1; // If the stack


is empty, the expression is balanced
} else {
return 0;
int main() { char
text[MAX_SIZE]; printf("lnput
an expression in parentheses:
"); scanf("%s", text);

if (isBaI(text)) { printf("The expression


is balanced.\n");
} else { printf("The expression is not
balanced.
\n");

return 0;
4.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int data; struct
Node *next; } Node;

void push(Node **head_ref, int new_data)


{
Node *new node =
(Node*)maIIoc(sizeof(Node));
new node->data = new data;
new node->next = *head
ref;

if (*head_ref != NULL) { Node


*last = *head ref; while (last-
>next != *head_ref)
last = last->next; last-
>next = new node;
} else {
new node->next = new node;

*head ref = new node;

void printList(Node *head) {


if (head == NULL)
{ printf("List is empty.\
n"); return;

Node *temp = head; do


{ printf("%d -> temp->data);
temp = temp->next;
} while (temp != head);
printf("(head)\n");
void EvenOdd(Node *head, Node
**even_head, Node **odd_head) {
if (head == NULL) return;

Node *temp = head; do { if (temp-


>data % 2 0) { push(even_head,
temp->data);
} else { push(odd_head, temp-
>data);

temp = temp->next;
} while (temp != head);
int main() {
Node *head = NULL;
Node *even head = NULL;
Node *odd head = NULL;

push(&head, 1 1
) push(&head, 1
0) push(&head,
8) push(&head,
6) push(&head,
4) push(&head,
2) push(&head,
0)

printf("OriginaI Circular Linked List:\n");


printList(head);

EvenOdd(head, &even_head,
&odd_head);

printf("\nEven Circular Linked List:\n");


printList(even_head);

printf("\nOdd Circular Linked List:\n");


printList(odd_head);
return 0;

outputs
1.

2.
3.

4.

5.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 100

// Global variables for stack


and top
char stack[MAX_SIZE];
int top = -1;

// Function to push a character


onto the stack
void push(char data) {
if (top == MAX_SIZE - 1) {
printf("Overflow stack!\
n");
return;
}
top++;
stack[top] = data;
}

// Function to pop a character


from the stack
char pop() {
if (top == -1) {
printf("Empty stack!\n");
return ' ';
}
char data = stack[top];
top--;
return data;
}

// Function to check if two


characters form a matching
pair of parentheses
int is_matching_pair(char
char1, char char2) {
if (char1 == '(' && char2 ==
')') {
return 1;
} else if (char1 == '[' &&
char2 == ']') {
return 1;
} else if (char1 == '{' &&
char2 == '}') {
return 1;
} else {
return 0;
}
}

// Function to check if the


expression is balanced
int isBalanced(char* text) {
int i;
for (i = 0; i < strlen(text); i++)
{
if (text[i] == '(' || text[i] ==
'[' || text[i] == '{') {
push(text[i]);
} else if (text[i] == ')' ||
text[i] == ']' || text[i] == '}') {
if (top == -1) {
return 0; // If no
opening bracket is present
} else if (!
is_matching_pair(pop(),
text[i])) {
return 0; // If closing
bracket doesn't match the last
opening bracket
}
}
}
if (top == -1) {
return 1; // If the stack is
empty, the expression is
balanced
} else {
return 0; // If the stack is
not empty, the expression is
not balanced
}
}

// Main function
int main() {
char text[MAX_SIZE];
printf("Input an expression in
parentheses: ");
scanf("%s", text);
// Check if the expression is
balanced or not
if (isBalanced(text)) {
printf("The expression is
balanced.\n");
} else {
printf("The expression is
not balanced.\n");
}
return 0;

You might also like