题解 | #牛牛的链表添加节点#
牛牛的链表添加节点
https://www.nowcoder.com/practice/e33b79c2e15a41f9b541e73cd256124a
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node* next;
}node;
node* create(int data)
{
node* newnode=(node*)malloc(sizeof(node));
if(newnode==NULL) exit(-1);
newnode->data=data;
newnode->next=NULL;
return newnode;
}
void appenden(node** head,int data)
{
node* newnode=create(data);
if(*head==NULL)
{
*head=newnode;
return;
}
node* temp=*head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
void addlist(node* head)
{
node* temp=head;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
int main()
{
node* head=NULL;
int arr[10000]={0};
int n,adnum;
scanf("%d %d",&n,&adnum);
for(int i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
appenden(&head,arr[i]);
if(i==adnum) appenden(&head,adnum);
}
addlist(head);
return 0;
}
传音控股公司福利 344人发布
