本文介绍了如何在链表中插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<pre>
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *createNode(){
struct node *ptr,*head;
ptr= (struct node *)malloc(sizeof(struct node));
ptr->next = NULL;
}
struct node *input(){
int num;
struct node *p,*head,*temp;
for(int i=0;i<5;i++){
scanf("%d",&num);
temp=createNode();
temp->data=num;
if(head == NULL){
p=head=temp;
}
else{
p=temp;
}
}
return head;
}
struct node *insertNode(int value){
struct node *p,*head,*neww;
neww = createNode();
neww ->data=value;
if(head==NULL){
head = neww;
}
else{
while(p!=NULL){
p->next=p;
p = neww;
}
}
}
void display(struct node * head){
struct node * ptr=head;
while(ptr!=NULL){
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
void main(){
struct node * head = input();
insertNode(3);
display(head);
}
我的尝试:
我写了这段代码,这有什么问题?
What I have tried:
I have written this code ,what is wrong with this?
推荐答案
这篇关于如何在链表中插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!