このリンク リストをコンパイルすると、最初の 2 つの整数を入力した後にコンソール ジャムが発生します。プログラムの目的は、scanf からの入力をメモリに保存し、画面に出力することです。この後、プログラムに入力をテキスト ファイルに保存させるつもりです。
#include <stdio.h>
#include <stdlib.h>
/*********************************************************
* Node to represent a packet which includes a link reference*
* a link list of nodes with a pointer to a packet Struct *
**********************************************************/
struct Packet {
unsigned int Source;
unsigned int Destination;
unsigned int Type;
unsigned int Port;
char *Data;
struct Packet *next;
};
typedef struct Packet node; // Removes the need to constantly refer to struct
/*********************************************************
* Stubs to fully declared functions below *
**********************************************************/
void Outpacket(node **head);
void push(node **head, node **aPacket);
node* pop(node **head);
int main() {
/*********************************************************
* pointers for the link list and the temporary packeyt to *
* insert into the list *
**********************************************************/
node *pPacket, *pHead = NULL;
/*********************************************************
* Create a packet and also check the HEAP had room for it *
**********************************************************/
pPacket = (node *)malloc(sizeof(node));
if (pPacket == NULL)
{
printf("Error: Out of Memory\n");
exit(1);
}
printf("Enter Source Number:\n");
scanf("%i", pPacket->Source);
printf("Enter Destination Number:\n");
scanf("%i", pPacket->Destination);
printf("Enter Type Number:\n");
scanf("%i", pPacket->Type);
printf("Enter Port Number:\n");
scanf("%i", pPacket->Port);
printf("Enter Data Number:\n");
scanf("%c", pPacket->Data);
pPacket->next = NULL;
/*********************************************************
* Push the Packet onto the selected Link List, the function *
* is written so the program will support multiple link *
* list if additional 'pHead' pointers are created. *
* *
**********************************************************
* NOTE: The push parameters are using references to the *
* pointers to get round the pass by value problem caused *
* by the way C handles parameters that need to be *
* modified *
**********************************************************/
push(&pHead, &pPacket);
pPacket = (node *)malloc(sizeof(node));
if (pPacket == NULL)
{
printf("Error: Out of Memory\n");
exit(1);
}
printf("Enter Source Number:\n");
scanf("%i", pPacket->Source);
printf("Enter Destination Number:\n");
scanf("%i", pPacket->Destination);
printf("Enter Type Number:\n");
scanf("%i", pPacket->Type);
printf("Enter Port Number:\n");
scanf("%i", pPacket->Port);
printf("Enter Data Number:\n");
scanf("%c", pPacket->Data);
pPacket->next = NULL;
push(&pHead, &pPacket);
/*********************************************************
* Display the Link List 'pHead' is passed as a reference *
**********************************************************/
Outpacket(&pHead);
if(pPacket = pop(&pHead))
{
printf("pPacket %s\n", pPacket->Data);
free(pPacket);
};
Outpacket(&pPacket);
while(pPacket = pop(&pHead)) {
free(pPacket);
}
return 0;
}
void Outpacket(node **head)
{
/*********************************************************
* Copy Node pointer so as not to overwrite the pHead *
* pointer *
**********************************************************/
node *pos = *head;
printf("Packet list\n");
/*********************************************************
* Walk the list by following the next pointer *
**********************************************************/
while(pos != NULL) {
printf("Source: %i Destination: %i Type: %i Data: %i \n", pos->Source, pos->Destination, pos->Type, pos->Data, pos->next);
pos = pos->next ;
}
printf("End of Packet\n\n");
}
void push(node **head, node **aPacket)
{
/*********************************************************
* Add the cat to the head of the list (*aCat) allows the *
* dereferencing of the pointer to a pointer *
**********************************************************/
(*aPacket)->next = *head;
*head = *aPacket;
}
node *pop(node **head)
{
/*********************************************************
* Walk the link list to the last item keeping track of *
* the previous. when you get to the end move the end *
* and spit out the last Packet in the list *
**********************************************************/
node *curr = *head;
node *pos = NULL;
if (curr == NULL)
{
return NULL;
} else {
while (curr->next != NULL)
{
pos = curr;
curr = curr->next;
}
if (pos != NULL) // If there are more packets move the reference
{
pos->next = NULL;
} else { // No Packets left then set the header to NULL (Empty list)
*head = NULL;
}
}
return curr;
}
ありがとう