c语言单向链表的简单实现,隐藏head节点
来源:互联网 发布:开学典礼上的讲话知乎 编辑:程序博客网 时间:2024/04/28 11:10
前些天写了一个c语言实现的顺序线性表,今天用c实现了一下单向链表。
ADT:
数据对象为:用一个结构体定义的节点,其中包括一个data域,next指针
数据操作有:创建链表,销毁链表,增删查改,求长
链表的计数还是从0开始,head节点影藏,不计入长度,下面这种图帮助理解
将head对用户不可见
I的值可取0至size
此中的size=5; j=0;i=3
操作插入一个元素至position3,那么首相要将p定位到position 2
这部的操作为
p=head;
While(j<i){
p=p->next;
j++;
}
删除position 3处的元素,首先还是要将p定位值postiion 2;且i的值可取从0只size-1
查找 position3 处的元素,要将p定位到position 3;
这里可以让p=head->next,那么p移动的节点数还是为3
相应代码如下:
linkList.h
#include <stdio.h>#include <stdlib.h>#define Elem int//define the nodetypedef struct Node{Elem data;struct Node *next;}LNode; //defien the operation//initializing/create the linklist,void createList(LNode **head);//PS:using a point's point to create the linklist, or it will be fail//count from the head of the linklist,insert a elem e into the position i,the i is counted from 0 to sizevoid insertList(LNode *head,int i,Elem e);//append a elem into the tail of the linklistvoid append(LNode *head,Elem e);//delete a elem on the postition i,the i is counted from 0 to size-1void deleteList(LNode *head,int i);//Get the elem on the position i,the i is counted from 0 to size-1,return the ElemElem getList(LNode *head,int i);//updaet the List where the position is i to Elem e,the i is counted from 0 to size-1void updateList(LNode *head,int i,Elem e);//calculate the length of the list,return the lengthint getLength(LNode *head);//destory the listvoid destoryList(LNode **head);
linkList.c
#include "linkList.h"void createList(LNode **head)//PS:using a point's point to create the linklist, or it will be failed{*head=(LNode*)malloc(sizeof(LNode));(*head)->data=0;//head->data restored the size of the linklist;(*head)->next=NULL;}//count from the head of the linklist,insert a elem e into the position i,the i is counted from 0 to sizevoid insertList(LNode *head,int i,Elem e){LNode *p,*q;//p is used for loacting,and q is the inserted node int j=0;p=head;if(i<0||i>head->data){printf("insert error,check the value of i is between 0 to %d! \n",head->data);exit(1);}while(j<i){p=p->next;j++;}//insertq=(LNode *)malloc(sizeof(LNode));q->data=e;//link the pre and the next nodeq->next=p->next;p->next=q;head->data++;}//append a elem into the tail of the linklistvoid append(LNode *head,Elem e){LNode *p,*q;p=head;if(head==NULL){printf("no linklist here!\n");exit(1);}//locate the p to the last nodewhile(p->next!=NULL){p=p->next;}//insert the node q to the lastq=(LNode*)malloc(sizeof(LNode));q->data=e;//linkq->next=p->next;p->next=q;head->data++;}//delete a elem on the postition i,the i is counted from 0 to size-1void deleteList(LNode *head,int i){LNode *p,*q;int j=0;p=head;if(i<0||i>(head->data)-1){printf("delete error,check the value of i is between 0 to %d \n",(head->data)-1);exit(1);}while(j<i){p=p->next;j++;}//deleteq=p->next;p->next=(p->next)->next;free(q);head->data--;}//Get the elem on the position i,the i is counted from 0 to size-1,return the ElemElem getList(LNode *head,int i){LNode *p;int j=0;p=head->next;if(i<0||i>(head->data)-1){printf("getList error,check the value of i is between 0 to %d \n",(head->data)-1);exit(1);}while(j<i){p=p->next;j++;}return p->data;}//updaet the List where the position is i to Elem e,the i is counted from 0 to size-1void updateList(LNode *head,int i,Elem e){LNode *p;int j=0;p=head->next;if(i<0||i>(head->data)-1){printf("updateList error,check the value of i is between 0 to size-1 \n");exit(1);}while(j<i){p=p->next;j++;}p->data=e;}//calculate the length of the list,return the lengthint getLength(LNode *head){if(head==NULL){printf("no linklist here!\n");exit(1);}return head->data;}//destory the listvoid destoryList(LNode **head){LNode *p,*q;p=*head;while(p!=NULL){q=p;p=p->next;free(q);}*head=NULL;}
testlink.c
#include "linkList.h"#include <time.h>void main(){clock_t start,finish;//运行时间节点double time;//运行时间int size;int i;LNode *head;createList(&head);size=getLength(head);printf("size=%d\n",size);for(i=1;i<=10;i++){append(head,i);//test append}size=getLength(head);printf("size=%d\n",size);for(i=0;i<10;i++){printf("data=%3d\tposition=%d\n",getList(head,i),i);//test getList}insertList(head,0,66);insertList(head,11,111);insertList(head,5,55);//test insertListsize=getLength(head);printf("size=%d\n",size);for(i=0;i<size;i++){printf("data=%3d\tposition=%d\n",getList(head,i),i);}deleteList(head,0);deleteList(head,11);deleteList(head,5);//test deleteList;size=getLength(head);printf("size=%d\n",size);for(i=0;i<size;i++){printf("data=%3d\tposition=%d\n",getList(head,i),i);}updateList(head,0,89);updateList(head,9,99);updateList(head,7,100);size=getLength(head);printf("size=%d\n",size);for(i=0;i<size;i++){printf("data=%3d\tposition=%d\n",getList(head,i),i);}size=getLength(head);printf("size=%d\n",size);destoryList(&head);size=getLength(head);printf("size=%d\n",size);}
PS:
1、第一次写的时候,在head中的data保存的不是size,这使得程序效率降低,复杂性增加
2、写testLink.c文件时,将其后缀名写成了.cpp,发现报错,后来找到原因为链接错误,只有C才能够找到某些函数
3、关于计算size,如果不把head隐藏,那么对我来讲,计数时容易混淆,所以将其“对外部隐藏”
4、虽然设置了计算运行时间的参数,但是并没有去使用,因为计算的时候节点数太小了,导致运行时间为0.000000
start=clock();//要计算的起始位置
<代码段>
finish=clock();//要计算的终点位置
time=(double)((finish-start)/CLOCKS_PER_SEC);
5、createList和destoryList函数的参数都为LNode **head,为什么后面的函数传入的只是LNode *head,这部分知识点将在另外一篇博文中分享。
- c语言单向链表的简单实现,隐藏head节点
- C语言实现一个简单的单向链表list
- C语言-------实现一个简单的单向链表
- C语言实现简单的单向链表
- C实现简单单向链表,一次遍历查找倒数第k个节点的值
- C语言单向链表的实现
- C语言实现的单向链表
- 单向链表的C语言实现
- C语言单向链表的实现
- c语言单向链表的实现
- C语言单向链表的实现
- C语言单向链表的实现
- C语言单向链表的实现
- C语言单向链表的实现
- C语言单向链表的实现
- 转载的标准文档:C语言实现一个简单的单向链表list
- C语言写的简单的单向链表
- c语言单向链表的基本功能的实现
- LeetCode: Word Break
- JSF生命周期的6个阶段
- UML类图符号
- mysql 防止插入重复数据
- android支付平台集成调研
- c语言单向链表的简单实现,隐藏head节点
- ubuntu服务器下git ssh 密钥 配置
- 经典回溯问题-----旅行员售货问题
- svn的迁移
- 揭秘百度用户得分算法
- OCP-1Z0-053-V12.02-180题
- java GUI编程-贪吃蛇游戏简单实现
- 客户端程序异常退出
- VS2010程序打包操作(超详细的)