c语言代码实现
C语言实现 线性表(单链表)的相关操作,实现了如下方法:
- 初始化单链表
InitLinkList(LinkList &L); - 销毁单链表
DestroyLinkList(LinkList &L); - 清空单链表
ClearLinkList(LinkList &L); - 在单链表指定位置插入元素
LinkListInsert(LinkList &L, int i, ElemType e); - 单链表头插法
LinkListInsert_h(LinkList &L, ElemType e); - 单链表尾插法
LinkListInsert_r(LinkList &L, ElemType e); - 获取单链表的长度
LinkListLength(LinkList L); - 在单链表中查找指定元素所在的位置
LocateElem(LinkList L, ElemType e); - 在单链表中查找指定元素是否存在,存在就把该节点作为函数返回值返回
LocateElem_(LinkList L, ElemType e); - 在单链表中获取指定位置的元素
GetElem(LinkList L, int i, ElemType &e); - 判断单链表是否为空
IsEmpty(LinkList L); - 删除单链表指定位置的元素
LinkListDelete(LinkList &L, int i, ElemType &e);
写的匆忙,如有错误,请大家指正一下。
#include <stdio.h>
#include <cstdlib>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef char ElemType;
typedef struct node{
ElemType data;
struct node *next;
}LinkListNode, *LinkList;
Status InitLinkList(LinkList &L);
void DestroyLinkList(LinkList &L);
void ClearLinkList(LinkList &L);
Status LinkListInsert(LinkList &L, int i, ElemType e);
Status LinkListInsert_h(LinkList &L, ElemType e);
Status LinkListInsert_r(LinkList &L, ElemType e);
int LinkListLength(LinkList L);
int LocateElem(LinkList L, ElemType e);
LinkListNode* LocateElem_(LinkList L, ElemType e);
Status GetElem(LinkList L, int i, ElemType &e);
Status IsEmpty(LinkList L);
Status LinkListDelete(LinkList &L, int i, ElemType &e);
int main(){
LinkList list;
InitLinkList(list);
ElemType e1 = 'a';
ElemType e2 = 'b';
ElemType e3 = 'c';
ElemType e4 = 'd';
ElemType e5 = 'e';
LinkListInsert(list, 1, e1);
LinkListInsert(list, 2, e2);
LinkListInsert(list, 3, e3);
LinkListInsert(list, 4, e4);
LinkListInsert(list, 5, e5);
for(int i = 0; i < LinkListLength(list); i++){
ElemType e;
GetElem(list, i+1, e);
printf("%c\t", e);
}
Status s = IsEmpty(list);
printf("\n是否为空线性表:%d\n", s);
int length = LinkListLength(list);
printf("线性表的长度:%d\n", length);
ElemType e6;
LinkListDelete(list, 2, e6);
printf("被删除的元素:%c\n", e6);
for(int i = 0; i < LinkListLength(list); i++){
ElemType e;
GetElem(list, i+1, e);
printf("%c\t", e);
}
printf("\n");
ElemType c = 'c';
int index = LocateElem(list, c);
printf("元素c所在的序号是:%d\n", index);
ElemType e7 = '7';
ElemType e8 = '8';
LinkListInsert_h(list, e7);
LinkListInsert_r(list, e8);
for(int i = 0; i < LinkListLength(list); i++){
ElemType e;
GetElem(list, i+1, e);
printf("%c\t", e);
}
printf("\n");
ElemType e9 = 'c';
LinkListNode *locateNode = LocateElem_(list, e9);
printf("查找到的结点的数据域:%c,下一个结点的地址:%p\n", locateNode->data, locateNode->next);
ClearLinkList(list);
s = IsEmpty(list);
printf("是否为空线性表:%d\n", s);
DestroyLinkList(list);
return 0;
}
Status InitLinkList(LinkList &L){
L = new LinkListNode;
if (!L){
printf("单链表初始化失败!");
return OVERFLOW;
}
L->next = NULL;
return OK;
}
Status IsEmpty(LinkList L){
return L->next? FALSE: TRUE;
}
void DestroyLinkList(LinkList &L){
for(LinkListNode *p = L; p; p = L->next){
L = p->next;
delete p;
}
}
void ClearLinkList(LinkList &L){
DestroyLinkList(L->next);
LinkListNode *p1 = L->next, *p2;
while(p1){
p2 = p1->next;
delete p1;
p1 = p2;
}
L->next = NULL;
}
int LinkListLength(LinkList L){
int count = 0;
for(LinkListNode *p = L->next; p; (p = p->next), count++);
return count;
}
Status GetElem(LinkList L, int i, ElemType &e){
LinkListNode *p = L;
for(int k = 1; k <= i && p; k++){
p = p->next;
if (k != i) continue;
e = p->data;
return OK;
}
return ERROR;
}
int LocateElem(LinkList L, ElemType e){
LinkListNode *p = L->next;
for(int i = 1; p; i++){
if(p->data == e) return i;
p = p->next;
}
return 0;
}
LinkListNode* LocateElem_(LinkList L, ElemType e){
LinkListNode *p = L->next;
while(p){
if(p->data == e) return p;
p = p->next;
}
return NULL;
}
Status LinkListInsert(LinkList &L, int i, ElemType e){
LinkListNode *p = L;
for(int k = 1; k <= i && p; k++, p = p->next){
if(k != i) continue;
LinkListNode *newNode = (LinkListNode*)malloc(sizeof(LinkListNode));
newNode->data = e;
newNode->next = p->next;
p->next = newNode;
return OK;
}
printf("插入失败!\n");
return ERROR;
}
Status LinkListDelete(LinkList &L, int i, ElemType &e){
LinkListNode *p = L->next;
int deletePreIndex = i - 1;
for(int k = 1; k <= deletePreIndex; k++, p = p->next){
if(k != deletePreIndex) continue;
e = p->next->data;
LinkListNode *delNode = p->next;
p->next = delNode->next;
delete delNode;
return OK;
}
return ERROR;
}
Status LinkListInsert_h(LinkList &L, ElemType e){
LinkListNode *newNode = new LinkListNode;
newNode->data = e;
newNode->next = L->next;
L->next = newNode;
return OK;
}
Status LinkListInsert_r(LinkList &L, ElemType e){
LinkListNode *p = L;
for(; p->next; p = p->next);
LinkListNode *newNode = new LinkListNode;
newNode->data = e;
newNode->next = NULL;
p->next = newNode;
return OK;
}
|