线性表中单链表基本功能实现
2017-09-27 23:41
387 查看
#include <stdio.h>
#include <iostream>
//#include <sdlib.h>
#include <malloc.h>
#include <list>
#include <vector>
#define MaxSize 50
using namespace std;
typedef char ElemType;
typedef struct LNone{
ElemType data;
struct LNone *next;
}LinkNone;
void InitList(LinkNone *&L)
{
L=(LinkNone *)malloc(sizeof(LinkNone));
L->next=NULL;
}//初始化单链表
void DestroyList(LinkNone *&L)
{
LinkNone *pre=L,*p=L->next;
while(p!=NULL)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
}
bool ListEmpty(LinkNone *L)
{
return (L->next==NULL);
}
int ListLength(LinkNone *L)
{
int n=0;
LinkNone *p=L;
while(p->next!=NULL)
{
n++;
p=p->next;
}
return(n);
}
void DispList(LinkNone *L)
{
LinkNone *p=L->next;
while(p!=NULL)
{
printf("%c ",p->data);
p=p->next;
}
printf("\n");
}
bool GetElem(LinkNone *L,int i,ElemType &e)
{
int j=0;
LinkNone *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 LocataElem1(LinkNone *L,ElemType e)
{
int i=1;
LinkNone *p=L->next;
while(p!=NULL&&p->data!=e)
{
p=p->next;
i++;
}
if(p==NULL)
return (0);
else
return (i);
}
int LocataElem2(LinkNone *L,ElemType e)
{
int i=1;
LinkNone *p=L->next;
while(p!=NULL&&p->data!=e)
{
p=p->next;
i++;
}
if(p==NULL)
return (0);
else
return (i);
}
bool ListInsert(LinkNone *&L,int i,ElemType e)
{
int j=0;
LinkNone *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=(LinkNone *)malloc(sizeof(LinkNone));
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
}
bool ListDelete(LinkNone *&L,int i,ElemType e)
{
int j=0;
LinkNone *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;
}
}
int main()
{
LinkNone *L;
ElemType e;
InitList(L);
ListInsert(L,1,'a');
ListInsert(L,2,'b');
ListInsert(L,3,'c');
ListInsert(L,4,'d');
ListInsert(L,5,'e');
DispList(L);
printf("%d\n",ListLength(L));
if(!ListEmpty(L))
printf("此链表不为空\n");
else
printf("此链表为空\n");
if(GetElem(L,3,e))
printf("%c\n",e);
else
printf("不存在此值\n");
printf("%d\n",LocataElem1(L,'a'));
ListInsert(L,4,'f');
DispList(L);
ListDelete(L,3,e);//无序
DispList(L);
DestroyList(L);
//DispList(L);
//return 0;
}
输出数据:
#include <iostream>
//#include <sdlib.h>
#include <malloc.h>
#include <list>
#include <vector>
#define MaxSize 50
using namespace std;
typedef char ElemType;
typedef struct LNone{
ElemType data;
struct LNone *next;
}LinkNone;
void InitList(LinkNone *&L)
{
L=(LinkNone *)malloc(sizeof(LinkNone));
L->next=NULL;
}//初始化单链表
void DestroyList(LinkNone *&L)
{
LinkNone *pre=L,*p=L->next;
while(p!=NULL)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
}
bool ListEmpty(LinkNone *L)
{
return (L->next==NULL);
}
int ListLength(LinkNone *L)
{
int n=0;
LinkNone *p=L;
while(p->next!=NULL)
{
n++;
p=p->next;
}
return(n);
}
void DispList(LinkNone *L)
{
LinkNone *p=L->next;
while(p!=NULL)
{
printf("%c ",p->data);
p=p->next;
}
printf("\n");
}
bool GetElem(LinkNone *L,int i,ElemType &e)
{
int j=0;
LinkNone *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 LocataElem1(LinkNone *L,ElemType e)
{
int i=1;
LinkNone *p=L->next;
while(p!=NULL&&p->data!=e)
{
p=p->next;
i++;
}
if(p==NULL)
return (0);
else
return (i);
}
int LocataElem2(LinkNone *L,ElemType e)
{
int i=1;
LinkNone *p=L->next;
while(p!=NULL&&p->data!=e)
{
p=p->next;
i++;
}
if(p==NULL)
return (0);
else
return (i);
}
bool ListInsert(LinkNone *&L,int i,ElemType e)
{
int j=0;
LinkNone *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=(LinkNone *)malloc(sizeof(LinkNone));
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
}
bool ListDelete(LinkNone *&L,int i,ElemType e)
{
int j=0;
LinkNone *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;
}
}
int main()
{
LinkNone *L;
ElemType e;
InitList(L);
ListInsert(L,1,'a');
ListInsert(L,2,'b');
ListInsert(L,3,'c');
ListInsert(L,4,'d');
ListInsert(L,5,'e');
DispList(L);
printf("%d\n",ListLength(L));
if(!ListEmpty(L))
printf("此链表不为空\n");
else
printf("此链表为空\n");
if(GetElem(L,3,e))
printf("%c\n",e);
else
printf("不存在此值\n");
printf("%d\n",LocataElem1(L,'a'));
ListInsert(L,4,'f');
DispList(L);
ListDelete(L,3,e);//无序
DispList(L);
DestroyList(L);
//DispList(L);
//return 0;
}
输出数据:
相关文章推荐
- c语言单向链表的基本功能的实现
- 用链表实现带菜单功能的一元多项式的基本操作 (Use linked list to realize the basic operation of a one-dimensional polynom)
- 链表的建立和一些基本功能的实现
- 链表的基本功能实现
- 实现单向链表的基本功能
- Queue类模板的链表实现(基本功能)
- C++实现双链表的基本功能
- 数据结构--单链表基本功能实现程序…
- Java实现单向链表的基本功能详解
- Java实现单向链表基本功能
- 链表队列基本功能的实现
- 用C实现基本链表功能
- 数据结构与算法学习 第1季02 链表的基本功能 C++实现
- HashMap存取效率高原因、ArrayList和LinkedList区别、JAVA实现链表的基本功能
- 线性表基本功能的c语言实现
- 单链表的基本功能 与扩展功能的实现
- 【C++/STL】list的实现(没有采用迭代器和空间配置器所实现的双向链表的基本功能)
- 数据结构C语言版--单链表的基本功能实现
- 双链表一些基本功能的简单实现