(C)單鏈表

老師版

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 // 定于Node數據類型
  5 struct Node
  6 {
  7     int data;                     // 數據域
  8     struct Node *next;            // 指針域
  9 };
 10 
 11 // 創建一個單鏈表,并把head節點返回;
 12 struct Node* createLinkList(void);
 13 struct Node* createLinkList(void)
 14 {
 15     struct Node *head = NULL;
 16     struct Node *tail = NULL;
 17     struct Node *temp = NULL;
 18     
 19     int data;
 20     
 21     scanf("%d",&data);
 22     // data 不等于0
 23     while (data)
 24     {
 25         // malloc 函數申請內存
 26         temp = (struct Node *)malloc(sizeof(struct Node));
 27         temp->data = data;
 28         temp->next = NULL;
 29         
 30         if (head == NULL)
 31         {
 32             head = temp;
 33             tail = temp;
 34         }
 35         else
 36         {
 37             tail->next = temp;
 38             tail = temp;
 39         }
 40         
 41         scanf("%d",&data);
 42     }
 43     
 44     
 45     return  head;
 46 }
 47 
 48 // 輸出鏈表元素
 49 void printLinkList(struct Node *head);
 50 void printLinkList(struct Node *head)
 51 {
 52     struct Node *p = head;
 53     if (p == NULL)
 54     {
 55         return;
 56     }
 57     else
 58     {
 59         while (p)
 60         {
 61             printf("%d-》",p->data);
 62             // 指針重定向
 63             p = p->next;
 64         }
 65     }
 66 }
 67 
 68 //  計算鏈表的長度
 69 int count(struct Node *head);
 70 int count(struct Node *head)
 71 {
 72     int count = 0;
 73     struct Node *p = NULL;
 74     p = head;
 75     while (p)
 76     {
 77         count++;
 78         p = p->next;
 79     }
 80     
 81     return count;
 82 }
 83 
 84 
 85 int getNode(struct Node *head,int pos);
 86 int getNode(struct Node *head,int pos)
 87 {
 88     if (pos > count(head))
 89     {
 90         return 0;
 91     }
 92     
 93     struct Node *p = head;
 94     for (int i = 1; i < pos; i++)
 95     {
 96         p = p->next;
 97     }
 98     
 99     return p->data;
100 }
101 
102 void insertIntoHead(struct Node **h,int value);
103 void insertIntoHead(struct Node **h,int value)
104 {
105     struct Node *temp = NULL;
106     temp = (struct Node *)malloc(sizeof(struct Node));
107     temp->data = value;
108     temp->next = NULL;
109     
110     if (h == NULL)
111     {
112         *h = temp;
113     }
114     else
115     {
116         temp->next = *h;
117         *h = temp;
118     }
119 }
120 
121 
122 //2、把單鏈表中第pos個結點的值修改為x的值,若修改成功返回1,否則返回0
123 int modifyNode(struct Node *head,int pos,int x);
124 int modifyNode(struct Node *head,int pos,int x)
125 {
126     // 如果鏈表為空,或者pos 超出鏈表長度
127     if (head == NULL || pos > count(head))
128     {
129         return 0;
130     }
131     
132     struct Node *p = NULL;
133     p = head;
134     for (int i = 1; i < pos; i++)
135     {
136         p = p->next;
137     }
138     
139     p->data = x;
140     return 1;
141 }
142 
143 void insertIntoTail(struct Node **head,int value);
144 void insertIntoTail(struct Node **head,int value)
145 {
146     struct Node *tmp = NULL;
147     tmp = malloc(sizeof(struct Node));
148     tmp->data = value;
149     tmp->next = NULL;
150     if (*head == NULL)
151     {
152         *head = tmp;
153     }
154     else
155     {
156         struct Node *p;
157         // 定位最后一個節點
158         p = *head;
159         while (p->next)
160         {
161             p = p->next;
162         }
163         // 將申請的tmp加到鏈表后面
164         p->next = tmp;
165     }
166 }
167 
168 int insertInto(struct Node **head,int pos,int value);
169 int insertInto(struct Node **head,int pos,int value)
170 {
171     if (*head == NULL)
172     {
173         // 在第一個位置添加一個節點
174         insertIntoHead(head, value);
175         return 1;
176     }
177     if (pos > count(*head))
178     {
179         // 在最后一個位置添加一個節點
180         insertIntoTail(head, value);
181         return 1;
182     }
183     // 申請一個節點
184     struct Node *tmp;
185     tmp = malloc(sizeof(struct Node));
186     tmp->data = value;
187     tmp->next = NULL;
188     if (tmp==NULL)//tmp 申請內存失敗
189     {
190         return 0;
191     }
192     else
193     {
194         // 聲明一個輔助指針
195         struct Node *p;
196         p = *head;
197         // 定位輔助指針,指向pos位置前一個節點
198         for (int i = 1; i<pos-1; i++)
199         {
200             p = p->next;
201         }
202         tmp->next = p->next;
203         p->next = tmp;
204         return 1;
205     }
206 }
207 
208 
209 void insertIntoSortedList(struct Node **head,int value);
210 void insertIntoSortedList(struct Node **head,int value)
211 {
212     // 如果是鏈表為空,在第一個位置添加
213     if (*head == NULL)
214     {
215         insertIntoHead(head, value);
216         return;
217     }
218     
219     // 記錄要添加元素的位置
220     int pos = 1;
221     struct Node *p = NULL;
222     p = *head;
223     while (p)
224     {
225         if (p->data < value)
226         {
227             pos++;
228             p = p->next;
229         }
230         else
231         {
232             // 跳出循環
233             break;
234         }
235     }
236     
237     insertInto(head, pos, value);
238 }
239 
240 int deleteFirstNode(struct Node **head);
241 int deleteFirstNode(struct Node **head)
242 {
243     if (*head == NULL)
244     {
245         return 0;
246     }
247     
248     struct Node *p = NULL;
249     p = *head;
250     
251     *head = p->next;
252     int result = p->data;
253     
254     // 注意釋放內存
255     free(p);
256     p = NULL;
257     
258     return result;
259     
260     
261 }
262 
263 int deleteTailNode(struct Node **head);
264 int deleteTailNode(struct Node **head)
265 {
266     if (*head == NULL)
267     {
268         return 0;
269     }
270     
271     struct Node *p,*q;
272     p = q = NULL;
273     p = *head;
274     q = p->next;
275     // 通過循環定位讓q指向最后一個節點,p指向q前面一個節點
276     for (int i = 1; i <= count(*head)-2; i++)
277     {
278         p = q;
279         q = q->next;
280     }
281     // 取最后一個節點的數據
282     int result = q->data;
283     
284     p->next = NULL;
285     free(q);
286     q = NULL;
287     
288     return result;
289 }
290 
291 
292 int main(int argc, const char * argv[])
293 {
294 
295     struct Node *head = NULL;
296     // 產生鏈表
297     head = createLinkList();
298     
299     // 輸出鏈表元素個數
300     int c = count(head);
301     printf("c = %d\n",c);
302     
303     
304     //insertIntoHead(&head, 10);
305     
306     //modifyNode(head, 2, 10);
307     
308     //insertIntoTail(&head, 100);
309     //insertInto(&head, 2, 100);
310     
311     //insertIntoSortedList(&head, 6);
312     //deleteFirstNode(&head);
313     int r = deleteTailNode(&head);
314     printf("刪除最后一個節點%d\n",r);
315     
316     // 輸出鏈表
317     printLinkList(head);
318     
319     
320     /*
321     // 輸出第二個節點的數據域;
322     c = getNode(head, 2);
323     printf("\n");
324     printf("c = %d\n",c);
325     */
326     
327     return 0;
328 }

不才版

  1 #include <stdio.h>
  2 
  3 #include <stdlib.h>
  4 
  5 struct Node
  6 {
  7     int data;
  8     struct Node *next;
  9 };
 10 
 11 struct Node *creatLinkList(void)
 12 {
 13     struct Node *head=NULL;
 14     struct Node *tail=NULL;
 15     struct Node *temp=NULL;
 16     
 17     int data;
 18     scanf("%d",&data);
 19     while (data)
 20     {
 21         temp=malloc(sizeof(struct Node));
 22         temp->data=data;
 23         temp->next=NULL;
 24         
 25         if(head==NULL)
 26             head=tail=temp;
 27         else
 28         {
 29             tail->next=temp;
 30             tail=temp;
 31         }
 32         scanf("%d",&data);
 33     }
 34     return head;
 35 }
 36 
 37 void printLinkList(struct Node *head)
 38 {
 39     struct Node *tmp=head;
 40     if (tmp==NULL) {
 41         return;
 42     }
 43     while (tmp!=NULL) {
 44         printf("%d->>",tmp->data);
 45         tmp=tmp->next;
 46     }
 47     printf("\n");
 48 }
 49 
 50 int toNode(struct Node *head,int pos)
 51 {
 52     int data;
 53     for (int i=0; i<pos; i++) {
 54         if ((i!=pos-1)&&(head->next==NULL))
 55             return 0;
 56         data=head->data;
 57         head=head->next;
 58     }
 59     return data;
 60     
 61     //鏈表長度可以用count(head)算出。
 62 }
 63 
 64 int changeData(struct Node *head,int pos,int x)
 65 {
 66     for (int i=0; i<pos; i++) {
 67         if ((i!=pos-1)&&(head->next==NULL))
 68             return 0;
 69         if (i==pos-1) {
 70             head->data=x;
 71         }
 72         head=head->next;
 73     }
 74     return 1;
 75 }
 76 
 77 void insertNodeInHead(struct Node **head,int value)
 78 {
 79     struct Node *tmp=NULL;
 80     tmp=(struct Node *)malloc(sizeof(struct Node));
 81     tmp->data=value;
 82     tmp->next=*head;
 83     *head=tmp;
 84 }
 85 
 86 void insertNodeInEnd(struct Node *head,int value)
 87 {
 88     struct Node *tmp=NULL;
 89     tmp=(struct Node *)malloc(sizeof(struct Node));
 90     tmp->data=value;
 91     tmp->next=NULL;
 92     if (head==NULL) {
 93         head=tmp;
 94     }
 95     while (head->next!=NULL) {
 96         head=head->next;
 97     }
 98     head->next=tmp;
 99 }
100 
101 int insertData(struct Node *head,int pos,int value)
102 {
103     struct Node *tmp=NULL;
104     tmp=(struct Node *)malloc(sizeof(struct Node));
105     if (tmp==NULL) {
106         return 0;
107     }
108     tmp->data=value;
109     if (pos==0) {
110         return 0;
111     }
112     for (int i=0; i<pos; i++) {
113         
114         if ((i!=pos-1)&&(head->next==NULL)) {
115             return 0;
116         }
117         if (i==pos-1) {
118             tmp->next=head->next;
119             head->next=tmp;
120         }
121         head=head->next;
122     }
123     return 1;
124 }
125 
126 void insertSortData(struct Node *head,int value)
127 {
128     struct Node *tmp=(struct Node *)malloc(sizeof(struct Node));
129     tmp->data=value;
130     while (head->data<tmp->data) {
131         if (head->next->data>=tmp->data) {
132             tmp->next=head->next;
133             head->next=tmp;
134             return;
135         }
136         if (head->next==NULL) {
137             tmp->next=head->next;
138             head->next=tmp;
139             return;
140         }
141         head=head->next;
142     }
143     
144 }
145 
146 int deleteHead(struct Node **pointhead)
147 {
148     int data;
149     data=(*pointhead)->data;
150     struct Node *p=*pointhead;
151     *pointhead=(*pointhead)->next;
152     free(p);
153     p=NULL;
154     if (*pointhead==NULL||(*pointhead)->next==NULL) {
155         return 0;
156     }
157     return data;
158 }
159     
160 int deleteEnd(struct Node *head)
161 {
162     struct Node *tmp=head;
163     int data;
164     if(head==NULL||head->next==NULL)
165         return 0;
166     while (tmp->next!=NULL) {
167         if (tmp->next->next==NULL) {
168             data=tmp->next->data;
169             free(tmp->next);
170             tmp->next=NULL;
171             break;
172         }
173         tmp=tmp->next;
174     }
175     return data;
176 }
177 
178 int main(int argc,const char *argv[]) 
179 {
180     struct Node *h = creatLinkList();
181     printLinkList(h);
182     
183     //1、返回單鏈表中第pos個結點中的元素,若pos超出范圍,則返回0
184     printf("%d\n",toNode(h,5));
185     
186     //2、把單鏈表中第pos個結點的值修改為x的值,若修改成功返回1,否則返回0
187     if (changeData(h,5,5)) {
188         printLinkList(h);
189     }
190     
191     //3、向單鏈表的表頭插入一個元素
192     insertNodeInHead(&h, 0);
193     printLinkList(h);
194     
195     //4、向單鏈表的末尾添加一個元素
196     insertNodeInEnd(h, 50);
197     printLinkList(h);
198     
199     //5、向單鏈表中第pos個結點位置插入元素為x的結點,若插入成功返回1,否則返回0
200     if(insertData(h, 7, 60))
201         printLinkList(h);
202     
203     //6、向有序單鏈表中插入元素x結點,使得插入后仍然有序
204     insertSortData(h, 6);
205     printLinkList(h);
206     
207     //7、從單鏈表中刪除表頭結點,并把該結點的值返回,若刪除失敗則返回0
208     printf("%d\n",deleteHead(&h));
209     printLinkList(h);
210     
211     //8、從單鏈表中刪除表尾結點并返回它的值,若刪除失敗則返回0
212     printf("%d\n",deleteEnd(h));
213     printLinkList(h);
214     
215     return 0;
216 }

?

?

轉載于:https://www.cnblogs.com/mingfung-liu/p/3158901.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/376580.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/376580.shtml
英文地址,請注明出處:http://en.pswp.cn/news/376580.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

實驗:sigsuspend(),sigprocmask()

實驗&#xff1a;sigsuspend(),sigprocmask()源代碼&#xff1a;/* * Program: pause_suspend.c * To test the difference between sigsuspend() and paus(). * Author: zsl * Date: 2014-10-17 * First release. * 參見網頁&#xff1a;http://blog.csdn.net/liwentao1091/ar…

后臺系統可擴展性學習筆記(七)Service Discovery與微服務

文章目錄應用層微服務架構服務注冊查詢 Service Discovery客戶端 Service DiscoveryDNS-SD DNS-based Service Discovery服務端 Service Discovery服務注冊與注銷自注冊模式第三方注冊模式總結參考應用層 在簡單的 3 層結構中&#xff0c;Web 服務層既要處理請求&#xff0c;又…

很久沒寫代碼了,這(那)幾天真是累死了。。。先寫一個幻方的程序吧

1 #include <stdio.h>2 #include <stdlib.h>3 #include <windows.h>4 5 #define EVEN_DOUBLE_4 4 //雙偶的最基本類型&#xff0c;4階雙偶6 #define SCREEN_SIZE 19 //屏幕顯示不變形的最大尺寸&#xff08;主要是因為窗口大小限制&#xff09;7 #defi…

#pragma once

http://baike.baidu.com/view/1276747.htm?fraladdin 轉載于:https://www.cnblogs.com/prayer521/p/4069040.html

后臺系統可擴展性學習筆記(八)Service Mesh

文章目錄網絡傳輸可靠性將微服務控制下沉到網絡棧&#xff1f;Sidecar從 Sidecar 到 Service MeshService Mesh 部署平臺參考網絡傳輸可靠性 從計網的學習過程中我們可以知道數據在網絡傳輸中可能會出現一些異常狀況&#xff1a; 數據丟失&#xff1a;數據包可能會到達一個緩…

關于Spring batch的學習之CSV2DB

最近在學習Spring batch相關的內容&#xff0c;網上也有不少Spring Batch相關的知識&#xff0c;不過大多都是使用xml進行配置的。這里是我用注解的方式進行相關的學習心得。 首先我們來看如何將一個文本文件中的內容導入到數據庫中。 我們先來看一下我們所需要的環境。我們這里…

后臺系統可擴展性學習筆記(九)Database Replication

文章目錄數據庫擴展一致性問題Replication &#xff08;復制&#xff09;異步復制同步復制半同步復制拓撲結構單主結構多主結構無主結構復制具體措施參考數據庫擴展 之前在第一章后臺系統可擴展性學習筆記&#xff08;一&#xff09;概要談到&#xff1a;理論上&#xff0c;有…

python中的sum函數.sum(axis=1)

看起來挺簡單的樣子&#xff0c;但是在給sum函數中加入參數。sum&#xff08;a&#xff0c;axis0&#xff09;或者是.sum(axis1) 就有點不解了 在我實驗以后發現 我們平時用的sum應該是默認的axis0 就是普通的相加 而當加入axis1以后就是將一個矩陣的每一行向量相加 例如&…

后臺系統可擴展性學習筆記(十)Database Partitioning

為了提升數據庫的處理能力&#xff0c;我們把單庫擴展成多庫&#xff0c;并通過更新同步機制&#xff08;即Replication&#xff09;來保證多份數據的一致性。然而&#xff0c;在 各種復制方案下&#xff0c;每個數據庫都持有一份完整數據&#xff0c;基于全量數據提供增刪改查…

基于FPGA的HDTV視頻圖像灰度直方圖統計算法設計

隨著HDTV的普及&#xff0c;以LCD-TV為主的高清數字電視逐漸進入蓬勃發展時期。與傳統CRT電視不同的是&#xff0c;這些高清數字電視需要較復雜的視頻處理電路來驅動&#xff0c;比如&#xff1a;模數轉換&#xff08;A/D Converter&#xff09;、去隔行&#xff08;De-interla…

后臺系統可擴展性學習筆記(十一)Database Denormalization

之前的兩篇筆記中談到了從單庫擴展到多庫以承載更多的請求量以及單庫&#xff08;表&#xff09;拆分成多庫&#xff08;表&#xff09;&#xff0c;打破單庫的性能瓶頸。 這都是為了應對大數據量下的措施。 然而&#xff0c;除卻數據量外&#xff0c;還有一個極其影響單庫性能…

Java Swing 影樓管理系統之登錄功能

開頭打廣告&#xff0c;Java1234.com。 首先&#xff0c;來個效果圖。 關鍵代碼 1&#xff0c;界面層 private void Jb_DengLuActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:String UserName this.Jb_UserNameTxt.getText();String …

Bdsyn百度手機助手是何物,它是怎樣神不知鬼不覺地安裝到你的電腦里的?

【電腦軟件管理中Bdsyn手機助手的問題】Bdsyn手機助手 is developed by Baidu, Inc. and is used by 10 users of Software Informer. 并不是本人安裝的&#xff08;應該是自己自己主動安裝的&#xff09;&#xff0c;卸載以后過幾天又會出如今軟件列表里。百度搜索卻無法搜索出…

后臺系統可擴展性學習筆記(十二)NoSQL

文章目錄NoSQL定義NoSQL種類鍵值存儲文檔存儲寬列存儲圖形數據庫NoSQL 意味著什么ACID vs. BASESQL or NoSQLNoSQL定義 不同于關系型數據庫&#xff0c;NoSQL 數據庫&#xff08;也叫非 SQL 或非關系型數據庫&#xff09;提供的數據存儲、檢索機制并不是基于表關系建模的。沒有…

android2.2應用開發之IccCard(sim卡或USIM卡)

tyle"margin:20px 0px 0px; font-size:14px; line-height:26px; font-family:Arial; color:rgb(51,51,51)"> 如果要做android通訊錄的聯系人的機卡混排顯示&#xff0c;由于手機卡類型的不同&#xff0c;導致手機卡存儲容量以及可以存儲信息不同&#xff0c;就要涉…

后臺系統可擴展性學習筆記(十三)緩存

文章目錄在哪兒加緩存緩存什么內容緩存原始查庫結果緩存數據對象怎么查詢緩存結果預留緩存模式直讀模式直寫模式回寫式緩存繞寫式緩存提前刷新模式緩存滿了如何處理參考讀寫分離、分庫分表、反范式化、采用 NoSQL……如果這些擴展手段全都上了&#xff0c;數據響應依舊越來越慢…

[linux]gdb調試

使用gdb可以在命令行方便地調試&#xff0c;并且能以命令程序的方式調試源代碼。 常用命令簡寫print-p,step-s,next-n 進入gdb //方式一 gdb test//test 為可執行文件&#xff0c;使用-g編譯得到 //方式二 gdb -q //不顯示版權信息 file test //file命令打開文件 退出gdb quit …

后臺系統可擴展性學習筆記(十四)異步機制與MQ

對于 Web 服務而言&#xff0c;提升可擴展性的主要途徑是將耗時的同步工作改成異步處理&#xff0c;從而允許將這些工作“外包”給多個 Worker 去做&#xff0c;或者提前完成能夠預知的部分。 異步機制與可擴展性之間的關系需要從&#xff08;異步&#xff09;并行處理的優勢說…

RegisterClientScriptBlock與 RegisterStartupScript區別

Page.ClientScript.RegisterClientScriptBlock而用Page.ClientScript.RegisterStartupScript是因為&#xff1a;RegisterStartupScript 把script放置在ASP.NET page的底部&#xff0c;而RegisterClientScriptBlock把script放置在ASP.NET page的頂部&#xff0c;用RegisterClien…

【Web后端筆記】SQL Server與java數據類型對應

編號數據庫類型JDBC類型JDBC索引描述1intjava.lang.Integer4 2varcharjava.lang.String12 3charjava.lang.String1 4ncharjava.lang.String1 5nvarcharjava.lang.String12 6textjava.lang.String-1 7ntextjava.lang.String-1 8tinyintjava.lang.Integer-6 9intjava.lang.Intege…