数据结构-1. 线性表、栈和队列

1. 线性表、栈和队列

1.1 链表

1.1.1 链表的实现
  1 class Node{
  2     public: 
  3     int value;
  4     Node *next;
  5     Node(const int nodeValue,Node *nextNode){
  6         value=nodeValue;
  7         next=nextNode;
  8     }
  9     Node(Node *nextNode){
 10         next=nextNode;
 11     }
 12 };
 13 
 14 class LinkList{
 15     private:
 16         Node *head;
 17         Node *tail;
 18         Node *fence;
 19         int leftcnt;
 20         int rightcnt;
 21        
 22         void init(){
 23             head=tail=fence=new Node(0,NULL);
 24             leftcnt=rightcnt=0;
 25         }
 26        
 27         void removeAll(){
 28              while(head!=0){
 29                  fence=head;
 30                  head=head->next;
 31                  delete fence;          
 32              }
 33         }
 34     public:
 35         LinkList() {
 36             init();
 37         }
 38         ~LinkList() {
 39             removeAll();
 40         }
 41         void clear() {
 42              removeAll();
 43              init();
 44         }
 45         bool insert(const int &item){
 46             fence->next=new Node(item,fence->next);
 47             if(tail==fence)
 48                 tail=fence->next;
 49             return true;
 50         }
 51         bool append(const int &item){
 52             tail=tail->next=new Node(item,NULL);
 53             rightcnt++;
 54             return true;
 55         }
 56         bool remove(int &it){
 57             if(fence->next==NULL)
 58                 return false;
 59             it=fence->next->value;
 60             Node *ltemp=fence->next;
 61             fence->next=ltemp->next;
 62             delete ltemp;
 63             if(tail==ltemp)
 64                 tail=fence;
 65             rightcnt--;
 66             return true;      
 67         }
 68         void setStart() {
 69              fence=head;
 70              rightcnt+=leftcnt;
 71              leftcnt=0;
 72         }
 73         void setEnd() {
 74              fence=tail;
 75              leftcnt+=rightcnt;
 76              rightcnt=0;
 77         }
 78         void prev(){
 79             Node *ltemp=head;
 80             if(fence==head) return;
 81             while(ltemp->next!=fence)
 82                 ltemp=ltemp->next;
 83             fence=ltemp;
 84             leftcnt--;
 85             rightcnt++;   
 86         }
 87         void next(){
 88             if(fence!=tail){
 89                 fence=fence->next;
 90                 leftcnt++;
 91                 rightcnt--;
 92             }               
 93         }
 94         int leftLength() const {
 95             return leftcnt;
 96         }
 97         int rightLength() const { return rightcnt; }
 98         bool setPos(int pos){
 99              if((pos<0)||(pos>(rightcnt+leftcnt)))
100                  return false;
101              fence=head;
102              for(int i=0;i<pos;i++)
103                  fence=fence->next;
104              return true;      
105         }
106         bool getValue(int & it) const{
107             if(rightLength()==0)
108                 return false;
109             else
110                 it=fence->next->value;
111             return true;   
112         }
113         void print() const{
114             Node *ltemp=head;
115             cout<<"<";
116             while(ltemp!=fence){
117                 cout<<ltemp->next->value<<" ";
118                 ltemp=ltemp->next;
119             }
120             cout<<"|";
121             while(ltemp->next!=NULL){
122                 cout<<ltemp->next->value<<" ";
123                 ltemp=ltemp->next;
124             }
125             cout<<">\n";
126         }
127 };
1.1.2 链表的应用-约瑟夫问题
 1 #include<iostream>
 2 using namespace std;
 3 
 4 struct Node
 5 {
 6     int value;
 7     Node *next;
 8 };
 9 
10 int main()
11 {
12     Node *head,*list,*tmp;
13     tmp=new Node;
14     tmp->value=1;
15     tmp->next=NULL;
16     head=list=tmp;
17     int total=0,outIndex=0,outcount=0,noout=0;
18     cout<<"Input totoal:";
19     cin>>total;
20     cout<<"Input out index:";
21     cin>>outIndex;
22    
23     for(int i=1;i<total;i++)
24     {
25         Node *temp=new Node;
26         temp->value=1;
27         temp->next=NULL;
28         list->next=temp;
29         list=list->next;
30     }
31     list->next=head;
32     list=head;
33     /*
34              Node *temp=list;
35              for(int i=0;i<total;i++)
36             {
37                 cout<<temp->value<<" ";
38                 temp=temp->next;
39             }
40      */      
41     while(1)
42     {
43         if(outcount==total-1)
44             break;
45            
46         if(noout==(outIndex-1))
47         {
48             if(list->value!=0)
49             {
50                 list->value=0;
51                 noout=0;
52                 outcount++;
53                 Node *temp=head;
54                  for(int i=0;i<=total;i++)
55                 {
56                     cout<<temp->value<<" ";
57                     temp=temp->next;
58                 }
59                 cout<<endl;
60             }
61         }
62         else
63         {
64             noout+=list->value;
65         }
66         list=list->next;
67       
68     }
69    
70     system("pause");
71     return 0;
72 }

1.2 栈

1.2.1 顺序栈
 1 class Stack{
 2     private:
 3         int size;
 4         int top;
 5         int *listArray;
 6     public:
 7        Stack(int sz){
 8             size=sz;
 9             top=0;
10             listArray=new int[sz];       
11         }
12        
13         void clear(){
14             top=0;
15         }
16        
17         bool push(const int &item){
18             if(top==size)
19                 return false;
20             listArray[top++]=item;
21             return true;   
22         }
23        
24         bool pop(int &it){
25             if(top==0)
26                 return false;
27             it=listArray[--top];
28             return true;   
29         }
30        
31         bool topValue(int &it) const{
32             if(top==0)
33                 return false;
34             it=listArray[top-1];
35             return true;   
36         }
37        
38         int length() const{
39             return top;
40         }
41 };
1.2.2 链式栈
 1 class Node{
 2     public: 
 3     int value;
 4     Node *next;
 5     Node(const int nodeValue,Node *nextNode){
 6         value=nodeValue;
 7         next=nextNode;
 8     }
 9     Node(Node *nextNode){
10         next=nextNode;
11     }
12 };
13 
14 class LinkStack{
15     private:
16         Node *top;
17         int size;
18     public:
19         LinkStack(int sz){
20             top=NULL;
21             size=0;
22         }
23        
24         void clear(){
25             while(top!=NULL){
26                 Node *tmp=top;
27                 top=top->next;
28                 delete tmp;
29             }
30             size=0;
31         }
32        
33         bool push(const int &item){
34             top=new Node(item,top);
35             size++;
36             return true;
37         }
38        
39         bool pop(int &it){
40             if(size==0)
41                 return false;
42             it=top->value;
43             Node *tmp=top;
44             top=top->next;
45             delete tmp;
46             size--;
47             return true;
48         }
49        
50         bool topValue(int &it){
51             if(size==0)
52                 return 0;
53             it=top->value;
54             return true;   
55         }
56        
57         int length() const{
58             return size;
59         }
60 };
1.2.3 栈的应用-迷宫问题
  1 #include <iostream>
  2 using namespace std;
  3 
  4 int maze[12][12]={    1, 1, 1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1,
  5                     1, 0, 1 ,1 ,1 ,1 ,1 ,0 ,0 ,0 ,0 ,1,
  6                     1, 0, 0 ,1 ,0 ,0 ,1 ,0 ,1 ,0 ,0 ,1,
  7                     1, 0, 0 ,0 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,1,
  8                     1, 0, 1 ,0 ,1 ,0 ,1 ,0 ,1 ,1 ,0 ,1,
  9                     1, 0, 1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,0 ,1,
 10                     1, 0, 1 ,1 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,1,
 11                     1, 0, 1 ,0 ,0 ,0 ,1 ,0 ,1 ,0 ,1 ,1,
 12                     1, 0, 1 ,0 ,1 ,1 ,1 ,0 ,1 ,0 ,0 ,1,
 13                     1, 0, 0 ,0 ,0 ,0 ,0 ,0 ,1 ,0 ,0 ,1,
 14                     1, 0, 0 ,0 ,0 ,1 ,1 ,1 ,1 ,0 ,0 ,1,
 15                     1, 1, 1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1
 16             };
 17 
 18 struct Position{
 19     int row;
 20     int col;
 21 };
 22 
 23 bool findPath(){
 24     LinkStack *path=new LinkStack();   
 25     Position offset[4];
 26     offset[0].row=0;  offset[0].col=1;
 27     offset[1].row=1;  offset[1].col=0;
 28     offset[2].row=0;  offset[2].col=-1;
 29     offset[3].row=-1; offset[3].col=0;
 30    
 31     Position cur;
 32     cur.row=1;
 33     cur.col=1;
 34     maze[1][1]=6;
 35    
 36     int option=0,lastOption=3;
 37     while(cur.row!=10||cur.col!=10){
 38         int r,c;
 39         bool isNode=false;
 40        
 41         option=0;
 42         while(option<=lastOption){
 43             r=cur.row+offset[option].row;
 44             c=cur.col+offset[option].col;
 45             if(maze[r][c]==0){
 46                 isNode=true;
 47                 break;
 48             }
 49             option+=1;
 50         }
 51         if(isNode){
 52             cur.row=r;
 53             cur.col=c;
 54             path->push(cur);
 55             maze[r][c]=2;
 56             //option=0;
 57         }
 58         else{
 59             if(path->length()==0)
 60                 return false;
 61             maze[r][c]=3;
 62             //Position pre;
 63             path->pop(cur);
 64             cout<<"("<<cur.row<<","<<cur.col<<")"<<"->";
 65             /*
 66             if(pre.row==cur.row)
 67                 option=2+pre.col-cur.col;
 68             else
 69                 option=3+pre.row-cur.row;
 70             */   
 71             //cur=pre;
 72         }
 73     }
 74    
 75     LinkStack *p2=new LinkStack();
 76    
 77     while(path->length()!=0){
 78         path->pop(cur);
 79         p2->push(cur);
 80     }
 81     cout<<endl;
 82     while(p2->length()!=0){
 83         p2->pop(cur);
 84         cout<<"("<<cur.row<<","<<cur.col<<")"<<"->";
 85     }
 86     cout<<endl;
 87    
 88     return true;
 89 }
 90 
 91 int main(){
 92     for(int i=0;i<12;i++){
 93         for(int j=0;j<12;j++)
 94             cout<<maze[i][j]<<" ";
 95         cout<<endl;
 96     }   
 97     cout<<endl;
 98    
 99     findPath();
100    
101     cout<<endl;
102     for(int i=0;i<12;i++){
103         for(int j=0;j<12;j++)
104             cout<<maze[i][j]<<" ";
105         cout<<endl;
106     }   
107     system("pause");
108     return 0;
109 }

1.3 队列

1.3.1 顺序队列
 1 template<class Elem>class AQueue{
 2     private:
 3         int size;
 4         int front;
 5         int rear;
 6         Elem *listArray;
 7     public:
 8         AQueue(int sz){
 9             size=sz+1;
10             rear=0;
11             front=1;
12             listArray=new Elem[size];
13         }
14        
15         ~AQueue(){
16             delete [] listArray;
17         }
18        
19         void clear(){
20             front=rear;
21         }
22         bool enqueue(const Elem &it){
23             if((rear+2)%size==front)
24                 return false;
25             rear=(rear+1)%size;
26             listArray[rear]=it;
27             return true;
28         }
29        
30         bool dequeue(Elem &it){
31             if(length()==0)
32                 return false;
33             it=listArray[front];
34             front=(front+1)%size;
35             return true;
36         }
37        
38         bool frontValue(Elem &it){
39             if(length()==0)
40                 return false;
41             it=listArray[front];
42             return false;
43         }
44        
45         int length() const{
46             return ((rear+size)-front+1)%size;
47         }
48 };
1.3.2 链式队列
 1 template<class Elem>class Node{
 2     public: 
 3     Elem value;
 4     Node *next;
 5     Node(const Elem nodeValue,Node *nextNode){
 6         value=nodeValue;
 7         next=nextNode;
 8     }
 9     Node(Node *nextNode){
10         next=nextNode;
11     }
12 };
13 
14 template<class Elem>class LinkQueue{
15     private:
16         Node<Elem> *front;
17         Node<Elem> *rear;
18         int size;
19     public:
20         LinkQueue(){
21             front=rear=NULL;
22             size=0;
23         }
24        
25         ~LinkQueue(){
26             clear();
27         }
28        
29         void clear(){
30             while(front!=NULL){
31                 rear=front;
32                 front=front->next;
33                 delete rear;
34             }
35             rear=NULL;
36             size=0;
37         }
38        
39         bool enqueue(const Elem &it){
40             if(rear==NULL)
41                 front=rear=new Node<Elem>(it,NULL);
42             else{
43                 rear->next=new Node<Elem>(it,NULL);
44                 rear=rear->next;
45             }
46             size++;
47             return true;
48         }
49        
50         bool dequeue(Elem &it){
51             if(size==0)
52                 return false;
53             it=front->value;
54             Node<Elem>*temp=front;
55             front=front->next;
56             delete temp;
57             if(front==NULL)
58                 rear=NULL;
59              size--;
60         }
61        
62         bool frontValue(Elem &it){
63             if(size==0)
64                 return false;
65             it=front->value;
66             return true;
67         }
68        
69         int length() const{
70             return size;
71         }
72 };
posted @ 2024-03-01 15:13  码上领航者  阅读(5)  评论(0编辑  收藏  举报