作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
刚学完单链表,开始做练习。
对于这个,我试图让程序交换两个相邻的节点。当我没有选择正确交换的头或最后一个节点时,我的问题是当我尝试交换头时。它与下一个节点交换了(这是正确的),但下一个节点似乎没有到达头部。
下面是完整的代码,但我认为这里唯一关心的是 VOID SWAP 过程。刚刚发布了整个代码,以防有人索要整个代码。
typedef struct node{
char element [4];
struct node * next;
}nodeType;
nodeType * push(nodeType *early){
nodeType *nptr = (nodeType*)malloc(sizeof(nodeType)) ;
printf("Enter three character long word: ");
fflush(stdin);
gets(nptr->element);
if(early==NULL){
nptr->next = NULL;
}else{
nptr->next = early;
}
return nptr;
}
nodeType* searchNode(nodeType*startNode){
///get user input
char input[4];
int match;
nodeType * wnptr = NULL;
printf("Enter the word to swap: ");
fflush(stdin);
gets(input);
///search for the word
while(startNode!=NULL){
if (strcmp(input, startNode->element)==0){
if(startNode->next == NULL){
printf("Nothing to swap with.\n");
match = 1;
break;
} else {
wnptr = startNode;
match = 1;
break;
}
} else {
wnptr = NULL;
match = 0;
startNode = startNode->next;
}
}
if (match == 0){
printf("Match not found\n");
}
return wnptr;
}
nodeType * searchPrev(nodeType*startNode, nodeType*searchedNode){
nodeType * prevptr = NULL;
while(startNode!=NULL){
if (startNode->next==searchedNode){
prevptr = startNode;
break;
} else if (startNode->next!=searchedNode){
prevptr = NULL;
startNode = startNode->next;
}
}
return prevptr;
}
void swap(nodeType*newhead ,nodeType*prev, nodeType*searchedNode){
if(prev == NULL){
nodeType*adjNode = searchedNode->next;
searchedNode->next=adjNode->next;
adjNode->next = searchedNode;
} else {
nodeType*adjNode = searchedNode->next;
prev->next = adjNode;
searchedNode->next=adjNode->next;
adjNode->next = searchedNode;
}
}
void printList(nodeType*start){
nodeType*currentNode = start;
while(currentNode!=NULL){
printf("%s ", currentNode->element);
currentNode=currentNode->next;
}
printf("\n");
}
void cleanUp (nodeType*start){
nodeType*currentNode = start;
nodeType*holdadd = NULL;
while(currentNode!=NULL){
holdadd = currentNode->next;
free(currentNode);
currentNode=holdadd;
}
}
int main(){
char command[7];
char temp[4];
nodeType *head = NULL;
nodeType *swaptr = NULL;
nodeType *preptr = NULL;
while(printf("Type command to perform:\npush\nswap\nprint\nexit\nCommand: ")){
fflush(stdin);
scanf("%s", command);
if(strcmp(command, "exit")==0){
break;
}
if (strcmp(command, "push")==0){
head = push(head);
}
if (strcmp(command, "swap")==0){
swaptr = searchNode(head);
preptr = searchPrev(head, swaptr);
swap(head, preptr, swaptr);
}
if (strcmp(command, "print")==0){
printList(head);
printf("%u", head);
}
}
cleanUp(head);
}
最佳答案
void swap(nodeType*newhead ,nodeType*prev, nodeType*searchedNode){
if(prev == NULL){
nodeType*adjNode = searchedNode->next;
searchedNode->next=adjNode->next;
adjNode->next = searchedNode;
newhead= adjNode;
} else {
nodeType*adjNode = searchedNode->next;
prev->next = adjNode;
searchedNode->next=adjNode->next;
adjNode->next = searchedNode;
}
}
关于c - 单链路链路交换节点崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21793132/
对我来说遇到了一个棘手的问题......基于主干的路由 Controller 和期望的效果没有实现。 我需要仅根据 URL 的第一部分获取 URL,并根据任何一种可能性定向到路线。 JS routes
假设链路上同时存在弹性流量和非弹性流量(如图所示)。那么它们中的哪一个将主导链路(弹性或非弹性)。根据我的说法,这应该取决于如果非弹性流量作为弹性流量可以调整流量,那么流量可以根据它进行调整。所以任何
我是一名优秀的程序员,十分优秀!