(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.mzph.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…