- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在制作一个通过节点字段从 X 点移动到 Y 点的程序的过程中。我是 C 新手,我的代码中存在错误问题。我请求你帮助修复这些(很可能是愚蠢的错误),这样我就有希望让它正常运行。任何建议将不胜感激!
这是节点字段 http://i1350.photobucket.com/albums/p771/Clarkyy4/Untitled_zps9bbda929.png
给出要解决的确切问题项目:找到网络中任意两个节点之间的最短网络路径。*输入上述网络中的源节点。*输入网络中的目标节点。* 找到从源到目的地的最短路径并显示它。即输入源节点:E输入源节点:H最短路径:E - F - G - H(3 跳) (最短路径可能不止一条,找到至少一条)您必须使用我们在类里面讨论过的 ADT 之一(我建议使用queuesADT 或linkListADT)。
这里是 linkListADT.h
/* This header file contains the functions to maintain
and process a linked list.
Written by:
Date:
*/
//#include "P5-01.h" /* Singly-Linked List ADT Type Definitions */
// List ADT Type Defintions
typedef struct node
{
void* dataPtr;
struct node* link;
} NODE;
typedef struct
{
int count;
NODE* pos;
NODE* head;
NODE* rear;
int (*compare) (void* argu1, void* argu2);
} LIST;
//#include "P5-02.h" /* List ADT Prototype Declarations */
// Prototype Declarations
LIST* createList (int (*compare)
(void* argu1, void* argu2));
LIST* destroyList (LIST* list);
int addNode (LIST* pList, void* dataInPtr);
bool removeNode (LIST* pList,
void* keyPtr,
void** dataOutPtr);
bool searchList (LIST* pList,
void* pArgu,
void** pDataOut);
bool retrieveNode (LIST* pList,
void* pArgu,
void** dataOutPtr);
bool traverse (LIST* pList,
int fromWhere,
void** dataOutPtr);
int listCount (LIST* pList);
bool emptyList (LIST* pList);
bool fullList (LIST* pList);
static bool _insert1 (LIST* pList,
NODE* pPre,
void* dataInPtr);
static void _delete (LIST* pList,
NODE* pPre,
NODE* pLoc,
void** dataOutPtr);
static bool _search (LIST* pList,
NODE** pPre,
NODE** pLoc,
void* pArgu);
// End of List ADT Definitions
//#include "P5-03.h" /* Create linked list */
/* =============== createList ==============
Allocates dynamic memory for a list head
node and returns its address to caller
Pre compare is address of compare function
used to compare two nodes.
Post head has allocated or error returned
Return head node pointer or null if overflow
*/
LIST* createList
(int (*compare) (void* argu1, void* argu2))
{
// Local Definitions
LIST* list;
// Statements
list = (LIST*) malloc (sizeof (LIST));
if (list)
{
list->head = NULL;
list->pos = NULL;
list->rear = NULL;
list->count = 0;
list->compare = compare;
} // if
return list;
} // createList
//#include "P5-04.h" /* Add Node */
/* ================== addNode =================
Inserts data into list.
Pre pList is pointer to valid list
dataInPtr pointer to insertion data
Post data inserted or error
Return -1 if overflow
0 if successful
1 if dupe key
*/
int addNode (LIST* pList, void* dataInPtr)
{
// Local Definitions
bool found;
bool success;
NODE* pPre;
NODE* pLoc;
// Statements
found = _search (pList, &pPre, &pLoc, dataInPtr);
if (found)
// Duplicate keys not allowed
return (+1);
success = _insert1 (pList, pPre, dataInPtr);
if (!success)
// Overflow
return (-1);
return (0);
} // addNode
//#include "P5-05.h" /* Insert Node */
/* =================== _insert1 ==================
Inserts data pointer into a new node.
Pre pList pointer to a valid list
pPre pointer to data's predecessor
dataInPtr data pointer to be inserted
Post data have been inserted in sequence
Return boolean, true if successful,
false if memory overflow
*/
static bool _insert1 (LIST* pList, NODE* pPre,
void* dataInPtr)
{
// Local Definitions
NODE* pNew;
// Statements
if (!(pNew = (NODE*) malloc(sizeof(NODE))))
return false;
pNew->dataPtr = dataInPtr;
pNew->link = NULL;
if (pPre == NULL)
{
// Adding before first node or to empty list.
pNew->link = pList->head;
pList->head = pNew;
if (pList->count == 0)
// Adding to empty list. Set rear
pList->rear = pNew;
} // if pPre
else
{
// Adding in middle or at end
pNew->link = pPre->link;
pPre->link = pNew;
// Now check for add at end of list
if (pNew->link == NULL)
pList->rear = pNew;
} // if else
(pList->count)++;
return true;
} // _insert1
//#include "P5-06.h" /* Remove Node */
/* ================= removeNode ================
Removes data from list.
Pre pList pointer to a valid list
keyPtr pointer to key to be deleted
dataOutPtr pointer to data pointer
Post Node deleted or error returned.
Return false not found; true deleted
*/
bool removeNode (LIST* pList, void* keyPtr,
void** dataOutPtr)
{
// Local Definitions
bool found;
NODE* pPre;
NODE* pLoc;
// Statements
found = _search (pList, &pPre, &pLoc, keyPtr);
if (found)
_delete (pList, pPre, pLoc, dataOutPtr);
return found;
} // removeNode
//#include "P5-07.h" /* Delete Node */
/* ================= _delete ================
Deletes data from a list and returns
pointer to data to calling module.
Pre pList pointer to valid list.
pPre pointer to predecessor node
pLoc pointer to target node
dataOutPtr pointer to data pointer
Post Data have been deleted and returned
Data memory has been freed
*/
void _delete (LIST* pList, NODE* pPre,
NODE* pLoc, void** dataOutPtr)
{
// Statements
*dataOutPtr = pLoc->dataPtr;
if (pPre == NULL)
// Deleting first node
pList->head = pLoc->link;
else
// Deleting any other node
pPre->link = pLoc->link;
// Test for deleting last node
if (pLoc->link == NULL)
pList->rear = pPre;
(pList->count)--;
free (pLoc);
return;
} // _delete
//#include "P5-08.h" /* Search Interface */
/* ================== searchList =================
Interface to search function.
Pre pList pointer to initialized list.
pArgu pointer to key being sought
Post pDataOut contains pointer to found data
-or- NULL if not found
Return boolean true successful; false not found
*/
bool searchList (LIST* pList, void* pArgu,
void** pDataOut)
{
// Local Definitions
bool found;
NODE* pPre;
NODE* pLoc;
// Statements
found = _search (pList, &pPre, &pLoc, pArgu);
if (found)
*pDataOut = pLoc->dataPtr;
else
*pDataOut = NULL;
return found;
} // searchList
//#include "P5-09.h" /* Search List */
/* ================== _search =================
Searches list and passes back address of node
containing target and its logical predecessor.
Pre pList pointer to initialized list
pPre pointer variable to predecessor
pLoc pointer variable to receive node
pArgu pointer to key being sought
Post pLoc points to first equal/greater key
-or- null if target > key of last node
pPre points to largest node < key
-or- null if target < key of first node
Return boolean true found; false not found
*/
bool _search (LIST* pList, NODE** pPre,
NODE** pLoc, void* pArgu)
{
// Macro Definition
#define COMPARE \
( ((* pList->compare) (pArgu, (*pLoc)->dataPtr)) )
#define COMPARE_LAST \
((* pList->compare) (pArgu, pList->rear->dataPtr))
// Local Definitions
int result;
// Statements
*pPre = NULL;
*pLoc = pList->head;
if (pList->count == 0)
return false;
// Test for argument > last node in list
if ( COMPARE_LAST > 0)
{
*pPre = pList->rear;
*pLoc = NULL;
return false;
} // if
while ( (result = COMPARE) > 0 )
{
// Have not found search argument location
*pPre = *pLoc;
*pLoc = (*pLoc)->link;
} // while
if (result == 0)
// argument found--success
return true;
else
return false;
} // _search
//#include "P5-10.h" /* Retrieve Node */
/* ================== retrieveNode ================
This algorithm retrieves data in the list without
changing the list contents.
Pre pList pointer to initialized list.
pArgu pointer to key to be retrieved
Post Data (pointer) passed back to caller
Return boolean true success; false underflow
*/
static bool retrieveNode (LIST* pList,
void* pArgu,
void** dataOutPtr)
{
// Local Definitions
bool found;
NODE* pPre;
NODE* pLoc;
// Statements
found = _search (pList, &pPre, &pLoc, pArgu);
if (found)
{
*dataOutPtr = pLoc->dataPtr;
return true;
} // if
*dataOutPtr = NULL;
return false;
} // retrieveNode
//#include "P5-11.h" /* Empty List */
/* ================= emptyList ================
Returns boolean indicating whether or not the
list is empty
Pre pList is a pointer to a valid list
Return boolean true empty; false list has data
*/
bool emptyList (LIST* pList)
{
// Statements
return (pList->count == 0);
} // emptyList
//#include "P5-12.h" /* Full List */
/* ================== fullList =================
Returns boolean indicating no room for more data.
This list is full if memory cannot be allocated for
another node.
Pre pList pointer to valid list
Return boolean true if full
false if room for node
*/
bool fullList (LIST* pList)
{
// Local Definitions
NODE* temp;
// Statements
if ((temp = (NODE*)malloc(sizeof(*(pList->head)))))
{
free (temp);
return false;
} // if
// Dynamic memory full
return true;
} // fullList
//#include "P5-13.h" /* List Count */
/* ================== listCount ==================
Returns number of nodes in list.
Pre pList is a pointer to a valid list
Return count for number of nodes in list
*/
int listCount(LIST* pList)
{
// Statements
return pList->count;
} // listCount
//#include "P5-14.h" /* Traverse List */
/* ================== traverse =================
Traverses a list. Each call either starts at the
beginning of list or returns the location of the
next element in the list.
Pre pList pointer to a valid list
fromWhere 0 to start at first element
dataPtrOut address of pointer to data
Post if more data, address of next node
Return true node located; false if end of list
*/
bool traverse (LIST* pList,
int fromWhere,
void** dataPtrOut)
{
// Statements
if (pList->count == 0)
return false;
if (fromWhere == 0)
{
//Start from first node
pList->pos = pList->head;
*dataPtrOut = pList->pos->dataPtr;
return true;
} // if fromwhere
else
{
// Start from current position
if (pList->pos->link == NULL)
return false;
else
{
pList->pos = pList->pos->link;
*dataPtrOut = pList->pos->dataPtr;
return true;
} // if else
} // if fromwhere else
} // traverse
//#include "P5-15.h" /* Destroy List */
/* ================== destroyList =================
Deletes all data in list and recycles memory
Pre List is a pointer to a valid list.
Post All data and head structure deleted
Return null head pointer
*/
LIST* destroyList (LIST* pList)
{
// Local Definitions
NODE* deletePtr;
// Statements
if (pList)
{
while (pList->count > 0)
{
// First delete data
free (pList->head->dataPtr);
// Now delete node
deletePtr = pList->head;
pList->head = pList->head->link;
pList->count--;
free (deletePtr);
} // while
free (pList);
} // if
return NULL;
} // destroyList
主要代码
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "linkListADT.h"
typedef struct
{
void* dataPtr;
struct node* link;
} NODE;
typedef struct
{
int count;
NODE* pos;
NODE* head;
NODE* rear;
} DATA;
int _tmain(int argc, _TCHAR* argv[])
{
// empty list
node *head = NULL;
// create a temporary
node *temp;
temp = (node*)malloc(sizeof(node)); // allocate
// place info to first node
temp->data->head = 'A';
temp->data->pos = 1;
temp->data->rear = NULL;
// get address of head
temp->next=head;
head = temp;
}
{
char cnode = 'A','B','C','D','E','F','G','H','I','J';
int next;
int cnode;
printf(cnode);
switch(cnode);
{
case 'A';
next = rand()%3;
switch(next);
{
case 0: cnode = 'E'; break;
case 1: cnode = 'F'; break;
case 2: cnode = 'B'; break;
}
case 'B';
next = rand()%3;
switch(next);
{
case 0: cnode = 'A'; break;
case 1: cnode = 'C'; break;
case 2: cnode = 'D'; break;
}
case 'C';
next = rand()%3;
switch(next);
{
case 0: cnode = 'B'; break;
case 1: cnode = 'F'; break;
case 2: cnode = 'G'; break;
}
case 'D';
next = rand()%3;
switch(next)
{
case 0: cnode = 'B'; break;
case 1: cnode = 'G'; break;
case 2: cnode = 'H'; break;
}
case 'E';
next = rand()%3;
switch(next)
{
case 0: cnode = 'B'; break;
case 1: cnode = 'G'; break;
case 2: cnode = 'H'; break;
}
case 'F';
next = rand()%6;
switch(next)
{
case 0: cnode = 'C'; break;
case 1: cnode = 'G'; break;
case 2: cnode = 'E'; break;
case 3: cnode = 'A'; break;
case 4: cnode = 'J'; break;
case 5: cnode = 'K'; break;
}
case 'G';
next = rand()%4;
switch(next)
{
case 0: cnode = 'D'; break;
case 1: cnode = 'F'; break;
case 2: cnode = 'H'; break;
case 3: cnode = 'C': break;
}
case 'H';
next = rand()%4;
switch(next)
{
case 0: cnode = 'D'; break;
case 1: cnode = 'G'; break;
case 2: cnode = 'L'; break;
case 3: cnode = 'K': break;
}
case 'I';
next = rand()%2;
switch(next)
{
case 0: cnode = 'E'; break;
case 1: cnode = 'J'; break;
}
case 'J';
next = rand()%3;
switch(next)
{
case 0: cnode = 'F'; break;
case 1: cnode = 'I'; break;
case 2: cnode = 'K'; break;
}
case 'K';
next = rand()%4;
switch(next)
{
case 0: cnode = 'H'; break;
case 1: cnode = 'L'; break;
case 2: cnode = 'F'; break;
case 3: cnode = 'J'; break;
}
case 'L';
next = rand()%2;
switch(next)
{
case 0: cnode = 'H'; break;
case 1: cnode = 'K'; break;
}
}
}
}
return 0;
}
最佳答案
NODE 中的成员名称是 dataPtr,这就是您收到错误的原因:类“node”没有成员“data”编译时还遇到哪些其他错误?
关于c - 在 C 中移动节点字段点对点编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16326461/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!