续上文----线性表之单链表(C实现)


本文绪上文线性表之顺序表(C实现)

本文将继续使用单链表实现线性表的另外一种存储结构.这种使用链表实现的存储结构在内存中是不连续的.

C实现代码如下:

  1 #include<stdio.h>
  2 
  3 typedef struct node
  4 {
  5     int data;
  6     struct node *next;
  7 }Node;
  8 
  9 //链表的初始化
 10 Node* InitList(int number)
 11 {
 12     int i;
 13     Node *pHead=(Node *)malloc(sizeof(Node));
 14     Node *TempHead=pHead;
 15     Node *Head=pHead;
 16     int data;
 17     for(i=0;i<number;i++)
 18     {
 19         pHead=(Node *)malloc(sizeof(Node));
 20         printf("Please input the %dst node data:\n",i+1);
 21         scanf("%d",&data);
 22         pHead->data=data;
 23         pHead->next=NULL;
 24         TempHead->next=pHead;
 25         TempHead=TempHead->next;
 26     }
 27     return Head;
 28 }
 29 
 30 //显示链表
 31 void ShowList(Node *Head)
 32 {
 33     Node *TempNode=Head;
 34     Head=Head->next;
 35     while(Head->next!=NULL)
 36     {
 37         printf("%d ",Head->data);
 38         Head=Head->next;
 39     }
 40     printf("%d",Head->data);
 41     printf("\n");
 42 }
 43 
 44 //输出链表某个值的位置
 45 int ListLocation(Node *Head,int data,int number)
 46 {
 47     Node *TempNode=Head;
 48     int location=1;
 49     TempNode=TempNode->next;
 50     while(TempNode->next!=NULL)
 51     {
 52         if(TempNode->data==data)
 53             {
 54                 return location;
 55             }
 56             location++;
 57             TempNode=TempNode->next;
 58     }
 59     if(location>=number)
 60         printf("Not found!");
 61 }
 62 
 63 //输出链表某个位置的值
 64 int ListData(Node *Head,int location,int number)
 65 {
 66     if(location>number)
 67         printf("Not found!");
 68 
 69     Node *TempNode=Head;
 70     TempNode=TempNode->next;
 71     int i;
 72     for(i=1;i<=number;i++)
 73     {
 74         if(location==i)
 75             return TempNode->data;
 76         TempNode=TempNode->next;
 77     }
 78 }
 79 
 80 //头入法插入元素
 81 void HeadInsertData(Node *Head,int data)
 82 {
 83     Node *InsertNode=(Node *)malloc(sizeof(Node));
 84     InsertNode->data=data;
 85     InsertNode->next=Head->next;
 86     Head->next=InsertNode;
 87 }
 88 
 89 //尾入插入除元素
 90 void TailInsertData(Node *Head,int data)
 91 {
 92     Node *TempNode=Head;
 93     Node *InsertNode=(Node *)malloc(sizeof(Node));
 94     InsertNode->data=data;
 95     while(TempNode->next!=NULL)
 96         TempNode=TempNode->next;
 97 
 98     TempNode->next=InsertNode;
 99     InsertNode->next=NULL;
100 }
101 
102 
103 
104 //删除头结点
105 void HeadDeleteData(Node *Head)
106 {
107     Head->next=Head->next->next;
108 }
109 
110 
111 //删除尾结点
112 void TailDeleteData(Node *Head)
113 {
114     Node *TempNode=Head;
115     while(Head->next->next!=NULL)
116         Head=Head->next;
117 
118     Head->next=NULL;
119 }
120 
121 int main()
122 {
123     Node *Head;
124     int number;
125     printf("Please input the node number:\n");
126     scanf("%d",&number);
127     Head=InitList(number);
128     printf("The initital list is:\n");
129     ShowList(Head);
130 
131     int flag;
132     printf("\n\n");
133     printf("**********************Your Choice********************\n");
134     printf("****************1-输出链表某个值的位置***************\n");
135     printf("****************2-输出链表某个位置的值***************\n");
136     printf("****************3-头入法插入元素*********************\n");
137     printf("****************4-尾入法插入元素*********************\n");
138     printf("****************5-删除头结点*************************\n");
139     printf("****************6-删除尾结点*************************\n");
140     printf("****************0-退出*******************************\n");
141     printf("\n\n");
142     printf("Please input flag:\n");
143     scanf("%d",&flag);
144 
145     switch(flag)
146     {
147         case 1:
148         {
149             int data;
150             printf("Please input the data you want locate:\n");
151             scanf("%d",&data);
152             int location;
153             location=ListLocation(Head,data,number);
154             printf("The data's location is: %d",location);
155             break;
156         }
157         case 2:
158         {
159             int location;
160             printf("Please input the location you want  data:\n");
161             scanf("%d",&location);
162             int data;
163             data=ListData(Head,location,number);
164             printf("The location's data is: %d\n",data);
165             break;
166         }
167         case 3:
168         {
169             int data;
170             printf("Please input the data you want insert in head:\n");
171             scanf("%d",&data);
172             HeadInsertData(Head,data);
173             ShowList(Head);
174             break;
175         }
176         case 4:
177         {
178             int data;
179             printf("Please input the data you want insert in tail:\n");
180             scanf("%d",&data);
181             TailInsertData(Head,data);
182             ShowList(Head);
183             break;
184         }
185         case 5:
186         {
187             HeadDeleteData(Head);
188             ShowList(Head);
189             break;
190         }
191         case 6:
192         {
193             TailDeleteData(Head);
194             ShowList(Head);
195             break;
196         }
197         case 7:
198         {
199            printf("You choose to exit.\n");
200            break;
201         }
202     }
203     return 0;
204 }

结果图:


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn