0% found this document useful (0 votes)
6 views3 pages

Singly Linked List Text Editor Operations

The document describes an implementation of a text editor using a singly linked list to manage lines of text. It includes functionalities for inserting, deleting, displaying, and reversing lines of text. The code provides a menu-driven interface for user interaction with these operations.

Uploaded by

f716nakulkapse
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Singly Linked List Text Editor Operations

The document describes an implementation of a text editor using a singly linked list to manage lines of text. It includes functionalities for inserting, deleting, displaying, and reversing lines of text. The code provides a menu-driven interface for user interaction with these operations.

Uploaded by

f716nakulkapse
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment No.

6:

Singly Linked List Opera ons


You are building a text editor where lines of text are stored dynamically. You need to allow inser on and
dele on of lines at any posi on, and display text both normally and in [Link] a singly linked list to
implement: display, insert (front/end/middle), delete (front/end/middle), display in reverse, and reverse
the list.

//DSA PROGRAM 6

//Name:- Deshmukh Sarthak Sunil//


//Roll No. :- 2401028//
//DIV :- SE (A)//
//Batch :- B//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char line[100];
struct Node* next;
};
struct Node* head = NULL;
// Insert at end
void insertEnd(char text[]) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
strcpy(newNode->line, text);
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
prin ("Line added at end.\n");
}
// Display all lines
void display() {
struct Node* temp = head;
if (temp == NULL) {
prin ("List is empty.\n");
return;
}
prin ("Text Lines:\n");
while (temp != NULL) {
prin ("%s\n", temp->line);
temp = temp->next;
}
}
// Display lines in reverse using recursion
void displayReverse(struct Node* node) {
if (node == NULL) return;
displayReverse(node->next);
prin ("%s\n", node->line);
}
// Reverse the list
void reverseList() {
struct Node *prev = NULL, *curr = head, *next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
prin ("List reversed.\n");
}
// Delete from front
void deleteFront() {
if (head == NULL) {
prin ("List is empty.\n");
return;
}
struct Node* temp = head;
head = head->next;
free(temp);
prin ("First line deleted.\n");
}
int main() {
int choice;
char text[100];
do {
prin ("\n--- Simple Text Editor ---\n");
prin ("1. Add Line at End\n");
prin ("2. Display All Lines\n");
prin ("3. Display in Reverse\n");
prin ("4. Reverse the List\n");
prin ("5. Delete First Line\n");
prin ("6. Exit\n");
prin ("Enter choice: ");
scanf("%d", &choice);
getchar(); // clear newline
switch (choice) {
case 1:
prin ("Enter line: ");
fgets(text, sizeof(text), stdin);
text[strcspn(text, "\n")] = 0; // remove newline
insertEnd(text);
break;
case 2:
display();
break;
case 3:
prin ("Lines in reverse:\n");
displayReverse(head);
break;
case 4:
reverseList();
break;
case 5:
deleteFront();
break;
case 6:
prin ("Exi ng...\n");
break;
default:
prin ("Invalid choice.\n");
}
} while (choice != 6);
return 0;
}

You might also like