当我创建第三个节点时,它创建了该节点的无限循环。我该怎么办? 并请在以下情况下插入代码' b'用于在某个节点后面插入节点。
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;
2部分---------------------------------------------- -------------------------------------------------- -----------------------------
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;
}
3部分---------------------------------------------- -------------------------------------------------- -----------------------------
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);
第4部分---------------------------------------------- -------------------------------------------------- -----------------------------
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;
6部分---------------------------------------------- -------------------------------------------------- -----------------------------
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;
}
答案 0 :(得分: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;
}