问题描述
您好,我是计算机科学的新学生,目前正在尝试学习C ++语言。
我正在尝试创建一个具有嵌套链表的程序,其中主链表包含客户的名称,Id号,头和尾指针(每个客户节点唯一),用于辅助链表,该链表包含表示相应客户的注释行。
我要创建的示例:
主链表:
NULL William / 1228 stefan / 1344 andre / 1566 [TAIL指针]-> NULL
第一个节点(William / 1228)具有第二个链接列表:
NULL “喜欢点甜点” “对坚果过敏” [tail_notes]-> NULL
问题是我无法为每个客户节点创建不同的头指针,因此每个客户都有相同的共享便笺,而且我不知道如何解决。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define SIZE 20
struct notes{
//contained attributes
char string[SIZE];
//pointer to other nodes
struct notes *prev = NULL;
struct notes *next = NULL;
};
struct node{
//contained attributes
char customer_name[SIZE];
int id_number;
//pointer to other nodes
struct node *prev;
struct node *next;
//head and tail pointer to notes;
struct notes *head_notes;
struct notes *tail_notes;
struct notes *curr_notes;
void showNotes(){
printf("==========================================================\n");
printf("Name : %s\n",customer_name);
printf("ID number : %d\n",id_number);
printf("Notes :\n");
printf("==========================================================\n");
curr_notes = head_notes;
while(curr_notes != NULL){
printf(" -) %s\n",curr_notes->string);
curr_notes = curr_notes->next;
}
printf("==========================================================\n");
}
void addNotes(char string[]){
struct notes *temp = (struct notes*)malloc(sizeof(struct notes));
strcpy(temp->string,string);
temp->prev = NULL;
temp->next = NULL;
if(this->head_notes == NULL){
this->head_notes = temp;
this->tail_notes = temp;
}else{
temp->prev = this->tail_notes;
this->tail_notes->next = temp;
this->tail_notes = temp;
}
this->tail_notes->next = NULL;
this->head_notes->prev = NULL;
}
};
struct node *head = NULL;
struct node *tail = NULL;
struct node *curr;
void push(struct node* new_node){
//if linkedlist is empty
if(head == NULL){
head = new_node;
tail = new_node;
}
//insert from tail
else{
new_node->prev = tail;
tail->next = new_node;
tail = new_node;
}
head->prev = NULL;
tail->next = NULL;
return;
}
void printList(){
curr = head;
while(curr != NULL){
curr->showNotes();
curr = curr->next;
}
}
struct node *create(){
struct node *customer = (struct node*)malloc(sizeof(struct node));
customer->head_notes = NULL;
customer->tail_notes = NULL;
customer->next = NULL;
customer->tail_notes = NULL;
return customer;
}
void Input(){
//decalring variable
char key_pressed,string[SIZE];
int length,id;
bool repeat,ask_name,ask_notes;
repeat = true;
//creating and initialize the Node for customer's data
struct node *customer = create();
//starting main loop
while(repeat){
//initialize necessary variable
strcpy(string,"");
length = 0;
ask_name = true;
//loop to ask customer's name
system("cls");
printf("\n==========================\n\nInsert Name : %s",string);
while(ask_name){
if(kbhit()){
key_pressed = getch(); fflush(stdin);
//user press enter
if(key_pressed == 13 && length < SIZE){
if(strcmp(string,"END") == 0) return;
string[length] = '\0';
strcpy(customer->customer_name,string); //inserting customer name to the node
strcpy(string,"");
length = 0;
ask_name = false;
}
//user press backspace
else if(key_pressed == 8){
if(length != 0) length--;
string[length] = '\0';
}
//user press normal character
else if (key_pressed >= 32 && key_pressed <= 126 && length < SIZE){
string[length] = key_pressed;
length++;
string[length] = '\0';
}
//refresh screen
system("cls");
printf("\n==========================\n\nInsert Name : %s\n",string);
if(length == SIZE) printf("[Name Limit is 30 Character]\n");
}
}
//asking for id number
system("cls");
printf("\n==========================\n\n");
printf("Insert Name : %s\n",customer->customer_name);
printf("Insert ID Number : ");
scanf("%d",&id);getchar();
customer->id_number = id;
//asking notes
system("cls");
printf("\n==========================\n\nNotes :\n");
customer->showNotes();
ask_notes = true;
while(ask_notes){
if(kbhit()){
key_pressed = getch(); fflush(stdin);
//user press enter
if(key_pressed == 13 || length == SIZE){
if(strcmp(string,"END") == 0){
strcpy(string,"");
length = 0;
ask_notes = false;
continue;
}
string[length] = '\0';
customer->addNotes(string);
strcpy(string,"");
length = 0;
}
//user press backspace
else if(key_pressed == 8){
if(length != 0) length--;
string[length] = '\0';
}
//user press normal character
else if (key_pressed >= 32 && key_pressed <= 126){
string[length] = key_pressed;
length++;
string[length] = '\0';
}
//refresh screen
system("cls");
printf("==========================\n\nNotes : %s\n\n",string);
customer->showNotes();
}
}
push(customer);
}
}
int main(){
Input();
printList();
printf("DONE\n");
return 0;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)