Hi, everyone.
I am a student preparing to return to school through a book called "Data Structure Written Easily in C Language."
I was looking at an example of creating an mp3 playback program through a dual connection list.
"Yes... "Why did you put this conditional sentence here?" I post this thinking.
(I want to upload a part of the entire program, but I'm posting the full text because I think the source code is to write something like a novel. I made a typo check, but if there is, please think of it as a cute act Haha)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char element[100];
typedef struct DListNode {
element data;
struct DListNode *llink;
struct DListNode *rlink;
}DListNode;
DListNode* current;
// Initialize dual connection list
void init(DListNode* phead)
{
phead->llink = phead;
phead->rlink = phead;
}
// Output a node in a dual connection list
void print_dlist(DListNode* phead)
{
DListNode* p;
for (p = phead->rlink; p != phead; p = p->rlink)
{
if(p == current)
printf("<-| #%s# |->",p->data);
else
printf("<-| %s |->",p->data);
}
printf("\n");
}
// Insert a node newnode to the right of the node before.
void dinsert(DListNode *before, element data)
{
DListNode *newnode = (DListNode *)malloc(sizeof(DListNode));
strcpy_s(newnode->data, data,sizeof(data));
// I don't understand why you use strcpy() instead of '= operator'.
// I posted a question about this, too!
newnode->llink = before;
newnode->rlink = before->rlink;
before->rlink->llink = newnode;
before->rlink = newnode;
}
// Delete the node removed.
void ddelete(DListNode* head, DListNode* removed)
{
if (removed == head) return;
removed->llink->rlink = removed->rlink;
removed->rlink->llink = removed->llink;
free(removed);
}
int main(void)
{
char; // operation key variable
DListNode *head = (DListNode *)malloc(sizeof(DListNode))); // head node declaration
init(head); // head node initialization
// Insertion
dinsert(head, "Mamamia");
dinsert(head, "Dancing Queen");
dinsert(head, "Fernando");
current = head->rlink;
print_dlist(head);
do {
printf("\nEnter the command (<,>,q): ");
ch = getchar();
if (ch == '<') {
current = current->llink;
if (current == head)
current = current->llink;
}
else if (ch == '>') {
current = current->rlink;
if (current == head)
current = current->rlink;
}
print_dlist(head);
getchar();
} while (ch! = 'q'); // loop until the operation key variable is q
// Dynamic memory release code here
}
The part that I don't understand in the core is
I don't understand why the if conditional statement exists in the do-while statement right above.
do {
printf("\nEnter the command (<,>,q): ");
ch = getchar();
if (ch == '<') {
current = current->llink;
if (current == head)
current = current->llink;
}
else if (ch == '>') {
current = current->rlink;
if (current == head)
current = current->rlink;
}
print_dlist(head);
getchar();
} } while (ch != 'q');
Even if you always try to think long and think deeply,
If you just look at it from the same perspective, I ask you this question.
Also, I pray that the ridiculous mistakes or thoughts were not short.
Thank you :)
linked-list data-structure c
A dummy node whose head node has a trash value. So when you type '<' or '>', move once in the direction, but if the result of the move is the head node, what you get is the garbage price, so move it once more.
© 2024 OneMinuteCode. All rights reserved.