c语言单链表的基本操作创建删除查询遍历

#include"constants.h"

typedef struct node{
ElemType elem;
struct node *next;
}SqLinkedList;

//初始化链表
Status SqLinkedInit(SqLinkedList *L) {
L = (SqLinkedList*)malloc(sizeof(SqLinkedList));
if (!L) {
return FALSE;
}
L->next = NULL;
return OK;
}

//清空链表
Status SqLinkedClear(SqLinkedList *L) {
SqLinkedList* temp;
temp = L;
if (!(L->next)) return FALSE;
while (L->next) {
temp = L->next;
free(L->next);
L->next = temp->next;
}
return OK;
}

//删除链表
Status SqLinkedDestory(SqLinkedList* L) {
if (!L) {
return FALSE;
}
free(L);
return OK;
}

//判空
Status SqLinkedEmpty(SqLinkedList L) {
if (L.next == NULL) return TRUE;
return FALSE;
}

//链长
int SqLinkedLength(SqLinkedList L) {
if (L.next!=NULL) {
return -1;
}
SqLinkedList* temp;
temp = L.next;
int n = 0;
while (temp!= NULL) {
temp = temp->next;
n++;
}
return n;
}

//取元素
Status SqLinkedGet(SqLinkedList L, int i, ElemType *e) {
if (i<1 || i>SqLinkedLength(L)) {
return ERROR;
}
SqLinkedList* temp;
temp = &L;
for (int j = 0; j < i; j++) {
temp = temp->next;
}
e = temp->elem;
return OK;
}

//定位
Status SqLinkedLocate(SqLinkedList L, ElemType e,int *i) {
if (!&L) {
return FALSE;
}
SqLinkedList* temp;
temp = L.next;
int j = 1;
for (j; temp->elem != e; j++) {
temp = temp->next;
}
i = &j;
return OK;
}
//正序建立单链表
Status SqLinkedCreate(SqLinkedList* L) {
int i = 0;
SqLinkedList* p;
SqLinkedList* temp;
L->next = NULL;
temp = L;
printf(“input length of linked:”);
scanf_s("%d", &i);
printf(“input the node:”);
while (i) {
p =(SqLinkedList*)malloc(sizeof(SqLinkedList));
if (!p) return ERROR;
p->next = NULL;
scanf_s("%d", &p->elem);
temp->next = p;
temp = p;
i–;
}
return OK;
}

//逆序建立单链表
Status SqLinkedCreateBackWard(SqLinkedList* L) {
int i = 0;
SqLinkedList* p;
L->next = NULL;
printf(“input length of linked:”);
scanf_s("%d",&i);
printf(“input the node:”);
while (i) {
p = (SqLinkedList*)malloc(sizeof(SqLinkedList));
if (!p) return ERROR;
scanf_s("%d", &p->elem);
p->next = L->next;
L->next = p;
i–;
}
printf(“ok”);
return OK;
}

//遍历单链表

Status SqLinkedShow(SqLinkedList L) {
if (L.next == NULL) {
return ERROR;
}
SqLinkedList* temp1;
temp1 = L.next;
while (temp1!=NULL) {
printf("%d->", temp1->elem);
temp1 = temp1->next;
}
printf("\n");
return OK;
}

//在第i个位置之前插入
Status SqLinkedeBeforeInsert(SqLinkedList L,int i) {
if (L->next == NULL || i < 1) return ERROR;
SqLinkedList
temp;
SqLinkedList* p;
p = (SqLinkedList*)malloc(sizeof(SqLinkedList));
printf(“input the insert node:”);
scanf_s("%d", &p->elem);
temp = L;
int n =1;
while (n < i&&temp) {
temp = temp->next;
n++;
}
p->next = temp->next;
temp->next = p;
return OK;

}

//删除结点
Status SqLinkedDelete(SqLinkedList* L, int i) {
if (i < 1 || L->next == NULL) return ERROR;
SqLinkedList* temp;
temp = L;
for (int n = 0; n < i-1; n++) {//找前驱
temp = temp->next;
}
temp->next = temp->next->next;
return OK;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值