-
Notifications
You must be signed in to change notification settings - Fork 0
/
171860522沈天琪_ex11_1.cpp
88 lines (80 loc) · 1.13 KB
/
171860522沈天琪_ex11_1.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
82
83
84
85
86
87
88
/*
设计C/C++程序,对一个N个节点的单向链表中的一个int型数据成员求和(假设和非0,用非递归或递归函数实现求和功能均可。
除 main 函数外,需要实现链表的建立、输出、撤销和求和四个函数)。
*/
#define N 5
#include<iostream>
#include<cstdio>
using namespace std;
struct Node
{
int data;
Node *next;
};
Node *Creat()
{
Node * head = NULL, *tail = NULL, *p = NULL;
for (int i = 0; i < N; i++)
{
p = new Node;
cin >> p->data;
p->next = NULL;
if (head == NULL)
head = p;
else
tail->next = p;
tail = p;
}
return head;
}
int Sum(Node * head)
{
Node * p = head;
int s = 0;
while (p->next != NULL)
{
s += p->data;
p = p->next;
}
s += p->data;
return s;
}
int Print(Node * head)
{
Node * p = head;
if(p==NULL)
{
cout<<"NULL"<<endl;
return 0;
}
else
;
while (p->next != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << p->data << endl;
return 0;
}
void Delete_(Node *&head)
{
Node * p = head;
while (p->next != NULL)
{
head = p->next;
delete p;
p = head;
}
head = p->next;
delete p;
}
int main()
{
Node * p = Creat();
//Print(p);
cout << Sum(p) << endl;
Delete_(p);
//system("pause");
return 0;
}