gpt4 book ai didi

c - 单链路链路交换节点崩溃

转载 作者:行者123 更新时间:2023-11-30 18:13:51 26 4
gpt4 key购买 nike

刚学完单链表,开始做练习。

对于这个,我试图让程序交换两个相邻的节点。当我没有选择正确交换的头或最后一个节点时,我的问题是当我尝试交换头时。它与下一个节点交换了(这是正确的),但下一个节点似乎没有到达头部。

下面是完整的代码,但我认为这里唯一关心的是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com