Skip to content

Commit 1d1da46

Browse files
committed
add some algorithmes
1 parent 91e8c32 commit 1d1da46

File tree

4 files changed

+145
-5
lines changed

4 files changed

+145
-5
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.o
2+
*.exe
3+
makefile

SLLst.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,56 @@
33

44
template <class T>
55
SLLst<T>::~SLLst() {
6-
for (SLLstNode * p;!isEmpty();) {
6+
for (SLLstNode<T> * p;!isEmpty();) {
77
p=head->next;
8-
delet head;
8+
delete head;
99
head=p;
1010
}
1111
}
1212
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);
1515
if(tail == 0)
1616
tail=head;
1717
}
1818
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>
1939
T SLLst<T>::deletFromTail() {
2040
T el = tail->info;
2141
if (head == tail) {
2242
delete head;
2343
head =tail =0 ;
2444
}
2545
else {
26-
SLLstNode * tmp;
46+
SLLstNode<T> * tmp;
2747
for (tmp = head;tmp->next != tail ; tmp=tmp->next) ;
2848
delete tail;
2949
tail =tmp;
3050
tail->next = 0;
3151
}
3252
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;
3358
}

SLLst.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)