Topics Cursor Manipulated Singly Linked List Description Editor Create a program
ID: 3538124 • Letter: T
Question
Topics
Cursor Manipulated Singly Linked List
Description
Editor
Create a program that will implement a Line Editor using a singly linked list as described below.
Linked List
Each node in the linked list will contain one line of text. It will also contain a link that will point to the next node. Thus the first node will contain the first line of text, the second node the second line of text and so on. The link in the last node will contain NULL. We will consider the last node pointing to an imaginary non-existent node at location NULL. We will refer to this nodes as the %u201Cghost node%u201D and the imaginary non-existent line represented by this node as the %u201Cghost line%u201D.
Line Cursor
The editor will also have a cursor that will always point to the current (or working) line.
Moving Cursor
The editor will provide commands to move the cursor back and forth. The user will be able to move the cursor as far back as the first line and as far forward as the ghost line i.e. the imaginary line past the actual last line. In other words, the cursor will be allowed to move forward and have a value of NULL indicating that it is pointing to the ghost line. However, it will never use the NULL to access any values from the imaginary ghost line.
Inserting Lines
The editor will provide a command for inserting one or more lines. It will always insert lines just before the current line. By allowing the cursor to move to the ghost line (imaginary line past the last line), the user will be able to insert lines before the ghost line i.e. after the last line.
Deleting A Line
The editor will provide a command for deleting the current line (the line where cursor is pointing to).
Displaying Lines Starting From Cursor
The editor will provide a command for displaying lines starting with the current line and ending with the last existing line.
Displaying All Lines
The editor will provide a command for displaying lines starting with the very first line and ending with the last existing line.
Command Interface
The editor will provide a command interface. It will use the symbol %u201C>%u201D as a prompt. It will show the prompt initially at the start of the program and subsequently after completing each command. In response to the prompt, the user will enter a single character command followed by a new line.
Command List
All the single character commands provided by the editor are listed below.
I
D
M
L
A
X
Command Description
The above commands are described below in detail.
I (Insert)
This command will be used to insert multiple lines before the current line.
After this command is entered, the user will be able to input none, one or more lines of text. The user will indicate the end of input by entering two forward slashes %u201C//%u201D on a line by itself. One by one, the editor will insert each line just before the current line leaving the cursor unchanged.
When the user finishes entering the first line and presses the enter key, the editor will immediately insert that line just before the current line leaving the cursor unchanged. When the user enters the second line it too will be inserted just before the same current line (because the cursor is still pointing to the same current line) but after the first line which had already been inserted.
D (Delete)
This command will be used to delete the current line.
In response to this command, the editor will delete the current line and move the cursor forward to the next line. If the user attempts to delete a ghost line, the editor will do nothing.
M (Move)
This command will be used to move the cursor forward or backwards one or more positions and to display the contents of the new current line.
When the user enters this command, the editor will prompt the user to enter a zero, a positive or a negative number.
If the user enters a zero, the editor will display the contents of the current line leaving the cursor unchanged.
If the user enters a positive number, the editor will attempt to move the cursor that many number of lines forward unless the cursor reaches the ghost line. At a maximum, the editor will move the cursor forward to the ghost line (the imaginary line after the last line). After making the appropriate move, it will display the content of the new current line. If the new current line is the ghost line, it will display the symbol %u201C%u201D.
If the user enters a negative number, the editor will attempt to move the cursor that many lines backwards unless the cursor reaches the first line. At a maximum, the editor will move the cursor backward to the first line. After making the appropriate move, it will display the content of the new current line. If the original list was empty, the editor will leave the cursor unchanged pointing to the ghost entry and will display the symbol %u201C%u201D.
L (List)
This command is used to display lines from the current line to the last line.
After user enters this command, the editor will display all lines starting with the current line and ending with the last line. After displaying the required lines, it will display the symbol %u201C%u201D on a line by itself.
A (List All)
This command is used to display lines starting with the very first line and ending with the last line.
After user enters this command, the editor will display all lines starting with the very first line and ending with the last line. After displaying the required lines, it will display the symbol %u201C%u201D on a line by itself.
X (Exit)
This command is used to end the program.
When the user enters this command, the editor displays %u201CBye%u201D and exits.
Initialization
Initially, the list will be empty. The head and the cursor will be assigned NULL (i.e. they will point to the ghost entry). Over time, lines will be added to and deleted from the list. Then the head will point to the first line and the cursor to the current working line. Over time, through deletions, the list may again become empty. At that time, the head and the cursor should again become NULL indicating that the list is empty.
Test Data
Use the following input/out dialog for testing.
>L
>I
aaa aaa
bbb bbb
//
>L
>M
Enter a zero, pos or neg number:
-2
aaa aaa
>L
aaa aaa
bbb bbb
>M
Enter a zero, pos or neg number:
1
bbb bbb
>L
bbb bbb
>I
ccc ccc
ddd ddd
//
>L
bbb bbb
>M
Enter a zero,pos or neg number:
-1000
aaa aaa
>L
aaa aaa
ccc ccc
ddd ddd
bbb bbb
>M
Enter a zero, pos or neg number:
1000
>I
eee eee
//
> M
Enter a zero,pos or neg number:
-999
aaa aaa
>L
aaa aaa
ccc ccc
ddd ddd
bbb bbb
eee eee
>D
>L
ccc ccc
ddd ddd
bbb bbb
eee eee
> M
Enter a zero,pos or neg number:
1
ddd ddd
>D
>L
bbb bbb
eee eee
>> M
Enter a zero,pos or neg number:
-99
ccc ccc
> L
ccc ccc
bbb bbb
eee eee
>X
Bye
Explanation / Answer
#include #include #include #include typedef struct node { char line[300]; struct node *link; }NODE; typedef struct cursor { NODE *prev; NODE *next; }CURSOR; typedef struct list { NODE *link; int count; }LIST; void takeCommand(CURSOR **pCursor,LIST **pList); void insertLine(CURSOR **pCursor,LIST**); void deleteLine(CURSOR **pCursor,LIST **pList); void moveCursor(CURSOR **pCursor,LIST **pList); void displayFromCursor(CURSOR **pCursor); void displayAll(LIST **pList); void main(void) { LIST *pList; pList->count = 0; pList->link = NULL; CURSOR *pCursor; pCursor->prev = NULL; pCursor->next = NULL; takeCommand(&pCursor,&pList); printf("BYE "); return; } void takeCommand(CURSOR **pCursor,LIST **pList) { char c; printf(">"); c=getchar(); switch(c) { case 'I': insertLine(pCursor,pList); case 'D': deleteLine(pCursor,pList); case 'M': moveCursor(pCursor,pList); case 'L': displayFromCursor(pCursor); case 'A': displayAll(pList); case 'X':return; default : printf("Enter correct value again"); } takeCommand(pCursor,pList); } void insertLine(CURSOR **pCursor,LIST **pList) { char newLine[300]; bool x=true; do { if(strcmp(newLine,"//")) { x=false; } else { scanf("%s",newLine); NODE *pNew; pNew = (NODE*)malloc(sizeof(NODE)); strcpy(pNew->line,newLine); pNew->link = (*pCursor)->next; (*pCursor)->prev = pNew; ((*pList)->count)++; } }while(true); return; } void deleteLine(CURSOR **pCursor,LIST **pList) { NODE *temp; temp->link = (*pCursor)->next; (*pCursor)->next = ((*pCursor)->next)->link; free(temp); ((*pList)->count)--; return; } void displayFromCursor(CURSOR **pCursor) { if((*pCursor)->next !=NULL) { printf("%s ",(*pCursor)->next->line); displayFromCursor( ((*pCursor)->next) ); } return; } void displayAll(LIST **pList) { if((*pList)->link !=NULL) { printf("%s ",(*pList)->link->line); displayFromCursor( ((*pList)->link) ); } return; } void moveCursor(CURSOR **pCursor,LIST **pList) { int countNew=(*pList)->count; int temp,i; printf("Enter the number to move cursor "); scanf("%d",&temp); countNew-=temp; while((*pCursor)->next !=NULL) { (*pCursor) = (*pCursor)->next; countNew--; } (*pCursor)->next = (*pList)->link; (*pCursor)->prev = NULL; for(i=0;iprev = (*pCursor)->next; (*pCursor)->next = (*pCursor)->next->link; } return; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.