单链表的定义,插入,删除,读取(二级指针,一级指针的应用)
2017-12-11 22:54
513 查看
#include <stdio.h>
#include <stdlib.h>
#define Ok 1
#define ERROR 0
typedef int Status;
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
}Node;
typedef Node* Linklist;
void InitList(Linklist *Lp) //这里*Lp就是头指针,Lp是头指针的地址,*Lp为二级指针,之所以用二级指针是因为之后要修改头指针的地址,形参改变实参的值必须用地址来传
{
*Lp=NULL;
printf("初始化成功!建立一个空链表\n"); //初始化用二级指针是因为我们要把头指针指向空,修改了头指针
}
Status InsertList(Linklist *Lp,int pos,ElemType e)
{
if (pos<0)
{
printf("pos is wrong\n" );
return ERROR;
}
Node *p=(Node *)malloc(sizeof(Node));
if (p==NULL)
{
printf("malloc failed!\n");
return ERROR;
}
p->data=e;
if(pos==1)
{
p->next=*Lp;/*顺序不能颠倒*/
*Lp=p;
}
else
{
int i=1;
Linklist L=*Lp;
while (L!=NULL&&i<pos-1)
{
L=L->next;
i++;
}
if (L==NULL)
{
printf("pos is wrong!\n" );
free(p);
return ERROR;
}
p->next=L->next;
L->next=p;
}
return Ok;
}
Status DeleteList(Linklist *Lp,int pos,ElemType *e)
{
if (pos<=0)
{
printf("pos is wrong \n");
return ERROR;
}
if (*Lp==NULL)
{
printf("the list is NULL!\n");
return ERROR;
}
if (pos==1)
{
Linklist L=*Lp;
*e=L->data;
*Lp=L->next;
free(L);
}
else
{
int i=1;
Linklist L=*Lp;
while(L!=NULL&&i<pos-1)
{
L=L->next;
i++;
}
if (L==NULL)
{
printf("pos is wrong\n");
return ERROR;
}
*e=L->next->data;
Linklist pt=L->next;
L->next=L->next->next;
free(pt);
}
return Ok;
}
Status GetElem(Linklist L,int pos,ElemType *e) //查看不需要用二级指针,因为不需要修改主函数内的链表
{
int i=1;
if (pos<=0)
{
printf("pos is wrong\n" );
return ERROR;
}
while(L!=NULL&&i<pos)
{
i++;
L=L->next;
}
if (L==NULL)
{
printf("没有该位置上的元素\n");
return ERROR;
}
else
{
*e=L->data;
return Ok;
}
}
Status printlist(Linklist L)
{
if (L==NULL)
{
printf("nothing in the list \n");
return ERROR;
}
while(L!=NULL)
{
printf("%d\n",L->data);
L=L->next;
}
printf("\n");
return Ok;
}
int main()
{
Linklist L1;
InitList(&L1);
while(1)
{
int sel,pos;
ElemType e;
printf("\t\t欢迎使用顺序表!\n");
printf("\t\t菜单\n");
printf("\t\t1:插入模式\n");
printf("\t\t2:显示顺序表\n");
printf("\t\t3:退出程序\n");
printf("\t\t4:删除模式\n");
printf("\t\t5:查询模式\n");
scanf("%d",&sel);
switch(sel)
{
case 1:
printf("请输入待插位置和元素\n");
scanf("%d %d",&pos,&e);
InsertList(&L1,pos,e);
break;
case 2:
printlist(L1);
break;
case 3:
return 0;
case 4:
printf("输入要删除的位置\n");
scanf("%d",&pos);
DeleteList(&L1,pos,&e);
break;
case 5:
printf("输入查询的位置\n");
scanf("%d",&pos);
GetElem(L1,pos,&e);
printf("查询到得数是:%d\n",e);
}
}
return 0;
}
#include <stdlib.h>
#define Ok 1
#define ERROR 0
typedef int Status;
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
}Node;
typedef Node* Linklist;
void InitList(Linklist *Lp) //这里*Lp就是头指针,Lp是头指针的地址,*Lp为二级指针,之所以用二级指针是因为之后要修改头指针的地址,形参改变实参的值必须用地址来传
{
*Lp=NULL;
printf("初始化成功!建立一个空链表\n"); //初始化用二级指针是因为我们要把头指针指向空,修改了头指针
}
Status InsertList(Linklist *Lp,int pos,ElemType e)
{
if (pos<0)
{
printf("pos is wrong\n" );
return ERROR;
}
Node *p=(Node *)malloc(sizeof(Node));
if (p==NULL)
{
printf("malloc failed!\n");
return ERROR;
}
p->data=e;
if(pos==1)
{
p->next=*Lp;/*顺序不能颠倒*/
*Lp=p;
}
else
{
int i=1;
Linklist L=*Lp;
while (L!=NULL&&i<pos-1)
{
L=L->next;
i++;
}
if (L==NULL)
{
printf("pos is wrong!\n" );
free(p);
return ERROR;
}
p->next=L->next;
L->next=p;
}
return Ok;
}
Status DeleteList(Linklist *Lp,int pos,ElemType *e)
{
if (pos<=0)
{
printf("pos is wrong \n");
return ERROR;
}
if (*Lp==NULL)
{
printf("the list is NULL!\n");
return ERROR;
}
if (pos==1)
{
Linklist L=*Lp;
*e=L->data;
*Lp=L->next;
free(L);
}
else
{
int i=1;
Linklist L=*Lp;
while(L!=NULL&&i<pos-1)
{
L=L->next;
i++;
}
if (L==NULL)
{
printf("pos is wrong\n");
return ERROR;
}
*e=L->next->data;
Linklist pt=L->next;
L->next=L->next->next;
free(pt);
}
return Ok;
}
Status GetElem(Linklist L,int pos,ElemType *e) //查看不需要用二级指针,因为不需要修改主函数内的链表
{
int i=1;
if (pos<=0)
{
printf("pos is wrong\n" );
return ERROR;
}
while(L!=NULL&&i<pos)
{
i++;
L=L->next;
}
if (L==NULL)
{
printf("没有该位置上的元素\n");
return ERROR;
}
else
{
*e=L->data;
return Ok;
}
}
Status printlist(Linklist L)
{
if (L==NULL)
{
printf("nothing in the list \n");
return ERROR;
}
while(L!=NULL)
{
printf("%d\n",L->data);
L=L->next;
}
printf("\n");
return Ok;
}
int main()
{
Linklist L1;
InitList(&L1);
while(1)
{
int sel,pos;
ElemType e;
printf("\t\t欢迎使用顺序表!\n");
printf("\t\t菜单\n");
printf("\t\t1:插入模式\n");
printf("\t\t2:显示顺序表\n");
printf("\t\t3:退出程序\n");
printf("\t\t4:删除模式\n");
printf("\t\t5:查询模式\n");
scanf("%d",&sel);
switch(sel)
{
case 1:
printf("请输入待插位置和元素\n");
scanf("%d %d",&pos,&e);
InsertList(&L1,pos,e);
break;
case 2:
printlist(L1);
break;
case 3:
return 0;
case 4:
printf("输入要删除的位置\n");
scanf("%d",&pos);
DeleteList(&L1,pos,&e);
break;
case 5:
printf("输入查询的位置\n");
scanf("%d",&pos);
GetElem(L1,pos,&e);
printf("查询到得数是:%d\n",e);
}
}
return 0;
}
相关文章推荐
- 二级指针实现单链表的插入、删除及 linux内核源码双向链表之奇技
- 单链表的创建,删除,插入,清空全部用一级指针也没发现什么问题啊
- 说透一级指针和二级指以及(void**)&在双链表中的应用
- c语言实现单链表&二级指针在单链表的应用
- 二级指针删除单链表
- C语言学习7 :二级指针定义,强制转换,多级指针初步,6级指针构造,错误应用*p=&a,错误应用 二级p2,void型指针的兼容性,malloc函数基本用法,malloc分配空间和堆栈空间的区别,验证malloc函数内存的分配,验证malloc函数的越界,内存泄漏,指针不能返回局部变量地址,内存分配
- 二级指针在函数中的应用(读取文件到BYTE*数组中)
- 巧妙利用二级指针删除单链表
- 说透一级指针和二级指以及(void**)&在双链表中的应用
- 用二级指针删除单链表的一个节点
- 数据结构-单链表的读取,插入与删除
- 单链表的读取,插入和删除。
- 单链表的定义建立插入删除查询
- 数据结构----单链表的创建、插入、删除、读取、遍历
- 【数据结构】单链表(一)单链表的定义,插入,删除和查找操作
- 单链表的读取、插入与删除
- 3.2.2 单链表的读取、插入和删除
- 利用二级指针插入和删除单向链表
- 【回忆c语言】从指针的定义初始化到链表的插入删除排序实现code
- 单链表实现“插入”和“删除”操作