创建单链表
作者:
jzz2.0
,
2024-12-05 17:32:10
,
所有人可见
,
阅读 3
#include <stdio.h>
#include<malloc.h>
typedef struct Node {
int num;
struct Node *next;
} LinkNode;
void construct(LinkNode* &head) {
head = (LinkNode*)malloc(sizeof(LinkNode));
head->next = NULL;
LinkNode* p = head;
for (int i = 0; i < 10; i++) {
LinkNode* node = (LinkNode*)malloc(sizeof(LinkNode));
scanf("%d", &node->num);
node->next = NULL;
p->next = node;
p = node;
}
}
void print(LinkNode* head) {
LinkNode* p = head->next;
while (p != NULL) {
printf("%d ", p->num);
p = p->next;
}
printf("\n");
}
void delRepeat(LinkNode* head) {
LinkNode* p = head->next;
while (p != NULL && p->next != NULL) {
while (p->next != NULL && p->num == p->next->num) {
p->next = p->next->next;
}
p = p->next;
}
}
int main() {
LinkNode* L;
construct(L);
print(L);
delRepeat(L);
print(L);
return 0;
}