非科班出身学生学习计算机数据结构课程实操练习,用以巩固学习知识,项目函数有实操验证且结果符合预期。
代码取用自由,放出来也是自己方便复习。
如有算法上的漏洞或建议,可在评论区提出。
目录
单链表数据节点定义
typedef struct LNode
{
int data;
struct LNode* next;
} LNode, * LinkList;
初始化单链表
void InitLinkList(LinkList& L) {
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
}
表头插入法插入元素
LinkList InsertHead(LinkList& L) {
LNode* s;
int x;
InitLinkList(L);
printf("please input a number (If you input the number 999 which means the end of inputing)\n");
scanf("%d", &x);
while (x != 999)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
scanf("%d",&x);/* code */
}
return L;
}
表尾插入法插入元素
LinkList InsertTail(LinkList &L){
LNode* s,*t;
int x;
InitLinkList(L);
printf("please input a number (If you input the number 999 which means the end of inputing)\n");
scanf("%d", &x);
t = L;
while (x != 999)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = t->next;
t->next = s;
t = s;
scanf("%d", &x);/* code */
}
return L;
}
计算链表长度函数
int length(LinkList L) {
LNode* p = L->next;
int len = 0;
while (p)
{
len++;
p = p->next;/* code */
}
return len;
}
单链表便利函数
void PrintfList(LNode* L) {
if (L == NULL)
{
printf("error!\n");/* code */
}
while (L->next!=NULL)
{
printf("%d\n", L->next->data);
L = L->next;/* code */
}
}
单链表按位查找
LNode* FinebyPos(LinkList L, int i) {
LNode* p = L->next;
int j = 1;
if (i == 0)
{
return L;
}
if (i < 1)
{
printf("error!\n");
}
while (p && j < i)
{
p = p->next;
j++;
}
return p;
}
单链表按值查找
LNode* FinebyValue(LinkList L, int i) {
LNode* p = L->next;
int j = 1;
while (p->data!=i)
{
p = p->next;
if (p->next==NULL)
{
printf("it is not in this list!\n");
return NULL;
}
}
printf("it is in this list!\n");
return p;
}
单链表节点按位插入
void Insert(LinkList& L) {
LNode* p,*i;
p = L->next;
int x;
int j = 1;
int w;
printf("please input the number you want to insert:\n");
scanf("%d",&x);
printf("please select the position of this element :\n");
scanf("%d",&w);
if (w<=0)
{
printf("error!\n");
}
i = (LNode*)malloc(sizeof(LNode));
i->data = x;
while (p&&j<w-1)
{
p = p->next;
j++;
}
i->next = p->next;
p->next = i;
}
单链表节点按位删除
void DeleteLNode(LinkList& L){
LNode* p, * q;
p = L->next;
int j=1;
int x;
printf("please input the position of this element you want to erase:\n");
scanf("%d",&x);
if (x<=0|| x > length(L))
{
printf("error!\n");
}
if (x==1)
{
L->next = p->next;
free(p);
}
if (x>1)
{
while (j<x-1) {
p = p->next;
j++;
}
q = p->next;
p->next = q->next;
free(q);
}
}
附测试时主函数
int main()
{
//int a;
LinkList L;
printf("\n\n\nInsert number from head of this linklist\n");
/* InsertHead(L);
printf("\nthe length of this list is %d\n", length(L));
PrintfList(L);
printf("\n\n\n Fine by Pos Experience:\n");
printf("please input the position of number that you want:\n");
scanf("%d", &a);
int NumbyPos = (FinebyPos(L, a))->data;
printf("%d", NumbyPos);
printf("\n\n\nFine the number by value\n");
printf("please input the number that you want to fine :\n");
LNode* b;
int m;
scanf("%d",&m);
b = FinebyValue(L, m);
if (b)
{
printf("that number you want to fine is %d!\n", b->data);
}
*/
InsertTail(L);
PrintfList(L);
Insert(L);
printf("after inserting a element:\n");
PrintfList(L);
printf("delete an element :\n");
DeleteLNode(L);
printf("after deleting :\n");
PrintfList(L);
}