File tree Expand file tree Collapse file tree 4 files changed +145
-5
lines changed Expand file tree Collapse file tree 4 files changed +145
-5
lines changed Original file line number Diff line number Diff line change
1
+ * .o
2
+ * .exe
3
+ makefile
Original file line number Diff line number Diff line change 3
3
4
4
template <class T >
5
5
SLLst<T>::~SLLst () {
6
- for (SLLstNode * p;!isEmpty ();) {
6
+ for (SLLstNode<T> * p;!isEmpty ();) {
7
7
p=head->next ;
8
- delet head;
8
+ delete head;
9
9
head=p;
10
10
}
11
11
}
12
12
template <class T >
13
- SLLst<T>::addToHead(const T & el) {
14
- head=new SLLstNode (el,head);
13
+ void SLLst<T>::addToHead(const T & el) {
14
+ head=new SLLstNode<T> (el,head);
15
15
if (tail == 0 )
16
16
tail=head;
17
17
}
18
18
template <class T >
19
+ void SLLst<T>::addToTail (const T & el){
20
+ if (tail!=0 ){
21
+ tail->next =new SLLstNode<T>(el,0 );
22
+ tail=tail->next ;
23
+ }
24
+ else
25
+ head=tail=new SLLstNode<T>(el);
26
+ }
27
+ template <class T >
28
+ T SLLst<T>::deletFromHead() {
29
+ T el= head->info ;
30
+ SLLstNode<T> * tmp =head;
31
+ if (head==tail)
32
+ head=tail=0 ;
33
+ else
34
+ head=head->next ;
35
+ delete tmp;
36
+ return el;
37
+ }
38
+ template <class T >
19
39
T SLLst<T>::deletFromTail() {
20
40
T el = tail->info ;
21
41
if (head == tail) {
22
42
delete head;
23
43
head =tail =0 ;
24
44
}
25
45
else {
26
- SLLstNode * tmp;
46
+ SLLstNode<T> * tmp;
27
47
for (tmp = head;tmp->next != tail ; tmp=tmp->next ) ;
28
48
delete tail;
29
49
tail =tmp;
30
50
tail->next = 0 ;
31
51
}
32
52
return el;
53
+ }
54
+ template <class T >
55
+ bool SLLst<T>::isInList(const T & el) const {
56
+ for (SLLstNode<T> * tmp = head;tmp!=tail && !(tmp->info ==el);tmp=tmp->next ) ;
57
+ return tmp !=0 ;
33
58
}
Original file line number Diff line number Diff line change
1
+ #ifndef SLLINKED_LIST
2
+ #define SLLINKED_LIST
3
+
4
+ template <class T >
5
+ class SLLstNode {
6
+ public:
7
+ T info;
8
+ SLLstNode * next;
9
+ SLLstNode (const T & el,SLLstNode * n=0 ) {
10
+ info = el ;next=n;
11
+ }
12
+ };
13
+
14
+ template <class T >
15
+ class SLLst
16
+ {
17
+ public:
18
+ SLLst () {
19
+ head=tail=0 ;
20
+ }
21
+ ~SLLst ();
22
+ int isEmpty () {
23
+ return head==0 ;
24
+ }
25
+ void addToHead (const T &);
26
+ void addToTail (const T &);
27
+ T deletFromHead ();
28
+ T deletFromTail ();
29
+ bool isInList (const T &) const ;
30
+ private:
31
+ SLLstNode<T> * head ,* tail;
32
+ /* data */
33
+ };
34
+ template <class T >
35
+ SLLst<T>::~SLLst () {
36
+ for (SLLstNode<T> * p;!isEmpty ();) {
37
+ p=head->next ;
38
+ delete head;
39
+ head=p;
40
+ }
41
+ }
42
+ template <class T >
43
+ void SLLst<T>::addToHead(const T & el) {
44
+ head=new SLLstNode<T>(el,head);
45
+ if (tail == 0 )
46
+ tail=head;
47
+ }
48
+ template <class T >
49
+ void SLLst<T>::addToTail (const T & el){
50
+ if (tail!=0 ){
51
+ tail->next =new SLLstNode<T>(el,0 );
52
+ tail=tail->next ;
53
+ }
54
+ else
55
+ head=tail=new SLLstNode<T>(el);
56
+ }
57
+ template <class T >
58
+ T SLLst<T>::deletFromHead() {
59
+ T el= head->info ;
60
+ SLLstNode<T> * tmp =head;
61
+ if (head==tail)
62
+ head=tail=0 ;
63
+ else
64
+ head=head->next ;
65
+ delete tmp;
66
+ return el;
67
+ }
68
+ template <class T >
69
+ T SLLst<T>::deletFromTail() {
70
+ T el = tail->info ;
71
+ if (head == tail) {
72
+ delete head;
73
+ head =tail =0 ;
74
+ }
75
+ else {
76
+ SLLstNode<T> * tmp;
77
+ for (tmp = head;tmp->next != tail ; tmp=tmp->next ) ;
78
+ delete tail;
79
+ tail =tmp;
80
+ tail->next = 0 ;
81
+ }
82
+ return el;
83
+ }
84
+ template <class T >
85
+ bool SLLst<T>::isInList(const T & el) const {
86
+ SLLstNode<T> * tmp;
87
+ for (tmp = head;tmp!=tail && !(tmp->info ==el);tmp=tmp->next ) ;
88
+ return tmp != 0 ;
89
+ }
90
+ #endif
Original file line number Diff line number Diff line change
1
+ #include " SLLst.h"
2
+ #include < iostream>
3
+
4
+ int main ()
5
+ {
6
+ using namespace std ;
7
+ SLLst<int > aint;
8
+ int a;
9
+ cout<<" Please input aint:" <<endl;
10
+ cin>>a;
11
+ aint.addToHead (a);
12
+ while (a!=0 ) {
13
+ cout<<" Please input aint:" <<endl;
14
+ cin>>a;
15
+ aint.addToHead (a);
16
+ }
17
+
18
+ cout<<" The info in Head is : " <<endl;
19
+ cout<<aint.deletFromTail ()<<endl;
20
+ while (1 );
21
+ return 0 ;
22
+ }
You can’t perform that action at this time.
0 commit comments