2013-04-27 2 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). –

+0

@TimBish Я все еще работаю над ответом. – 10111

+0

Я не могу объяснить все изменения, но спросить меня, что когда-либо вы не понимаете, я думаю, что изменение 4 было концептуальным, потому что вам нужно было выделить память символьному указателю структуры, прежде чем хранить входной поток в нее. – 10111

Смежные вопросы