안녕하세요, 목록에 새 노드를 추가하는 방법에 대해 궁금합니다. 그래서 목록을 보유하고있는 컨테이너를 가지고 있는데, 이것도 추가해야합니다. 다음과 같이 정의됩니다. 이리스트목록에 새 노드 추가
struct vm
{
struct vm_list * item_list;
struct coin * coins;
char * foodfile;
char * coinsfile;
};
이위한 컨테이너 노드 보유 목록이다.
struct vm_list
{
struct vm_node * head;
unsigned length;
};
이
이 내가 목록에 항목을 추가하려고하고 어떻게 노드struct vm_node
{
struct stock_item * data;
struct vm_node * next;
};
의 정의입니다. 다음
BOOLEAN add_item(struct vm* vm)
{
struct stock_item *newItem;
newItem = malloc(sizeof(struct stock_item));
struct vm_node *newNode; //setup a new item to store the data in.
newNode = malloc(sizeof(struct vm_node));
struct vm_node *VMP; //setup the new Node to hold the newItem
VMP = malloc(sizeof(struct vm_node));
struct vm_node *VMF; //too keep track of the first element in the list. So i can print out the list from the start.
VMF = malloc(sizeof(struct vm_node));
VMF->next = vm->item_list->head;
/* the following loops through until it finds the last element in the list. Which it stores in the VMP structure. So we can increment the ID counter by 1.*/
while(vm->item_list->head){
VMP->data = vm->item_list->head->data; //stores the last element of the linked list. Allowing us to add information to the nth bottom of the list.
vm->item_list->head = vm->item_list->head->next;
}
/* defining the items to be stored in the newItem struct. */
char *newName;
char *newDesc;
char * onHand;
char *price;
newName = malloc(sizeof(char));
newDesc = malloc(sizeof(char));
price = malloc(sizeof(char));
onHand = malloc(sizeof(char));
char *dem = VMP->data->id; //returns the ID of the last item in the list.
int change = atoi(&dem[4]); //changes the item to a int.
change++; //increments by 1.
char str;
sprintf(&str, "%d", change); // converts back to char.
dem[4]=str; // stores back in the char array.
read_rest_of_line(); //buffer handler.
printf("Enter the item name: ");
newName = getlines();
printf("\n");
printf("Enter the item description: ");
newDesc = getlines();
printf("\n");
printf("Enter how many will be stocked: ");
onHand = getlines();
printf("\n");
printf("How much will the item cost: ");
price = getlines();
printf("\n");
//adding a new item to the list.
newNode->next = NULL;
newItem = add_tobottomoflist(dem,newName,newDesc,onHand,price); //add_tobottom is setups the newItem.
newNode->data = newItem; // sets the data for newNode = to newItem
vm->item_list->head = newNode; //sets up the new Node.
vm->item_list->head->next = VMF; //points to the first element in the list for linking.
display_items(vm); // displays all the elements in the list.
/* The UNUSED() function is designed to prevent warnings while your
* code is only partially complete. Delete this function call once
* you are using vm in your own code */
UNUSED(vm);
return FALSE;
}
출력 코드 RAN은이다.
IO NAME Description On Hand price
I0006 Nam
desc
12 $3.04
p� -2108446504 $-2108446504.32615
I0001 Coke 375 ml Can of coke 50 $3.50
I0002 Pepsi 375 ml Can of pepsi 20 $3.00
I0003 Lemon Cheesecake A delicious, 1/8 size slice of cheesecake 10 $4.00
I0004 Mars Bar A delicious 50 g Mars Bar chilled just the way you like it. 20 $3.00
I0006 Lemon Tart A delicious lemon butter tart with a pastry based 12 $3.75
레몬 타르트 ID는 내가이 증가하고있다 아무 생각, 새로운 항목이 없습니다 끝에 목록의 시작 부분에 추가되었습니다 같음 I0005
하지 I0006
해야한다. 또한 목록 시작 부분에 null
개체가있는 것으로 보입니다.
여기에서 일어날 수있는 일에 대한 의견이 있으십니까?
귀하의 추가 사항이 구체적으로 질문하지 않아야합니다. 삽입하기 전에이 모든 것을 가져 와서하십시오. – ChiefTwoPencils
@ChiefTwoPencils 응? –
어쨌든 주문한 목록이 아닙니다. – ChiefTwoPencils