0

当我在输入前两个整数后编译此链接列表时,我的控制台堵塞。该程序的目的是将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 回答