0

このリンク リストをコンパイルすると、最初の 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;
}

ありがとう

4

1 に答える 1

0
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>



struct Packet {
unsigned int Source;
unsigned int Destination;
unsigned int Type;
unsigned int Port;
char *Data;

struct Packet *next;

};

typedef struct Packet* node; 

node getnode(void){
        node p;
        p=malloc(sizeof(struct Packet));/*change1*/
        return p;
        }

void Outpacket(node *head);
void push(node *head, node *aPacket);/*change 2*/
node pop(node *head);

int main() {


node pPacket = NULL;
node pHead = NULL;



pPacket = getnode();/*change 3*/
if (pPacket == NULL)
{
    printf("Error: Out of Memory\n");
    exit(1);
}

printf("Enter Source :\n");
scanf("%d", &(pPacket->Source));
printf("Enter Destination Number:\n");
scanf("%d", &(pPacket->Destination));
printf("Enter Type Number:\n");
scanf("%d", &(pPacket->Type));
printf("Enter Port Number:\n");
scanf("%d", &(pPacket->Port));
printf("Enter Data Number:\n");
pPacket->Data=malloc(100);/*change 4,might overflor,so you decide how much memory you want*/
scanf("%s",pPacket->Data);


pPacket->next = NULL;


push(&pHead, &pPacket);

pPacket = getnode();/*change 5*/
if (pPacket == NULL)
{
    printf("Error: Out of Memory\n");
    exit(1);
}

printf("Enter Source Number:\n");
scanf("%d", &(pPacket->Source));
printf("Enter Destination Number:\n");
scanf("%d", &(pPacket->Destination));
printf("Enter Type Number:\n");
scanf("%d", &(pPacket->Type));
printf("Enter Port Number:\n");
scanf("%d", &(pPacket->Port));
printf("Enter Data Number:\n");
pPacket->Data=malloc(100);/*change 6*/
scanf("%s",pPacket->Data);

pPacket->next = NULL;

push(&pHead, &pPacket);

Outpacket(&pHead);

if(pPacket == pop(&pHead))/*change 7,this was a classic error*/
{
    printf("pPacket %s ", pPacket->Data);
    free(pPacket);

}

Outpacket(&pPacket);

while(pPacket == pop(&pHead))/*change 8,again the same error,please take care of this*/{
    free(pPacket);
}
getch();
return 0;
}

void Outpacket(node *head)
{

node pos = *head;
printf("Packet list\n");

while(pos != NULL) {
    printf("Source: %d Destination: %d Type: %d Data: %s \n", pos->Source, pos->Destination, pos->Type,pos->Data);/*change 9,what you wrote was absurd, I think*/
    pos = pos->next ;
}
printf("End of Packet\n\n");
}

 void push(node *head, node *aPacket)
 {

 (*aPacket)->next = *head;
 *head = *aPacket;
 }

 node pop(node *head)
 {

 node curr = *head;
 node pos = NULL;
 if (curr == NULL)
 {
    return NULL;
 } else {
    while (curr->next != NULL)
    {
        pos = curr;
        curr = curr->next;
    }
    if (pos != NULL) 
    {
        pos->next = NULL;
    } else {         
        *head = NULL;
    }
}
return curr;
}

このコードは私のマシンで完全に実行されます。変更を確認し、両方のコードを比較してください。変更が理解できない場合は私に尋ねてください。

于 2013-04-27T17:08:29.223 に答える