-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.c
55 lines (49 loc) · 1 KB
/
linkedlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// Created by ge on 2024/1/1.
//
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node* next;
}Node;
typedef struct {
Node *head;
Node *tail;
}LinkedList;
LinkedList *createList(){
LinkedList *list = malloc(sizeof(LinkedList));
list->head = NULL;
list->tail = NULL;
return list;
}
void insertData(LinkedList* list, int data){
Node *node = malloc(sizeof(Node));
node->data = data;
node->next = NULL;
if (list->head == NULL) {
list->head = node;
list->tail = node;
} else{
list->tail->next = node;
list->tail = node;
}
}
void printList(LinkedList* list){
Node *p = list->head;
while (p != NULL){
printf("%d -> ", p->data);
p = p->next;
}
printf("NULL\n");
}
int main()
{
LinkedList *list = createList();
insertData(list,11);
insertData(list,12);
insertData(list,13);
insertData(list,15);
insertData(list,14);
printList(list);
}