自己学习数据结构的编程练习;
平台:MacOS Catalina
软件:Xcode
时间:1h
//
// main.c
// test
//
// Created by 罗高辉 on 2020/4/16.
// Copyright © 2020 罗高辉. All rights reserved.
//
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include "stdlib.h"
//#include "io.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef struct Node
{
int data;
struct Node * next;
}Node, *pNode;
pNode create_Node(void);
pNode create_List(void);
int create_Node_2(pNode *pHead);
int create_List_2(pNode *pHead);
void show_List(pNode pHead);
int insert_List(pNode pHead, int pos, int val);
int delete_List(pNode pHead, int pos, int *val);
int is_empty(pNode pHead);
int length_List(pNode pHead);
int sort_List(pNode pHead);
int get_List(pNode pHead, int pos, int *val);
int main()
{
pNode pList;
int val;
//pList = create_List();
create_List_2(&pList);
show_List(pList);
insert_List(pList, 5, 100);
show_List(pList);
get_List(pList, 5, &val);
printf("get_List()取的节点数值为:%d\n", val);
delete_List(pList, 5, &val);
show_List(pList);
sort_List(pList);
show_List(pList);
return 0;
}
pNode create_Node(void)
{
pNode p;
p=(pNode)malloc(sizeof(Node));
if(NULL == p)
{
printf("创建节点失败!\n");
exit(-1);
}
p->next=NULL;
return p;
}
pNode create_List(void)
{
int i,val,len=0;
pNode pHead,p,pNew;
create_Node_2(&pHead);
//pHead=create_Node();
p = pHead;
printf("请输入链表的长度:\n");
scanf("%d",&len);
for (i=0; i<len; i++) {
pNew = create_Node();
printf("请输入第%d个元素的值: ",i);
scanf("%d",&val);
pNew->data = val;
//pNew->next = NULL;
p->next = pNew;
p = pNew; //p = p->next;
}
return pHead;
}
int create_Node_2(pNode *pHead)
{
*pHead=(pNode)malloc(sizeof(Node));
if(NULL == *pHead)
{
printf("创建节点失败!\n");
return 0;
}
(*pHead)->next=NULL;
return 1;
}
int create_List_2(pNode *pHead)
{
int i,val,len=0;
pNode p,pNew;
//这里是指针的指针,穿进去的就是指针pHead的地址,而pHead中存放的是结构体的地址
create_Node_2(pHead);
p = *pHead; //p指向尾节点
printf("请输入链表的长度:\n");
scanf("%d",&len);
for (i=0; i<len; i++) {
pNew = create_Node();
printf("请输入第%d个元素的值: ",i);
scanf("%d",&val);
pNew->data = val;
//pNew->next = NULL;
p->next = pNew;
p = pNew; //p = p->next;
}
return OK;
}
void show_List(pNode pHead)
{
pNode p;
if( is_empty(pHead) )
{
printf("链表为空!");
exit(-1);
}
p = pHead->next;
while(p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int is_empty(pNode pHead)
{
if(NULL == pHead->next)
return TRUE;
else
return FALSE;
}
int insert_List(pNode pHead, int pos, int val)
{
int i=0;
pNode p = pHead;
while(NULL != p->next && i < pos-1)
{
++i;
p=p->next;
}
if(NULL == p || i>pos-1)
{
printf("插入失败!\n");
return ERROR;
}
pNode pNew = create_Node();
pNew->data = val;
pNew->next=p->next;
p->next=pNew;
return OK;
}
int length_List(pNode pHead)
{
int len=0;
pNode p = pHead->next;
while(p)
{
++len;
p = p->next;
}
return len;
}
int delete_List(pNode pHead, int pos, int *val)
{
int i=0;
pNode p = pHead;
while(NULL != p->next && i < pos-1)
{
++i;
p=p->next;
}
if(NULL == p || i>pos-1)
{
printf("删除失败!\n");
return ERROR;
}
pNode q = p->next;
p->next = q->next;
free(q);
return OK;
}
int sort_List(pNode pHead)
{
int i, j, t, len;
pNode p, q;
len = length_List(pHead);
for(i=0, p=pHead->next; i < len-1; i++,p=p->next)
for(j=i+1, q=p->next; j<len; j++,q=q->next)
{
if(p->data > q->data)
{
t = p->data;
p->data = q->data;
q->data = t;
}
}
return OK;
}
int get_List(pNode pHead, int pos, int *val)
{
int i = 0;
pNode p = pHead;
while (NULL != p->next && i<pos-1) {
++i;
p = p->next;
}
if(NULL == p || i > pos-1)
{
printf("查找失败!\n");
return ERROR;
}
*val = p->next->data;
return OK;
}
测试结果:
请输入链表的长度:
7
请输入第0个元素的值: 2
请输入第1个元素的值: 8
请输入第2个元素的值: 1
请输入第3个元素的值: 5
请输入第4个元素的值: 3
请输入第5个元素的值: 9
请输入第6个元素的值: 3
2 8 1 5 3 9 3
2 8 1 5 100 3 9 3
get_List()取的节点数值为:100
2 8 1 5 3 9 3
1 2 3 3 5 8 9
Program ended with exit code: 0
备注: 这个好看的小块のMarkdown语法是<kbd> </kbd>