## 如何在C中实现单链表内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (1)
• 关注 (0)
• 查看 (7)

``````#include<stdio.h>
#include <stdlib.h>

typedef struct{
int data;
struct node *next;
}node;

int main(){
node *head = NULL;
head =  malloc(sizeof(node));
if(head==NULL){
printf("ta foirer quelque chose frero!");
return 1;
}
(*head).data=3;
(*head).next=NULL;

node *p = NULL;

p = (node*) head->next;
p =  malloc(sizeof(node));
p->data = 5;

p->next = NULL;
p= (node *)p->next;

int i=0;
while(i<5){
p =  malloc(sizeof(node));
i++;
p->data = i;
p->next=NULL;
p= (node *)p->next;
}

p = head;

while(p){
printf("\n%d",p->data);
p =(node*) p->next;
}

return 0;
}
``````

``````3
``````

``````3
5
0
1
2
3
4
``````

``````#include<stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node *next;
};

typedef struct Node node;

void insert(node* h, int v) {
node* tmp = h;

while(tmp->next)
tmp = tmp->next;
node* newnode = malloc(sizeof(node));
newnode->data = v;
newnode->next = NULL;
tmp->next = newnode;
}

int main(){
node *head = NULL;
head =  malloc(sizeof(node));
if(head==NULL){
printf("ta foirer quelque chose frero!");
return 1;
}
head->data=3;
head->next = NULL;

node *p = NULL;

insert(head, 5);
int i=0;
while(i<5){
insert(head, i++);
}

p = head;

while(p){
printf("%d\n",p->data);
p = p->next;
}

return 0;
}
``````

Tina

Go Serverless!

### 是否有语音转发功能？

software微软、谷歌、腾讯、阿里、百度……