2013-04-27 84 views
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; 
} 

感谢

回答

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; 
} 

此代码在我的机器perfectly.Please检查更改,并比较两者的代码,问我,如果你不明白的任何变化上运行。

+0

这不提供问题的答案。要批评或要求作者澄清,在他们的帖子下留下评论 - 你可以随时评论你自己的帖子,一旦你有足够的[声誉](http://stackoverflow.com/faq#reputation),你将能够[评论任何帖子](http://stackoverflow.com/privileges/comment)。 – 2013-04-27 17:24:36

+0

@TimBish我仍在努力寻找答案。 – 10111 2013-04-27 18:08:45

+0

我无法解释所有的变化,但问我什么你不明白,我认为变化4是一个概念,因为你必须分配内存到结构的字符指针之前,存储输入流到它。 – 10111 2013-04-27 19:49:24