单链表-头插法、尾插法和基本运算
来源:互联网 发布:线切割控制编程系统 编辑:程序博客网 时间:2024/06/04 18:10
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LinkNode;
//头插法
void CreateListF(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
for(int i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=a[i];
s->next=L->next ;
L->next =s;
}
}
//尾插法
//void CreateListR(LinkNode *&L,ElemType a[],int n)
//{
// LinkNode *s,*r;
// L=(LinkNode *)malloc(sizeof(LinkNode));
// L->next =NULL;
// for(int i=0;i<n;i++)
// {
// s=(LinkNode *)malloc(sizeof(LinkNode));
// s->data=a[i];
// r->next =s;
// r=s;
// }
// r->next =NULL;
//}
void InitList(LinkNode *&L)
{
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next =NULL;
}
void DestroyList(LinkNode *&L)
{
LinkNode *pre=L,*p=pre->next ;
while(p!=NULL)
{
free(pre);
pre=p;
p=pre->next ;
}
free(pre);
}
bool ListEmpty(LinkNode *L)
{
return (L->next ==NULL);
}
int ListLength(LinkNode *L)
{
int i=0;
LinkNode *p=L;
while(p->next !=NULL)
{
i++;
p=p->next ;
}
return (i);
}
void DispList(LinkNode *L)
{
LinkNode *p=L->next ;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next ;
}
printf("\n");
}
bool GetElem(LinkNode *L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L;
if(i<=0)
return false;
while(j<i&&p!=NULL)
{
j++;
p=p->next ;
}
if(p==NULL)
return false;
else
{
e=p->data ;
return true;
}
}
int LocateElem(LinkNode *L,ElemType e)
{
int i=1;
LinkNode *p=L->next;
while(p!=NULL&&p->data !=e)
{
p=p->next ;
i++;
}
if(p==NULL)
return (0);
else
return (i);
}
bool ListInsert(LinkNode *&L,int i,ElemType e)
{
int j=0;
LinkNode *p=L,*s;
if(i<=0)
return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next ;
}
if(p==NULL)
return false;
else
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=e;
s->next=p->next;
p->next =s;
return true;
}
}
bool ListDelete(LinkNode *&L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L,*q;
if(i<=0)
return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next ;
}
if(p==NULL)
return false;
else{
q=p->next ;
if(q==NULL)
return false;
e=q->data ;
p->next =q->next ;
free(q);
return true;
}
}
void p1(LinkNode *&L)
{
LinkNode *p,*pre,*q;
p=L->next->next;
L->next->next=NULL;
while(p!=NULL)
{
q=p->next;
pre=L;
while(pre->next!=NULL&&pre->next->data>p->data)
pre=pre->next;
p->next=pre->next;
pre->next=p;
p=q;
}
}
int main()
{
LinkNode *h;
ElemType e;
int a[10]={5,4,3,2,1};
printf("单链表的基本运算如下:\n");
printf(" (1)初始化单链表h\n");
InitList(h);
printf(" (2)依次采用头插入法插入a,b,c,d,e元素\n");
// ListInsert(h,1,'a');
// ListInsert(h,2,'b');
// ListInsert(h,3,'c');
// ListInsert(h,4,'d');
// ListInsert(h,5,'e');
CreateListF(h,a,5);
printf(" (3)输出单链表h:");
DispList(h);
printf(" (4)单链表h长度:%d\n",ListLength(h));
printf(" (5)单链表h为%s\n",(ListEmpty(h)?"空":"非空"));
GetElem(h,3,e);
printf(" (6)单链表h的第3个元素:%d\n",e);
printf(" (7)元素a的位置:%d\n",LocateElem(h,1));
printf(" (8)在第4个元素位置上插入f元素\n");
ListInsert(h,4,6);
printf(" (9)输出单链表h:");
DispList(h);
printf(" (10)删除h的第3个元素\n");
ListDelete(h,3,e);
printf(" (11)输出单链表h:");
DispList(h);
printf(" (12)释放单链表h\n");
DestroyList(h);
printf(" (13)递增排序:\n");
p1(h);
return 1;
}
#include <malloc.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LinkNode;
//头插法
void CreateListF(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
for(int i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=a[i];
s->next=L->next ;
L->next =s;
}
}
//尾插法
//void CreateListR(LinkNode *&L,ElemType a[],int n)
//{
// LinkNode *s,*r;
// L=(LinkNode *)malloc(sizeof(LinkNode));
// L->next =NULL;
// for(int i=0;i<n;i++)
// {
// s=(LinkNode *)malloc(sizeof(LinkNode));
// s->data=a[i];
// r->next =s;
// r=s;
// }
// r->next =NULL;
//}
void InitList(LinkNode *&L)
{
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next =NULL;
}
void DestroyList(LinkNode *&L)
{
LinkNode *pre=L,*p=pre->next ;
while(p!=NULL)
{
free(pre);
pre=p;
p=pre->next ;
}
free(pre);
}
bool ListEmpty(LinkNode *L)
{
return (L->next ==NULL);
}
int ListLength(LinkNode *L)
{
int i=0;
LinkNode *p=L;
while(p->next !=NULL)
{
i++;
p=p->next ;
}
return (i);
}
void DispList(LinkNode *L)
{
LinkNode *p=L->next ;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next ;
}
printf("\n");
}
bool GetElem(LinkNode *L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L;
if(i<=0)
return false;
while(j<i&&p!=NULL)
{
j++;
p=p->next ;
}
if(p==NULL)
return false;
else
{
e=p->data ;
return true;
}
}
int LocateElem(LinkNode *L,ElemType e)
{
int i=1;
LinkNode *p=L->next;
while(p!=NULL&&p->data !=e)
{
p=p->next ;
i++;
}
if(p==NULL)
return (0);
else
return (i);
}
bool ListInsert(LinkNode *&L,int i,ElemType e)
{
int j=0;
LinkNode *p=L,*s;
if(i<=0)
return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next ;
}
if(p==NULL)
return false;
else
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=e;
s->next=p->next;
p->next =s;
return true;
}
}
bool ListDelete(LinkNode *&L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L,*q;
if(i<=0)
return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next ;
}
if(p==NULL)
return false;
else{
q=p->next ;
if(q==NULL)
return false;
e=q->data ;
p->next =q->next ;
free(q);
return true;
}
}
void p1(LinkNode *&L)
{
LinkNode *p,*pre,*q;
p=L->next->next;
L->next->next=NULL;
while(p!=NULL)
{
q=p->next;
pre=L;
while(pre->next!=NULL&&pre->next->data>p->data)
pre=pre->next;
p->next=pre->next;
pre->next=p;
p=q;
}
}
int main()
{
LinkNode *h;
ElemType e;
int a[10]={5,4,3,2,1};
printf("单链表的基本运算如下:\n");
printf(" (1)初始化单链表h\n");
InitList(h);
printf(" (2)依次采用头插入法插入a,b,c,d,e元素\n");
// ListInsert(h,1,'a');
// ListInsert(h,2,'b');
// ListInsert(h,3,'c');
// ListInsert(h,4,'d');
// ListInsert(h,5,'e');
CreateListF(h,a,5);
printf(" (3)输出单链表h:");
DispList(h);
printf(" (4)单链表h长度:%d\n",ListLength(h));
printf(" (5)单链表h为%s\n",(ListEmpty(h)?"空":"非空"));
GetElem(h,3,e);
printf(" (6)单链表h的第3个元素:%d\n",e);
printf(" (7)元素a的位置:%d\n",LocateElem(h,1));
printf(" (8)在第4个元素位置上插入f元素\n");
ListInsert(h,4,6);
printf(" (9)输出单链表h:");
DispList(h);
printf(" (10)删除h的第3个元素\n");
ListDelete(h,3,e);
printf(" (11)输出单链表h:");
DispList(h);
printf(" (12)释放单链表h\n");
DestroyList(h);
printf(" (13)递增排序:\n");
p1(h);
return 1;
}
阅读全文
0 0
- 单链表-头插法、尾插法和基本运算
- 数据类型和基本运算
- 数据类型和基本运算
- 基本运算符和表达式
- 基本数据类型和运算符
- JavaScript基本数据类型和运算
- 基本数据类型和运算符
- 【python】基本运算和表达式
- c++基本数据结构和基本运算
- 单链表的基本运算
- 单链表的基本运算
- 单链表的基本运算
- 单链表的基本运算
- 10.4单链表基本运算
- 单链表各种基本的运算
- C 单链表的基本运算
- 单链表的各种基本运算
- 单链表的各种基本运算
- Android学习《第一行代码》02
- [BZOJ2006][NOI2010]超级钢琴(st表+堆贪心)
- MSP430单片机USART串口发送字符和字符串
- elasticsearch 配置外网访问
- Vulkan规范:第四章 4.1
- 单链表-头插法、尾插法和基本运算
- okhttp学习
- Spring学习笔记之通过注解配置Bean(1)
- 文章标题
- 函数的调用过程(栈帧)
- Error:(17, 24) 警告: Application namespace for attribute app:url will be ignored.
- Vulkan规范:第四章 4.2
- Java设计模式--享元模式
- Java SE 7 Archive Downloads