「C語言」「例題」指針與鏈表

来源:http://www.cnblogs.com/corvoh/archive/2016/03/06/5248564.html
-Advertisement-
Play Games

「C語言」「例題」指針與鏈表


     本篇收集《C語言程式設計教程》第十章“指針與鏈表”的所有例題。

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 //使用malloc函數動態分配空間
 5 
 6 int main()
 7 {
 8     int *iIntMalloc=(int *)malloc(sizeof(int));//分配空間
 9     *iIntMalloc=100;//使用該空間保存數據
10     printf("%d\n",*iIntMalloc); 
11     return 0;
12 }
10.1 使用malloc函數動態分配空間

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define LEN sizeof(char[26])/* 定義的值為26個字元的字元數組所占的位元組長度 */
 4 
 5 //使用calloc分配數組記憶體 
 6 
 7 int main()
 8 {
 9     int i;/* 迴圈變數i */
10     /* 使用calloc動態分配一個長度為26字元的字元數組 */
11     char *ch1=(char *)calloc(26,sizeof(char));
12     /* 使用malloc動態分配一個長度為26字元的字元數組 */
13     char *ch2=(char *)malloc(LEN);
14     for(i=0;i<26;i++)/* 為兩個字元數組復賦值 */
15     {
16         ch1[i]=65+i;/* ch1是大寫字元數組 */
17         ch2[i]=97+i;/* ch2是小寫字元數組 */
18     }
19     printf("26個大寫字母:\n");
20     for(i=0;i<26;i++)/* 列印大寫字母 */
21     {
22         printf("%c ",ch1[i]);
23         if(i==12 || i==25)/* 格式控制,輸出換行 */
24             printf("\n");
25     }
26     printf("26個小寫字母");
27     for(i=0;i<26;i++)/* 列印小寫字母 */
28     {
29         printf("%c ",ch2[i]);
30         if(i==12 || i==25)/* 格式控制,輸出換行 */
31             printf("\n");
32     }
33     return 0;
34 }
10.2 使用calloc分配數組記憶體

 

 1 #include <stdio.h>
 2 #include <stdlib.h> 
 3 
 4 //使用realloc函數重新分配記憶體
 5 
 6 int main()
 7 {
 8     short *s;/* 定義短整型指針變數s */
 9     double *f=(double *)malloc(sizeof(double));/* 申請double變數所占記憶體空間 */
10     printf("指針f指向記憶體空間的起始地址:%d\n",f);/* 列印首地址 */
11     printf("指針f指向記憶體空間的大小,%d位元組\n",sizeof(*f));/* 列印空間大小 */
12     s=(short *)realloc(f,sizeof(short));/* 重新分配記憶體 */
13     printf("指針s指向記憶體空間的起始地址:%d\n",s);/* 列印首地址 */
14     printf("指針s指向記憶體空間的大小,%d位元組\n",sizeof(*s));/* 列印空間大小 */
15     return 0;
16 }
10.3 使用realloc函數重新分配記憶體

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 //使用free函數釋放記憶體空間 
 5 
 6 int main()
 7 {
 8     int *pInt;
 9     pInt=(int *)malloc(sizeof(pInt));
10     *pInt=100;
11     printf("%d\n",*pInt);
12     free(pInt);
13     printf("%d\n",*pInt);
14     return 0;
15 }
10.4 使用free函數釋放記憶體空間

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 //創建鏈表並將其輸出
 5 
 6 struct Student
 7 {
 8     char cName[20];/* 姓名 */
 9     int iNumber;/* 學號 */
10     struct Student *next;/* next的類型是指向本結構體類型的指針類型 */
11 };
12 
13 int iCount;/* 全局變數表示鏈表長度 */
14 
15 struct Student *Create()
16 {
17     struct Student *pHead=NULL;/* 初始化鏈表,頭指針為空 */
18     struct Student *pEnd,*pNew;
19     iCount=0;/* 初始化鏈表長度 */
20     pEnd=pNew=(struct Student *)malloc(sizeof(struct Student));
21     printf("請輸入學生的姓名和學號:\n");
22     scanf("%s",pNew->cName);
23     scanf("%d",&pNew->iNumber);
24     while(pNew->iNumber!=0)
25     {
26         iCount++;
27         if(iCount==1)
28         {
29             pNew->next=pHead;/* 使得指針指向為空 */
30             pEnd->next=pNew;/* 跟蹤新加入的節點 */
31             pHead=pNew;/* 頭指針指向首節點 */ 
32         }
33         else
34         {
35             pNew->next=NULL;/* 新節點的指針為空 */
36             pEnd->next=pNew;/* 原來的節點指向新節點 */
37             pEnd=pNew;/* pEnd指向新節點 */
38         }
39         pNew=(struct Student *)malloc(sizeof(struct Student));/* 再次分配節點的記憶體空間 */
40         scanf("%s",pNew->cName);
41         scanf("%d",&pNew->iNumber);
42     }
43     free(pNew);/* 釋放節點空間 */
44     return pHead;
45 }
46 
47 void print(struct Student *pHead)
48 {
49     struct Student *pTemp;/* 迴圈所用的臨時指針 */
50     int iIndex=1;/* 表示鏈表中節點的序號 */
51     printf("**********本名單中有%d個學生**********\n",iCount);
52     pTemp=pHead;/* 指針得到首節點的地址 */
53     while(pTemp!=NULL)
54     {
55         printf("第%d個學生是:\n",iIndex);
56         printf("姓名:%s\n",pTemp->cName);/* 輸出姓名 */
57         printf("學號:%d\n\n",pTemp->iNumber);/* 輸出學號 */
58         pTemp=pTemp->next;/* 移動臨時指針到下一個節點 */
59         iIndex++;/* 進行自加運算 */
60     }
61 }
62 
63 int main()
64 {
65     struct Student *pHead;/* 定義頭結點 */
66     pHead=Create();/* 創建節點 */
67     print(pHead);/* 輸出鏈表 */
68     return 0;
69 }
10.5 創建鏈表並將其輸出

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 //鏈表的插入操作
 5 
 6 struct Student{
 7     char cName[20];/* 姓名 */
 8     int iNumber;/* 學號 */
 9     struct Student *next;/* next的類型是指向本結構體類型的指針類型 */
10 };
11 
12 int iCount;/* 全局變數表示鏈表長度 */ 
13 
14 struct Student *Create()
15 {
16     struct Student *pHead=NULL;/* 初始化鏈表,頭指針為空 */ 
17     struct Student *pNew,*pEnd;
18     iCount=0;/* 初始化鏈表長度 */ 
19     pEnd=pNew=(struct Student *)malloc(sizeof(struct Student));
20     printf("請輸入學生的姓名和學號:\n");
21     scanf("%s",pNew->cName);
22     scanf("%d",&pNew->iNumber);
23     while(pNew->iNumber!=0)
24     {
25         iCount++; 
26         if(iCount==1)
27         {
28             pNew->next=pHead;/* 使得指針指向為空 */ 
29             pEnd=pNew;/* 跟蹤新加入的節點 */ 
30             pHead=pNew;/* 頭指針指向首節點 */ 
31         }
32         else
33         {
34             pNew->next=NULL;/* 新節點的指針為空 */ 
35             pEnd->next=pNew;/* 原來的節點指向新節點 */ 
36             pEnd=pNew;/* pEnd指向新節點 */ 
37         }
38         pNew=(struct Student *)malloc(sizeof(struct Student));/* 再次分配節點的記憶體空間 */ 
39         scanf("%s",pNew->cName);
40         scanf("%d",&pNew->iNumber);
41     }
42     free(pNew);/* 釋放節點空間 */ 
43     return(pHead);
44 }
45 
46 void Print(struct Student *pHead)
47 {
48     struct Student *pTemp;/* 迴圈所用的臨時指針 */ 
49     int iIndex=1;/* 表示鏈表中節點的序號 */ 
50     printf("\n****本名單中有%d名學生****\n",iCount);
51     pTemp=pHead;/* 指針得到首節點的地址 */
52     while(pTemp!=NULL)
53     {
54         printf("第%d名學生是:\n",iIndex); 
55         printf("姓名:%s\n",pTemp->cName);/* 輸出姓名 */ 
56         printf("學號:%d\n\n",pTemp->iNumber);/* 輸出學號 */ 
57         pTemp=pTemp->next;/* 移動臨時指針到下一個節點 */ 
58         iIndex++;/* 進行自加運算 */ 
59     }
60 }
61 
62 struct Student *Insert(struct Student *pHead)
63 {
64     struct Student *pNew;/* 定義pNew指向新分配的空間 */ 
65     printf("請輸入學生的姓名和學號:\n");
66     pNew=(struct Student *)malloc(sizeof(struct Student));/* 分配記憶體空間,返回該記憶體空間地址 */ 
67     scanf("%s",pNew->cName);
68     scanf("%d",&pNew->iNumber);
69     pNew->next=pHead;/* 新節點指針指向原來的首節點 */ 
70     pHead=pNew;/* 頭指針指向新節點 */ 
71     iCount++;/* 增加鏈表節點數量 */ 
72     return pHead;/* 返回頭指針 */
73 }
74 
75 int main()
76 {
77     struct Student *pHead;/* 定義頭結點 */ 
78     pHead=Create();/* 創建節點 */
79     pHead=Insert(pHead);/* 插入節點 */ 
80     Print(pHead);/* 輸出鏈表 */ 
81     return 0;
82 }
10.6 鏈表的插入操作

 

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 //鏈表的刪除操作
  5 
  6 struct Student{
  7     char cName[20];/* 姓名 */
  8     int iNumber;/* 學號 */
  9     struct Student *next;/* next的類型是指向本結構體類型的指針類型 */
 10 };
 11 
 12 int iCount;/* 全局變數表示鏈表長度 */ 
 13 
 14 struct Student *Create()
 15 {
 16     struct Student *pHead=NULL;/* 初始化鏈表,頭指針為空 */ 
 17     struct Student *pNew,*pEnd;
 18     iCount=0;/* 初始化鏈表長度 */ 
 19     pEnd=pNew=(struct Student *)malloc(sizeof(struct Student));
 20     printf("請輸入學生的姓名和學號:\n");
 21     scanf("%s",pNew->cName);
 22     scanf("%d",&pNew->iNumber);
 23     while(pNew->iNumber!=0)
 24     {
 25         iCount++; 
 26         if(iCount==1)
 27         {
 28             pNew->next=pHead;/* 使得指針指向為空 */ 
 29             pEnd=pNew;/* 跟蹤新加入的節點 */ 
 30             pHead=pNew;/* 頭指針指向首節點 */ 
 31         }
 32         else
 33         {
 34             pNew->next=NULL;/* 新節點的指針為空 */ 
 35             pEnd->next=pNew;/* 原來的節點指向新節點 */ 
 36             pEnd=pNew;/* pEnd指向新節點 */ 
 37         }
 38         pNew=(struct Student *)malloc(sizeof(struct Student));/* 再次分配節點的記憶體空間 */ 
 39         scanf("%s",pNew->cName);
 40         scanf("%d",&pNew->iNumber);
 41     }
 42     free(pNew);/* 釋放節點空間 */ 
 43     return(pHead);
 44 }
 45 
 46 void Print(struct Student *pHead)
 47 {
 48     struct Student *pTemp;/* 迴圈所用的臨時指針 */ 
 49     int iIndex=1;/* 表示鏈表中節點的序號 */ 
 50     printf("****本名單中有%d名學生****\n",iCount);
 51     pTemp=pHead;/* 指針得到首節點的地址 */
 52     while(pTemp!=NULL)
 53     {
 54         printf("第%d名學生是:\n",iIndex); 
 55         printf("姓名:%s\n",pTemp->cName);/* 輸出姓名 */ 
 56         printf("學號:%d\n\n",pTemp->iNumber);/* 輸出學號 */ 
 57         pTemp=pTemp->next;/* 移動臨時指針到下一個節點 */ 
 58         iIndex++;/* 進行自加運算 */ 
 59     }
 60 }
 61 
 62 struct Student *Insert(struct Student *pHead)
 63 {
 64     struct Student *pNew;/* 定義pNew指向新分配的空間 */ 
 65     printf("請輸入學生的姓名和學號:\n");
 66     pNew=(struct Student *)malloc(sizeof(struct Student));/* 分配記憶體空間,返回該記憶體空間地址 */ 
 67     scanf("%s",pNew->cName);
 68     scanf("%d",&pNew->iNumber);
 69     pNew->next=pHead;/* 新節點指針指向原來的首節點 */ 
 70     pHead=pNew;/* 頭指針指向新節點 */ 
 71     iCount++;/* 增加鏈表節點數量 */ 
 72     return pHead;/* 返回頭指針 */
 73 }
 74 
 75 void Delete(struct Student *pHead,int iIndex)
 76 /* pHead為頭結點,iIndex表示要刪除的節點序號 */
 77 {
 78     int i;
 79     struct Student *pTemp;/* 臨時指針 */
 80     struct Student *pPre;/* 表示要刪除節點前的節點 */
 81     pTemp=pHead;/* 得到鏈表的頭結點 */
 82     pPre=pTemp;
 83     printf("--------刪除第%d個學生--------",iIndex);
 84     for(i=1;i<iIndex;i++)
 85     {/* 通過for迴圈使得pTemp指向要刪除的節點 */
 86         pPre=pTemp;
 87         pTemp=pTemp->next;
 88     }
 89     pPre->next=pTemp->next;/* 連接刪除兩邊的節點 */
 90     free(pTemp);/* 釋放要刪除節點的記憶體空間 */
 91     iCount--;/* 減少鏈表中的結點個數 */
 92 }
 93 
 94 int main()
 95 {
 96     struct Student *pHead;/* 定義頭結點 */ 
 97     pHead=Create();/* 創建節點 */
 98     pHead=Insert(pHead);/* 插入節點 */ 
 99     Delete(pHead,2);/* 刪除第二個節點 */ 
100     Print(pHead);/* 輸出鏈表 */ 
101     return 0;
102 }
10.7 鏈表的刪除操作

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在lanmp/wdcp/wdOS的當前版本中,預設的php都是用到5.2.17的版本如需要升級到php5.3的,可使用如下腳本升級(註:此升級無安全漏洞等原因,只為某些追求高版本或應用需求需要高版本,對於無這個必要的同學,可不用升級)wget http://down.wdlinux.cn/in/ph
  • 信息化管理軟體基本上就是基於資料庫的開發,而Delphi在資料庫開發有著顯著優勢, 而正因為Delphi的便捷,很多程式員喜歡信手拈來,擺擺控制項,寫寫代碼, 而隨著開發的需求的多樣化,程式變得越來越臃腫,越來越難以維護,幾乎沒有擴展空間。 我改過一段時間的爛代碼,深受刺激,突然發現有些程式員的思維方
  • //============================================================================ // Name : QuickSort.cpp // Author : Cheng Song // Version : // Copyrigh
  • //============================================================================ // Name : BubbleSort.cpp // Author : fffff // Version : // Copyright :
  • 1 /* 遍歷二叉樹就是以一定的規則將二叉樹中的節點排列成一個線性 2 * 序列,從而得到二叉樹節點的各種遍歷序列,其實質是:對一個 3 * 非線性的結構進行線性化。使得在這個訪問序列中每一個節點都 4 * 有一個直接前驅和直接後繼。 5 * 傳統的鏈式結構只能體現一種父子關係,¥不能直接得到節點在
  • 主要用於Web服務日誌最新行查看。 package main import( "fmt" "os" "bytes") const ( defaultBufSize = 4096) func tail( filename string, n int ) (lines []string,err erro
  • 文件操作介面類: package com.souvc.util.test; import java.io.File; public interface MyFileUtil { /** * 方法名:createDefaultFolder</br> * 詳述:創建預設文件夾-重覆的時候 會自動給文件命
  • const在C++中很常用,在編程中也建議多使用const去告訴編譯器和其他程式員某個值應該保持不變。 const可以用在很多地方: (1)用在classes外部修飾global或namespace作用域中的常量 (2)修飾文件、函數、或區塊作用域中被聲明為static的對象 (3)修飾classe
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...