单链表的建立
【摘要】
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
...
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node LNode, *LinkList;
void CreateList(LinkList head);
void PrintList(LinkList head);
void ListDelete(LinkList head, int i, int *e);
void ListInsert(LinkList head, int i, int e);
int main()
{
LinkList head;
int i, e, select;
head = (LinkList) malloc (sizeof(LNode));
head -> next = NULL;
while(1)
{
printf("1: Create a list.\n");
printf("2: Insert some elements in the list.\n");
printf("3: Delete some elements.\n");
printf("4: Print the list.\n");
printf("0: Quit!\n");
scanf("%d", &select);
switch(select)
{
case 1: CreateList(head); break;
case 2: printf("Input position and name of the element.\n");
scanf("%d%d", &i, &e);
ListInsert(head, i, e);
break;
case 3: printf("Input the position of the element.\n");
scanf("%d", &i);
ListDelete(head, i, &e);
case 4: PrintList(head);
printf("\n");
break;
case 0:
exit(0);
}
}
return 0;
}
//void CreateList()//头插法
//{
// LinkList p;
// int num;
//
// while(scanf("%d", &num), num!=-1)
// {
// p = (LinkList)malloc(sizeof(LNode));
// p ->data = num;
// p ->next = head ->next;
// head ->next = p;
// }
//}
void CreateList(LinkList head)//尾插法
{
LinkList p, rear = head;
int num;
while(scanf("%d", &num), num!=-1)
{
p = (LinkList)malloc(sizeof(LNode));
p ->data = num;
rear ->next = p;
rear = p;
}
rear ->next = NULL;
}
void PrintList(LinkList head)
{
LinkList p = head ->next;
while(p)
{
printf("%d\n", p ->data);
p = p ->next;
}
}
void ListInsert(LinkList head, int i, int e)//变相的头插法
{
LinkList p, pre = head;
int j = 0;
while(pre && j<i+1)
{
pre = pre ->next;
j++;
}
if(!pre || i<1)
{
printf("Input i error\n");
return;
}
p = (LinkList)malloc(sizeof(LNode));
p ->data = e;
p ->next = pre->next;
pre ->next = p;
}
void ListDelete(LinkList head, int i, int *e)
{
LinkList p, pre;
int j=0;
while(pre && j<i-1)
{
pre = pre ->next;
j++;
}
if(!pre || i<1)
{
printf("Input i error\n");
return;
}
p = pre ->next;
pre ->next = p ->next;
*e = p->data;
free(p);
}
- 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
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
文章来源: recclay.blog.csdn.net,作者:ReCclay,版权归原作者所有,如需转载,请联系作者。
原文链接:recclay.blog.csdn.net/article/details/60953553
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)