-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodinter2.1c++.cpp
81 lines (72 loc) · 1.43 KB
/
codinter2.1c++.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef struct node
{
int data;
struct node * next;
}node ;
typedef struct linkedlist
{
node *head;
}linkedlist;
void dupdelete(linkedlist l)
{
node* h=l.head->next,* pre=l.head->next,* p=pre->next;
while(h->next!=NULL)
{
while(p!=NULL)
{
if(p->data==h->data)
{
node * temp=p;
pre->next=p->next;
free(temp);
p=pre->next;
}
else
{
pre=p;
p=p->next;
}
}
h=h->next;
pre=h;
p=h->next;
}
}
int main()
{
linkedlist l;//思考:如何create一个链表?
l.head=(node *)malloc(sizeof(node));
l.head->next=NULL;
node *p=l.head;//两个空指针之间互相赋值是没有意义的,因此要为head指针初始化申请空间
for (int i=1;i<10;i++)
{
for(int j=0;j<i;j++)
{
node *temp=(node*)malloc(sizeof(struct node));
temp->data=j;
temp->next=NULL;
p->next=temp;
p=p->next;
}
}
p=l.head->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
printf("\n");
dupdelete(l);
p=l.head->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
getchar();
getchar();
}