绾挎€ц〃閾捐〃瀹炵幇

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绾挎€ц〃閾捐〃瀹炵幇相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/for' title='for'>for   mes   閾惧紡   reverse   cout   enter   鍏冪礌   put   閾炬帴   

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define sc scanf
  4 #define ElemType int 
  5 //绾挎€ц〃鐨勯摼寮忚〃绀哄拰瀹炵幇
  6 
  7 typedef struct LNode{
  8     int data;
  9     struct LNode *next;
 10 }LNode,*LinkList;
 11 //鍏充簬涓婇潰涓哄暐鏄繖鏍峰瓙鐨勶紝鐪嬩笅闈㈤摼鎺ュ嵆鍙?
 12 //https://zhidao.baidu.com/question/489369235.html
 13  
 14 //鍒涘缓閾捐〃,鏈塶涓粨鐐?
 15 void CreateList(LinkList &L,int n)
 16 {
 17     L = (LinkList)malloc(sizeof(LNode));
 18     L->next = NULL;//鍒涘缓涓€涓甫澶寸粨鐐圭殑鍗曢摼琛?/span>
 19     L->data = 0;//鐢ㄤ簬璁板綍褰撳墠閾捐〃鐨勫厓绱犱釜鏁?
 20     
 21     LNode *p;
 22     for(int i = n;i > 0;i--)
 23     {
 24         p = (LinkList)malloc(sizeof(LNode));//鐢熸垚鏂拌妭鐐?
 25         sc("%d",&p->data);
 26         p->next = L->next;
 27         L->next = p;
 28     } 
 29 } 
 30 
 31 //鎻掑叆鍏冪礌
 32 bool LinkInsert_L(LinkList &L,int i,ElemType e)
 33 {
 34     LNode *p = L; 
 35     int j = 0;
 36     while(p && j < i - 1)
 37     {
 38         p = p->next;
 39         ++j;
 40     }
 41     
 42     //鍒ゆ柇鏄惁闈炴硶浣嶇疆
 43     if(!p || j > i - 1) {
 44         puts("Oh! Baby! The insertion position is invalid. Please re-enter the insertion position!");
 45         return 0; 
 46     }
 47     
 48     LNode *s = (LinkList)malloc(sizeof(LNode));
 49     s->data = e;
 50     s->next = p->next;
 51     p->next = s;
 52     return 1;
 53 } 
 54 
 55 //閾捐〃鍒犻櫎鍏冪礌
 56 bool ListDelete_L(LinkList &L,int i,int &e)
 57 {
 58   //鍒犻櫎绗琲涓厓绱?
 59   LNode *p = L; int j = 0;
 60       while(p->next && j < i - 1)
 61     {
 62         p = p->next;
 63         ++j;
 64     }
 65     
 66     //鍒ゆ柇鏄惁闈炴硶浣嶇疆
 67     if(!(p->next) || j > i - 1) {
 68         puts("Oh! Baby! The position is invalid. Please re-enter the position!");
 69         return 0; 
 70     }
 71     
 72     LNode *q = (LinkList)malloc(sizeof(LNode));
 73     q = p->next;
 74     p->next = q->next;
 75     e = q->data;
 76     free(p);
 77     return 1; 
 78 } 
 79 
 80 //鍚堝苟閾捐〃
 81 void MergeList(LinkList &la,LinkList &lb,LinkList &lc)
 82 {
 83     LNode *pa,*pb,*pc;
 84     pa = la->next; pb = lb->next;
 85     lc = pc = la;
 86     
 87     while(pa && pb)
 88     {
 89         if(pa->data <= pb->data){
 90             pc->next = pa;
 91             pc = pa;
 92             pa = pa->next;
 93         }
 94         else{
 95             pc->next = pb;
 96             pc = pb;
 97             pb = pb->next;
 98         }
 99     }    
100     pc->next = pa? pa:pb;
101     free(lb);
102 } 
103 
104 //鑾峰彇閾捐〃闀垮害
105 int getListLength(LinkList &L)
106 {
107     LNode *p = L;
108     int cnt = 0;
109     while(p->next != NULL)
110     {
111         p = p->next;
112         ++cnt;
113         //cout << p->data << " ";
114     }
115     return cnt;
116 }
117 
118 //鑾峰彇閾捐〃鐨勬煇涓厓绱?/span>
119 bool getElem(LinkList &L,int q,int &e) {
120     
121     int length = getListLength(L);
122     if(q < 1 || q > length)
123     {
124         puts("Oh! Baby! The  position is invalid. Please re-enter the position!");
125         return 0;
126     }
127     LNode *p = L;
128     int j = 0;
129     while(p->next && j < q - 1)
130     {
131         p = p->next;
132         j++;
133     }
134     if(p->next == NULL)
135     {
136         puts("Sorry, didn鈥榯 find it");
137         return 0;
138     }
139     else{
140         p = p->next;
141         e = p->data;
142         return 1;
143     }
144 }
145 
146 void Reverse_List(LinkList &la,LinkList &lb)
147 {
148       LNode *p = la;
149       while(p->next != NULL)
150       {
151           p = p->next;
152           if(p != NULL)
153           LinkInsert_L(lb,1,p->data);
154     }
155 }
156 
157 //閬嶅巻閾捐〃 
158 void LinkList_Traver(LinkList &L)
159 {
160     LNode *p = L;
161     puts("Let鈥榮 do it! Traver! Traver_List!"); 
162     while(p->next != NULL)
163     {
164         p = p->next;
165         cout << p->data << " ";
166     }
167     puts("");
168 } 
169 
170 //閿€姣侀摼琛?
171 bool List_Destroed(LinkList &L)
172 {
173     LNode *p;
174     if(L == NULL) return 0;
175     while(L->next)
176     {
177         p = L->next;
178         free(L);
179         L = p;
180      } 
181     return 1;
182 }
183 int main()
184 {
185     LinkList la,lb,lc,ld;
186     int n;
187     sc("%d",&n);
188     CreateList(la,n);
189     CreateList(lb,n);
190     CreateList(ld,0);
191     
192     
193 //    for(int i = 1;i <= 5;i++)
194 //    {
195 //        LinkInsert_L(la,i,i);
196 //    }
197     
198     cout << "la length = " << getListLength(la) << endl;
199     cout << "lb length = " << getListLength(lb) << endl;
200     
201     int ele;
202     if(getElem(la,5,ele))
203     {
204         cout << ele << endl;
205     }
206     
207     if(getElem(lb,1,ele))
208     {
209         cout << ele << endl;
210     }
211     
212     LinkList_Traver(la);
213     LinkList_Traver(lb);
214     
215     MergeList(la,lb,lc);
216     LinkList_Traver(la);
217     LinkList_Traver(lc);
218     
219     Reverse_List(la,ld);
220     cout << "Traver ld = ";
221     LinkList_Traver(ld);
222     
223     List_Destroed(la);
224     LinkList_Traver(la);
225     return 0;;
226 }

 

以上是关于绾挎€ц〃閾捐〃瀹炵幇的主要内容,如果未能解决你的问题,请参考以下文章

Java涓瑿AS搴曞眰瀹炵幇鍘熺悊鍒嗘瀽

涓€鍙ヨ瘽瀹炵幇MySQL搴撲腑鐨勬寜鏉′欢鍙樺寲鍒嗙粍

璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?/h1>

瀹炵幇strcpy

018 瀹炵幇鍟嗗搧鍒嗙被鏌ヨ

webpack loader 瀹炵幇