数据结构_单链表的插入与删除_C语言源代码
2014-07-11 22:17
423 查看
int ListInsert(LNode *&L, int i, int e)//插入
{
int j=0;
LNode *p=L;
LNode *s=NULL;
while(j<i-1)
{
p=p->next;
++j;
if(NULL==p)
{
return 0;
}
}
if(j>i-1)
{
return 0;
}
s=(LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return 1;
}
int ListDelete(LNode *&L,int i,int &e)//删除
{
int j=0;
LNode *p=L;
LNode *q=NULL;
while(j<i-1)
{
p=p->next;
++j;
if(NULL==p)
{
return 0;
}
}
if(j>i-1)
{
return 0;
}
q=p->next;
e=q->data;
p->next=q->next;
free(q);
return 1;
}
可运行的完整代码如下:
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;;
}LNode;
void InitList(LNode *&L)
{
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
}
void CreateListR(LNode *&L,int a[],int n)
{
int i;
LNode *r=L,*s=NULL;
for(i=0;i<n;i++)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void VisitList(LNode *L)
{
LNode *s=L->next;
while(NULL!=s)
{
printf("%d\n",s->data);
s=s->next;
}
}
void ListDelete(LNode *&L,int elem)
{
LNode *s=L;
LNode *q=NULL;
while(s->next!=NULL)
{
if(s->next->data==elem)
{
q=s->next;
s->next=q->next;
free(q);
return ;
}
s=s->next;
}
}
int ListInsert(LNode *&L, int i, int e)
{
int j=0;
LNode *p=L;
LNode *s=NULL;
while(j<i-1)
{
p=p->next;
++j;
if(NULL==p)
{
return 0;
}
}
if(j>i-1)
{
return 0;
}
s=(LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return 1;
}
int ListDelete(LNode *&L,int i,int &e)
{
int j=0;
LNode *p=L;
LNode *q=NULL;
while(j<i-1)
{
p=p->next;
++j;
if(NULL==p)
{
return 0;
}
}
if(j>i-1)
{
return 0;
}
q=p->next;
e=q->data;
p->next=q->next;
free(q);
return 1;
}
int main(void)
{
const int N=5;
int temp;
int a
={2,3,4,5,6};
LNode *L;
InitList(L);
CreateListR(L,a,N);
VisitList(L);
putchar(10);
// ListDelete(L,6);//删除元素6
// VisitList(L);
if(ListInsert(L,2,10)==1)
{
VisitList(L);
}
else
{
printf("input error!");
}
ListDelete(L,2,temp);
putchar(10);
VisitList(L);
printf("===========%d\n",temp);
system("pause");
return 0;
}
{
int j=0;
LNode *p=L;
LNode *s=NULL;
while(j<i-1)
{
p=p->next;
++j;
if(NULL==p)
{
return 0;
}
}
if(j>i-1)
{
return 0;
}
s=(LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return 1;
}
int ListDelete(LNode *&L,int i,int &e)//删除
{
int j=0;
LNode *p=L;
LNode *q=NULL;
while(j<i-1)
{
p=p->next;
++j;
if(NULL==p)
{
return 0;
}
}
if(j>i-1)
{
return 0;
}
q=p->next;
e=q->data;
p->next=q->next;
free(q);
return 1;
}
可运行的完整代码如下:
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;;
}LNode;
void InitList(LNode *&L)
{
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
}
void CreateListR(LNode *&L,int a[],int n)
{
int i;
LNode *r=L,*s=NULL;
for(i=0;i<n;i++)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void VisitList(LNode *L)
{
LNode *s=L->next;
while(NULL!=s)
{
printf("%d\n",s->data);
s=s->next;
}
}
void ListDelete(LNode *&L,int elem)
{
LNode *s=L;
LNode *q=NULL;
while(s->next!=NULL)
{
if(s->next->data==elem)
{
q=s->next;
s->next=q->next;
free(q);
return ;
}
s=s->next;
}
}
int ListInsert(LNode *&L, int i, int e)
{
int j=0;
LNode *p=L;
LNode *s=NULL;
while(j<i-1)
{
p=p->next;
++j;
if(NULL==p)
{
return 0;
}
}
if(j>i-1)
{
return 0;
}
s=(LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return 1;
}
int ListDelete(LNode *&L,int i,int &e)
{
int j=0;
LNode *p=L;
LNode *q=NULL;
while(j<i-1)
{
p=p->next;
++j;
if(NULL==p)
{
return 0;
}
}
if(j>i-1)
{
return 0;
}
q=p->next;
e=q->data;
p->next=q->next;
free(q);
return 1;
}
int main(void)
{
const int N=5;
int temp;
int a
={2,3,4,5,6};
LNode *L;
InitList(L);
CreateListR(L,a,N);
VisitList(L);
putchar(10);
// ListDelete(L,6);//删除元素6
// VisitList(L);
if(ListInsert(L,2,10)==1)
{
VisitList(L);
}
else
{
printf("input error!");
}
ListDelete(L,2,temp);
putchar(10);
VisitList(L);
printf("===========%d\n",temp);
system("pause");
return 0;
}
相关文章推荐
- 【数据结构】C语言实现链表节点的插入和删除
- 数据结构复习————单链表在指定位置插入删除元素
- 初学线性表:1.数据结构线性表通过插入部分源代码推导删除
- 平衡二叉树的 插入 删除 查找 等功能c语言实现 数据结构
- C语言进阶-第18讲:单链表的遍历、创建、插入和删除结点
- 数据结构----单链表的创建、插入、删除、读取、遍历
- C语言单链表的创建、插入、查找、删除、求长、排序、遍历
- 【数据结构】C语言实现链表节点的插入和删除
- 数据结构(三)——单链表的插入与删除
- 平衡二叉树的C语言实现(创建、插入、查找、删除、旋转)【数据结构】
- 数据结构 单链表的创建 插入 删除
- c语言数据结构分析2之 链表插入删除
- 数据结构中单链表的实现+单链表的C语言实现源代码
- C语言之单链表的插入、删除与查找
- 4.数据结构单链表之链表成员的插入与删除
- 平衡二叉树之C语言实现(插入、删除,分裂、合并)附源代码
- C语言实现单链表的创建、插入、删除
- 数据结构双向循环链表的C语言实现(插入,查询,删除)
- C语言---单链表的插入、删除、查找操作
- 数据结构学习(三)——单链表的操作之查找、删除、插入。