![](/img/trans.png)
[英]Is there a function in Wireshark which I can use to decode packets in a memory buffer rather than a capture file?
[英]Can I use a CreatNode function to return a struct rather than its steps repeated in All other functions?
我想在 C 中創建一個 CreatNode() 函數以供其他函數調用。 我正在玩弄代碼,試圖達到很好的可讀性和功能。 教授有一個 CreatEmptyList() 函數,但沒有一個 CreatNode()。她是疏忽大意的,沒有概念和 C 語言的能力,沒有給我答案。
我不需要這種探索和嘗試的想法來通過課程,但我的目標是成為一名開發人員而不是畢業。
這是教授的代碼:
typedef struct nodetype
{
int info;
struct nodetype *next;
} node;
node *head;
void createemptylist(node *head)
{
head=NULL;
}
void insertatbeginning(node *head, int item)
{
node *newNode;
/* allocate memory for the new node and initialize the data in it*/
newNode= malloc(sizeof(node));
newNode->info=item;
/* assign the value of head to the “next” of newNode*/
newNode->next=head;
/* assign the address of newNode to head */
head=newNode;
}
void insertatend(node *head, int item)
{
node *newNode;
newNode=malloc(sizeof(node));
newNode->info=item;
newNode->next=NULL;
if(head==NULL)
head=newNode;
else
{
node *prev=head;
while(prev->next!=NULL)
prev=prev->next;
prev->next=newNode;
}
}
所有都是她提供的 PDF 的片段,不完全是可編譯的代碼。
這是我正在處理的代碼,它不斷給出錯誤:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int info;
struct Node *Next;
}ListNode;
ListNode CreatNode(ListNode *Head){///These steps not to be repeated using this function
printf("\n=================\nEntered CreatNode Function");
ListNode *NewNode;
NewNode = malloc(sizeof(ListNode));
return *NewNode;
}
void CreatList(ListNode *Head){
printf("\n=================\nEntered CreatList Function");
Head = NULL;
}
void InserBeg(ListNode *Head, int item){
///CreatNode() steps here
NewNode=CreatNode(&Head);
NewNode->info = item; ///Inesrt value
NewNode->Next = Head;///Insert Adress inside Head to the Next point
Head = NewNode;
printf("\nFinished InsertBeg Function");
printf("\nValue inserted is: %d\n=================\n", NewNode->info);
}
void Append(ListNode *Head, int item){
///CreatNode() steps here
///NewNode=CreatNode(Head);
NewNode ->info = item;
NewNode ->Next = NULL;
if (Head==NULL){
Head=ListNode
}
else{
ListNode *Prev=Head;
while(while->Prev!=NULL){
Prev = Prev->Next;
}
Prev->Next=NewNode;
}
}
int main(){
ListNode *Head;
CreatList(&Head);
InserBeg(&Head, 8);
return 0;
}
錯誤:
C:\Users\User\Desktop\all\C\Single Linked List test.c|27|error: incompatible types when assigning to type 'ListNode * {aka struct Node *}' from type 'ListNode {aka struct Node}'|
Undeclared NewNode struct errors since it can't see it
對以不同方式編碼我的想法或使我的代碼工作有任何幫助嗎?
教授提供的代碼非常糟糕。
首先,她使用全局變量head
。 由於變量是在文件范圍內聲明的,因此它已經被初始化為空指針。 所以這個函數
void createemptylist(node *head)
{
head=NULL;
}
沒有多大意義。 此外,它對原始指針head
沒有任何作用,因為它按值接受其參數。 也就是說,它處理原始指針值的副本。
由於這個原因,其他函數insertatbeginning
和insertatend
是錯誤的,因為它們不會更改它們按值接受的原始指針head
。
void insertatbeginning(node *head, int item)
{
//...
head=newNode;
}
void insertatend(node *head, int item)
{
//...
head=newNode;
//...
}
它們更改原始指針值的副本。
您的函數中也存在同樣的問題。
至於功能CreatNode
那么它沒有任何意義。
ListNode CreatNode(ListNode *Head){///These steps not to be repeated using this function
printf("\n=================\nEntered CreatNode Function");
ListNode *NewNode;
NewNode = malloc(sizeof(ListNode));
return *NewNode;
}
對於初學者,函數中不使用參數head
。 您需要將整數參數傳遞給將用作數據成員info
的初始化程序的函數。
您應該返回指向動態分配對象的指針,而不是ListNode
類型的對象。 否則,該函數將返回動態分配對象的副本,因此該函數將產生內存泄漏。
在函數InserBeg
和Append
中,名稱NewNode
未定義,例如
void InserBeg(ListNode *Head, int item){
///CreatNode() steps here
NewNode=CreatNode(&Head);
//...
您正在調用傳遞不兼容指針類型ListNode **
而不是ListNode *
表達式的函數。
CreatList(&Head);
InserBeg(&Head, 8);
這是一個演示程序,展示了如何定義函數CreateNode
和例如InsertBeg
。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int info;
struct Node *next;
} ListNode;
ListNode * CreateNode( int info )
{
ListNode *new_node = malloc( sizeof( *new_node ) );
if ( new_node != NULL )
{
new_node->info = info;
new_node->next = NULL;
}
return new_node;
}
int InsertBeg( ListNode **head, int info )
{
ListNode *new_node = CreateNode( info );
int success = new_node != NULL;
if ( success )
{
new_node->next = *head;
*head = new_node;
}
return success;
}
int main( void )
{
ListNode *head = NULL;
if ( InsertBeg( &head, 10 ) )
{
puts( "New node is added." );
}
else
{
puts( "Error: not enough memory." );
}
}
程序輸出為
New node is added.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.