C 链表节点错误无限循环的节点和插入之间的节点
当我创建第三个节点时,它创建了该节点的无限循环。我该怎么办? 请在某个节点后面插入节点的“b”格中插入代码 第一部分-----------------------------------------------------------------------------------------------------------------------------C 链表节点错误无限循环的节点和插入之间的节点,c,linked-list,C,Linked List,当我创建第三个节点时,它创建了该节点的无限循环。我该怎么办? 请在某个节点后面插入节点的“b”格中插入代码 第一部分----------------------------------------------------------------------------------------------------------------------------- #include<stdio.h> #include<stdlib.h> #include<stdb
#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;
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;
}
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);
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;
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;
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>
#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;
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;
}
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);
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;
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;
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>
#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;
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;
}
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);
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;
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;
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>
#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;
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;
}
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);
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;
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;
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>
#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;
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;
}
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);
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;
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;
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;
}
#包括
#包括
类型定义结构节点{
int-val;
结构节点*下一步;
}节点;
节点*头,*尾;
节点*新节点(int val){
结构节点*ptr=malloc(sizeof(*ptr));
如果(ptr==NULL){
佩罗尔(“马洛克:”);
printf(“\n未能创建新节点。\n”);
返回NULL;
}
ptr->val=val;
ptr->next=NULL;
返回ptr;
}
节点*创建第一个节点(int val){
返回头=尾=新节点(val);
}
作废打印列表(作废){
节点*np=头部;
printf(“\n----喜欢列表中的值----\n”);
while(np){
printf(“[%d],”,np->val);
np=np->next;
}
printf(“NULL\n”);
}
void自由列表(void){
while(head){
节点*np=头部->下一步;
自由(头);
水头=np;
}
tail=NULL;
}
内部主(空){
char cmd='';
printf(“\n------欢迎使用链表程序-----------\n\n”);
做{
int v=0;
printf(“\n添加到'h'头或't'尾或'b'尾值或'p'打印或'q'uit:”);
scanf(“%c”和&cmd);
开关(cmd){
案例“h”:
printf(“输入值节点头:”);
scanf(“%d”和“&v”);
if(head==NULL){
第一节点(v);
}否则{
Node*np=新的_节点(v);
np->next=头部;
水头=np;
}
打破
案例“t”:
printf(“输入值节点尾部:”);
scanf(“%d”和“&v”);
if(head==NULL){
第一节点(v);
}否则{
tail=tail->next=new_节点(v);
}
打破
案例“b”:
printf(“在后面输入值节点:”);
scanf(“%d”和“&v”);
printf(“\n尚未实现。\n”);
打破
案例“p”:
打印列表();
打破
案例‘q’:
自由列表();
printf(“\n字节!\n”);
打破
违约:
printf(“\n无效输入”);
}
}while(cmd!=“q”);
返回0;
}
我认为您还应该声明struct node*next=NULL;结构节点*before=NULL;:)如果没有这些声明,代码看起来不完整。:)哪里使用了节点A、B和C?:)1)分配每个节点。不要将其用作变量名,即使您使用的是C编译器,这也不是一个好的做法BTWp!='h'| | p!='t'| | p!='b'
始终正确。感谢您的代码。不再是一个意外的循环!还有一个问题:我希望案例“b”添加节点并插入到我想要的任何节点后面。案例“b”:
自己动手。嗯。。。好的,非常感谢兄弟!!:)当(np->val!=s){prev=np;np=np->next;}prev->next=new_节点时,请执行一些错误;新建节点->下一个=np->下一个;}@我认为这显然是错误的。我认为你应该和我一起问新问题。