链接列表节点bug无限循环节点和插入节点之间
当我创建第三个节点时,它使该节点的无限循环。我该怎么办? 并且请在插入节点的情况'b'插入代码在某个节点后面。链接列表节点bug无限循环节点和插入节点之间
part1 --------------------------------------------- -------------------------------------------------- ------------------------------
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
char p,j ;
int v;
struct node{
int val;
struct node *next;
};
struct node *head=NULL;
struct node *curr=NULL;
struct node *temp=NULL;
struct node *prev=NULL;
struct node *tail=NULL;
struct node *after=NULL;
part2 ------------ -------------------------------------------------- -------------------------------------------------- -------------
struct node *creatFirstNode(int val){
printf("\ncreating list with headnode as [%d]\n",val);
struct node *ptr=(struct node *)malloc(sizeof(struct node));
if(ptr==NULL){
printf("\nCreated Failed \n");
return NULL;
}
ptr->val=val;
ptr->next=NULL;
head=ptr;
curr=ptr;
return ptr;
}
part3 ----------------------------- -------------------------------------------------- ----------------------------------------------
main(){
int n,i;
struct node *A=(struct node *)malloc(sizeof(struct node));
struct node *B=(struct node *)malloc(sizeof(struct node));
struct node *C=(struct node *)malloc(sizeof(struct node));
struct node *new=(struct node *)malloc(sizeof(struct node));
struct node *addEnd=(struct node *)malloc(sizeof(struct node));
struct node *addAmong=(struct node *)malloc(sizeof(struct node));
printf("\n-------- Welcome to Linked List Program -----------\n\n");
do{
printf("\nAdd to 'h'ead or 't'ail or 'b'ehind value:");
scanf("%c",&p);
fflush(stdin);
part4 ------------------------------------------- -------------------------------------------------- --------------------------------
switch(p)
{
case 'h':
printf("Enter value node:");
scanf("%d",&v);
fflush(stdin);
if(head == NULL)
{
creatFirstNode(v);
fflush(stdin);
}
else
{
curr=head;
new->val=v;
new->next=NULL;
curr=new;
curr->next=head;
curr=curr->next;
("\ncreating list with headnode as [%d]\n",v);
head=new;
fflush(stdin);
}
//void printList();
curr=head;
printf("\n----Value in Liked list----\n");
while(curr!=NULL){
printf("[%d], ",curr->val);
curr=curr->next; //change current node
}
break;
part5 ---------- -------------------------------------------------- -------------------------------------------------- ---------------
case 't':
printf("Enter value node tail:");
scanf("%d",&v);
curr=head;
while(curr!=NULL){ //Seek for last node
tail=curr;
curr=curr->next; // shift to next node
}
addEnd->val=v;
addEnd->next=NULL;
tail->next=addEnd;
tail=new;
fflush(stdin);
//void printList();.
curr=head;
printf("\n----Value in Liked list----\n");
while(curr!=NULL){
printf("[%d], ",curr->val);
curr=curr->next; //change current node
}
break;
part6 --------------------------- -------------------------------------------------- ------------------------------------------------
case 'b':
printf("Enter value node behind:");
scanf("%d",&v);
fflush(stdin);
printf("Adding value [%d] in new node:",&v);
printf("Add new node behind the value:");
void printList();
break;
default:
printf("\n Invalid Input ");
getch();
}
}while(p != 'h' || p != 't' || p != 'b');
getch();
return 0;
}
例如
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node *next;
} Node;
Node *head, *tail;
Node *new_node(int val){
struct node *ptr = malloc(sizeof(*ptr));
if(ptr==NULL){
perror("malloc:");
printf("\nFailed to create a new node.\n");
return NULL;
}
ptr->val = val;
ptr->next = NULL;
return ptr;
}
Node *creatFirstNode(int val){
return head = tail = new_node(val);
}
void printList(void){
Node *np = head;
printf("\n----Value in Liked list----\n");
while(np){
printf("[%d], ", np->val);
np = np->next;
}
printf("NULL\n");
}
void freeList(void){
while(head){
Node *np = head->next;
free(head);
head = np;
}
tail = NULL;
}
int main(void){
char cmd =' ';
printf("\n-------- Welcome to Linked List Program -----------\n\n");
do {
int v = 0;
printf("\nAdd to 'h'ead or 't'ail or 'b'ehind value or 'p'rint or 'q'uit:");
scanf(" %c", &cmd);
switch(cmd){
case 'h':
printf("Enter value node head:");
scanf("%d", &v);
if(head == NULL){
creatFirstNode(v);
} else {
Node *np = new_node(v);
np->next = head;
head = np;
}
break;
case 't':
printf("Enter value node tail:");
scanf("%d", &v);
if(head == NULL){
creatFirstNode(v);
} else {
tail = tail->next = new_node(v);
}
break;
case 'b':
printf("Enter value node behind:");
scanf("%d", &v);
printf("\nUnimplemented yet.\n");
break;
case 'p':
printList();
break;
case 'q':
freeList();
printf("\nBye!\n");
break;
default:
printf("\n Invalid Input ");
}
}while(cmd != 'q');
return 0;
}
感谢您的代码。没有更多的意外循环! 剩下1个问题:我想要'b'的情况下添加节点并插入我想要的任何节点后面 thx –
'case'b':'自己动手。 – BLUEPIXY
呃...好Thx非常多兄弟! :)) –
我想你也应该声明结构节点*未来= NULL; struct node * before = NULL; :)没有这些声明代码看起来不完整:) –
哪里有节点A,B和C使用?:) –
1)分配每个节点。 – BLUEPIXY