-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_LinkList.h
80 lines (80 loc) · 1.26 KB
/
user_LinkList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once
#include<stdlib.h>
#include<stdio.h>
#define ElemType int //单链表基础类型
typedef struct Node{
ElemType data;
struct Node *next;
}Node,*LinkList;
int InitList_L(LinkList &L)//初始化一个单链表
{
L = (LinkList) malloc(sizeof(Node));
if (!L)
return -1;
L->next = NULL;
return 1;
}
int ListLength(LinkList &L)//返回单链表长度
{
LinkList p = L;
int n = 0;
while (p->next != NULL)
{
n++;
p = p->next;
}
return n;
}
int ListInsert_L(LinkList &L,int i,ElemType e)//在单链表L的第i个元素前插入e
{
LinkList p=L;
int j=0;
if (i <= ListLength(L))
{
while (j<i - 1)
{
p = p->next;
j++;
}
}
else
{
while (p->next != NULL)
p = p->next;
}
LinkList s;
s=(LinkList)malloc(sizeof(Node));
s->data=e;
s->next=p->next;
p->next=s;
return 1;
}
int ListDelete_L(LinkList &L, int i)//删除单链表中位置i处的元素
{
if (L->next == NULL || i>ListLength(L))
return 0;
LinkList p = L;
int j = 0;
while (j<i - 1)
{
p = p->next;
j++;
}
LinkList q = p->next;
p->next = q->next;
free(q);
return 1;
}
int Printf_L(LinkList &L)//顺次打印整个单链表
{
if(L->next==NULL)
return 0;
LinkList p=L->next;
while(p!=NULL)
{
printf("%d\t",p->data);
p=p->next;
}
printf("\n");
return 1;
}