调整以下代码删除节点:#include<stdio.h> #include<stdlib.h> typedef struct Node{ int data; Node* next; }Node; Node* createlist(){ Node* head=(Node*)malloc(sizeof(Node)); head->next=NULL; return head; } Node* createnode(int data){ Node* newnode=(Node*)malloc(sizeof(Node)); newnode->data=data; newnode->next=NULL; return newnode; } void insertnode(Node* head,int data){ Node* newnode=createnode(data); newnode->next=head->next; head->next=newnode; } void deletenode(Node* head){ Node* newnode=head->next; head->next=newnode->next->next; free(newnode); } void printlist(Node* head){ Node* p=head->next; while(p){ printf("%d",p->data); p=p->next; } printf("\n"); } int main() { Node* list=createlist(); insertnode(list,1); insertnode(list,3); insertnode(list,5); printlist(list); printf("\n"); deletenode(list); printlist(list); return 0; }
时间: 2024-01-05 09:02:27 浏览: 81
以下是已经删除节点的代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int data;
Node* next;
}Node;
Node* createlist(){
Node* head=(Node*)malloc(sizeof(Node));
head->next=NULL;
return head;
}
Node* createnode(int data){
Node* newnode=(Node*)malloc(sizeof(Node));
newnode->data=data;
newnode->next=NULL;
return newnode;
}
void insertnode(Node* head,int data){
Node* newnode=createnode(data);
newnode->next=head->next;
head->next=newnode;
}
void deletenode(Node* head){
Node* newnode=head->next;
head->next=newnode->next;
free(newnode);
}
void printlist(Node* head){
Node* p=head->next;
while(p){
printf("%d",p->data);
p=p->next;
}
printf("\n");
}
int main() {
Node* list=createlist();
insertnode(list,1);
insertnode(list,3);
insertnode(list,5);
printlist(list);
printf("\n");
deletenode(list);
printlist(list);
return 0;
}
阅读全文