0% found this document useful (0 votes)
9 views

Graded3 C

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)
9 views

Graded3 C

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/ 9

SOURCE CODE-

#include <stdio.h>

#include <stdlib.h>

typedef struct node {

char data;

struct node* next;

} Node;

Node* createCircularLinkedList(char name[]) {

Node* head = NULL;

Node* temp = NULL;

Node* prev = NULL;

for (int i = 0; name[i] != '\0'; i++) {

temp = (Node*)malloc(sizeof(Node));

temp->data = name[i];

temp->next = NULL;

if (head == NULL) {

head = temp;

prev = temp;

head->next = prev;

} else {

prev->next = temp;
temp->next = head;

prev = temp;

return head;

void reverse(Node** head) {

Node* prev = NULL;

Node* current = *head;

Node* next = NULL;

while (current != NULL) {

next = current->next;

current->next = prev;

prev = current;

current = next;

*head = prev;

int count(Node* head) {

Node* temp = head;

int count = 0;
if (head != NULL) {

do {

count++;

temp = temp->next;

} while (temp != head);

return count;

void display(Node* head) {

Node* temp = head;

if (head != NULL) {

do {

printf("%c ", temp->data);

temp = temp->next;

} while (temp != head);

printf("\n");

void insertafterchar(Node** head, char key, char ch) {


Node* temp = *head;

Node* newNode = NULL;

if (temp != NULL) {

do {

if (temp->data == key) {

newNode = (Node*)malloc(sizeof(Node));

newNode->data = ch;

newNode->next = temp->next;

temp->next = newNode;

temp = temp->next;

} while (temp != *head);

void deletechar(Node** head, char key) {

Node* temp = *head;

Node* prev = NULL;

if (temp != NULL && temp->data == key) {

*head = temp->next;

free(temp);

if (*head != NULL)
temp = *head;

else

return;

while (temp != *head) {

if (temp->data == key) {

prev->next = temp->next;

free(temp);

return;

prev = temp;

temp = temp->next;

int main() {

int choice;

char name[100];

char key, ch;

Node* head = NULL;

printf("Enter a name: ");

scanf("%s", name);
head = createCircularLinkedList(name);

while (1) {

printf("\n1. Reverse\n2. Count\n3. Display\n4. Insert after char\n5. Delete char\n6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

reverse(&head);

break;

case 2:

printf("Number of characters: %d\n", count(head));

break;

case 3:

display(head);

break;

case 4:

printf("Enter character to insert after: ");

scanf(" %c", &key);

printf("Enter character to insert: ");

scanf(" %c", &ch);

insertafterchar(&head, key, ch);

break;

case 5:
printf("Enter character to delete: ");

scanf(" %c", &key);

deletechar(&head, key);

break;

case 6:

exit(0);

default:

printf("Invalid choice. Please try again.\n");

return 0;

}
OUTPUT-

You might also like