代码小白,欢迎大神指导!
定义结构体:
typedef struct linknode
{
int data;
struct linknode* next;
}LinkList;
函数:
typedef struct linknode
{
int data;
struct linknode* next;
}LinkList;
//初始化链表
LinkList* InitList();
//删除链表
void DeleteList(LinkList* head, int i);
//按值查找
int Locate(LinkList* head, int x);
//头插
void CreateListH(LinkList* head, int n);
//遍历链表
void DispList(LinkList* head);
//逆置单链表
struct linknode* InverseList(LinkList* head);
//统计指定元素个数
struct linknode* CountList(LinkList* head, int x);
//链表转化为递增
struct linknode* IncreaseList(LinkList* head);
初始化链表:
//初始化链表
LinkList* InitList()
{
LinkList* head;
head = (LinkList*)malloc(sizeof(LinkList));
head->next = NULL;
return head;
}
按值查找:
//按值查找
int Locate(LinkList* head, int x)
{
int i = 1;
LinkList* p = head->next;
while (p->next != NULL)
{
if (p->data == x)
{
break;
}
i++;
p = p->next;
if (p->next == NULL)
break;
}
if (p->next!= NULL)
return i;
else
return 0;
}
删除链表:
void DeleteList(LinkList* head,int i)
{
int j=0,x;
LinkList* s, * p = head;
while (p->next != NULL && j < i - 1)
{
p=p->next;
j++;
}
if (p->next != NULL && j == i - 1)
{
s = p->next;
p->next = s->next;
free(s);
}
}
头插:
void CreateListH(LinkList* head, int n)
{
LinkList* s;
int i;
printf("请输入%d个整数:", n);
if (head->next == NULL)
{
s = (LinkList*)malloc(sizeof(LinkList));
s->next = NULL;
head->next = s;
}
for (i = 0; i < n; i++)
{
s = (LinkList*)malloc(sizeof(LinkList));
scanf("%d", &s->data);
s->next = head->next;
head->next = s;
}
printf("插入成功\n");
}
遍历链表:
void DispList(LinkList* head)
{
LinkList* p = head->next;
while (p->next != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
逆置单链表:
struct linknode* InverseList(LinkList* head)
{
if (head == NULL||head->next==NULL)
return head;
LinkList* beg = NULL, * mid = head, * end = head->next;
while (1)
{
mid->next = beg;
if (end == NULL)
break;
beg = mid;
mid = end;
end = end->next;
}
head = mid;
return head;
}
统计指定元素个数:
struct linknode* CountList(LinkList* head, int x)
{
int count = 0;
if (head == NULL)
return 0;
LinkList* p = head->next;
while (p->next!=NULL)
{
if (p->data == x)
count++;
p = p->next;
}
return count;
}
链表转化为递增:
struct linknode* IncreaseList(LinkList* head)
{
int i = 0;
int count=0;
int arr[20];
LinkList* p = head->next;
while (p->next!=NULL)
{
arr[i]=p->data;
p = p->next;
i++; count++;
}
for(int j=0;j<count;j++)
for (int k = 0; k < count; k++)
{
if (arr[j] < arr[k])
{
int temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
}
}
p = head->next;
i = 0;
while (p->next != NULL)
{
p->data = arr[i];
p = p->next;
i++;
}
return head;
}
主函数:
该主函数主要用来验证各个函数是否能运行
int main()
{
int x;//要删除的数据域为x的节点
LinkList* head = InitList();//初始化链表
int n;
printf("您要输入几个数:");
scanf("%d", &n);
CreateListH(head, n);//头插n个数
printf("头插后遍历:");
DispList(head);//删除前遍历
printf("请输入您要删除的数:");
scanf("%d", &x);
while (1)//题目:删除顺序表中值为x的所有结点。
{
int i = Locate(head, x);//查找待删除节点位置
if (i == 0)
break;
else
{
DeleteList(head, i);//删除
}
}
printf("删除后遍历:");
DispList(head);//删除之后遍历
head = InverseList(head);//逆置链表
printf("逆置后遍历:");
DispList(head);//逆置后遍历
int m;
printf("请输入您要统计数量的数:");
scanf("%d", &m);
printf("该链表共有%d个%d\n", CountList(head, m),m);
head = IncreaseList(head);
printf("递增排序后遍历:");
DispList(head);//递增排序后遍历
return 0;
}