数据结构复习 ——线性表

时间:2021-03-29 10:30:47
  1 #include <iostream>
  2 #include<stdlib.h>
  3 using namespace std;
  4 #define maxsize 10
  5 typedef int elemtype;
  6 /*struct xlist
  7 {
  8     elemtype *elem;
  9     int length;
 10 };
 11 int createxlist(xlist &k)           ##顺序表
 12 {
 13     k.elem = new elemtype[maxsize];
 14     if (!k.elem)
 15         exit(1);
 16     for (int i = 0; i < maxsize; i++)
 17     {
 18         k.elem[i] = 0;
 19     }
 20     k.length = 0;
 21     return 0;
 22 }
 23 int insertdata(xlist &k, int b,elemtype n[])
 24 {
 25     cout << " 25";
 26     for (int i = 0; i < b; i++)
 27     {
 28         if (k.elem[i]==0)
 29         {
 30             k.elem[i] = n[i];
 31             k.length++;
 32         }
 33     }
 34     return 0;
 35 }
 36 int output(xlist k)
 37 {
 38     for (int i = 0; i < k.length; i++)
 39     {
 40         cout << k.elem[i]<<" ";
 41     }
 42     return 0;
 43 }
 44 int main()
 45 {
 46     xlist k;
 47     createxlist(k);
 48     int t = 5,n[5],b;
 49     for(int i=0;i<t;i++)
 50     cin >> n[i];
 51     insertdata(k, t, n);
 52     output(k);
 53     return 0;
 54 }                   */     
 55 
 56 
 57 
 58 /*typedef struct lnode                链表
 59 {
 60     elemtype data;
 61     lnode *next;
 62 
 63 }lnode,*linklist;*/
 64 /*int createlinklist(linklist &h,int t,int n[])              #¥前插法
 65 {
 66     h = new lnode;
 67     h->next = NULL;
 68     lnode* p;
 69     for (int i = 0; i < t; i++)
 70     {
 71         p = new lnode;
 72         p->data = n[i];
 73         p->next = h->next;
 74         h->next = p;
 75     }
 76     return 0;
 77 }*/
 78 /*int createlinklist(linklist &h, int t, int n[])           后插法
 79 {
 80     h = new lnode;
 81     h->data = -maxsize;
 82     h->next = NULL;
 83     lnode *p,*r;
 84      r = h;
 85     for (int i = 0; i < t; i++)
 86     {
 87         p = new lnode;
 88         p->data = n[i];
 89         p->next = NULL;
 90         r->next = p;
 91         r = p;
 92     }
 93     return 0;
 94 }*/
 95 /*void output(linklist h)
 96 {
 97     h = h->next;
 98     while (h->next!= NULL)
 99     {
100         cout << h->data;
101         h = h->next;
102     }
103     cout << h->data;
104 }*/
105 
106 /*void outputsinglelist(linklist h)
107 {
108     h = h->next;             因为头结点数值不赋予就是乱码不是NULL,和它的Next域也不是NULL,因此输出时需控制 109     while (h->data!=-maxsize)
110     {
111         cout << h->data << " ";
112         h = h->next;
113         
114     }
115 }
116 void singlecircle(linklist &h, linklist &k)         两个单链表合成一个循环链表
117 {
118     lnode *A, *B,*p;
119     A = h;
120     B = k;
121     while (h->next != NULL)
122     {
123         h = h->next;
124     }
125     h->next = A;
126     while (k->next != NULL)
127     {
128         k = k->next;
129     }
130     k->next = B; 
131     p = k->next->next;
132     k->next = h->next;
133     h->next = p;
134     h = A;
135     delete B;
136 }*/
137 typedef struct doublelist
138 {
139     elemtype data;
140     doublelist *prior,*next;
141 }doublelist,*dlinklist;
142 int createdlinklist(dlinklist& k,int t,int n[])
143 {
144     doublelist *r,*p;
145     k = new doublelist;
146     k->data = -maxsize;
147     k->next = NULL;
148     r = k;
149     for (int i = 0; i < t; i++)
150     {
151         p = new doublelist;
152         p->data = n[i];
153         p->prior = r;
154         p->next =NULL;
155         r->next = p;
156         r = p;
157     }
158     r->next = k;
159     return 0;
160 }
161 void outputdoublelist(dlinklist h)
162 {
163     h = h->next;
164     while (h->data != -maxsize)
165     {
166         cout << h->data<<" ";
167         h = h->next;
168     }
169 }
170 int main()
171 {
172     dlinklist h,k;
173     int t = 5, n[5];
174     for (int i = 0; i < t; i++)
175         cin >> n[i];
176     createdlinklist(h, t, n);
177     /*createlinklist(k, t, n);
178     singlecircle(h, k); 
179     outputsinglelist(h);*/
180     outputdoublelist(h);
181     return 0;
182 }