c语言链表设计:
概述:
链表是一种常见的采用动态存储分配方式的数据结构。数据元素随机存储,并通过指针表示数据之间逻辑关系的存储结构就是链表。
提示:以下是本篇文章正文内容,下面案例可供参考
一、链表的创建
1 创建链表:
MyLinkedList* myLinkedListCreate() {
MyLinkedList * obj =(MyLinkedList*)malloc(sizeof(MyLinkedList));
obj->val = 0;
obj->next = NULL;
return obj;
}
2:头插法:
#include<stdio.h>
#include<stdlib.h>
typedef struct link{
int x; // 数据域
struct link * next; // 指针域
}LINK;
int main()
{
LINK* head = (LINK*)malloc(sizeof(LINK)); // 头节点
LINK *current;
head->next = NULL;
int number;
while(scanf("%d",&number) ==1 && number != -1)
{
current = (LINK*) malloc(sizeof(LINK));
current->x = number;
current->next = head->next ; // 新结点为原来结点的地址
head->next = current; // 头节点指向新结点
}
LINK *p;
for(p = head->next ; p ; p = p->next ) // 遍历输出
{
printf("%d\t",p->x );
}
return 0;
}
3: 尾插法:
二:尾插法
#include<stdio.h>
#include<stdlib.h>
typedef struct link{
int x; // 数据域
struct link * next; // 指针域
}LINK;
int main()
{
LINK* head = NULL; // 头节点
LINK *current ,*prev;
int number;
while(scanf("%d",&number) ==1 && number != -1)
{
current = (LINK*) malloc(sizeof(LINK));
current->x = number;
if(head == NULL) // 判断是否是第一个结构
head = current;
else
prev->next = current;
current->next = NULL; // 当前结点的指针指向NULL 表明是最后一个结构
prev = current;
}
LINK *p;
for(p = head ; p ; p = p->next ) // 遍历输出
{
printf("%d\t",p->x );
}
return 0;
}
二、增添元素
1.在头部添加
代码如下(示例):
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
MyLinkedList *p =(MyLinkedList*) malloc (sizeof(MyLinkedList));
p->val = val;
p->next = obj->next;
obj->next = p;
}
2.在尾部添加
代码如下(示例):
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
MyLinkedList *p = (MyLinkedList*) malloc (sizeof(MyLinkedList));
p->next = NULL;
p->val = val;
MyLinkedList *q = obj;
while(q->next)
{
q = q->next;
}
q->next = p;
}
3在任意位置增添:
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
MyLinkedList *p = (MyLinkedList*) malloc(sizeof(MyLinkedList));
MyLinkedList *q = obj->next;
p->next = NULL;
p->val = val;
int i = 1;
if(index <= 0)
{
myLinkedListAddAtHead(obj, val); //在头部增添
}
for( ; q ; q = q->next, i++)
{
if (i == index)
{
if(q->next)
{
p->next = q->next;
q->next = p;
}
else
q->next = p;
break;
}
}
}
三 :删除
1在任意位置删除:
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
MyLinkedList *p = obj->next;
MyLinkedList *q = obj;
int i = 0;
if (index < 0)
return;
for( ; p ; i++ )
{
if(i == index)
{
if(p->next)
{
q->next = p->next;
free(p);
p = NULL;
break;
}
else
{
free(p);
p = NULL;
q->next = NULL;
}
break;
}
q = p ;
p = p->next;
}
}
四 查询
按序号查询
int myLinkedListGet(MyLinkedList* obj, int index) {
MyLinkedList *p = obj->next ;
int i = 0;
for( ; p ; i++ )
{
if(i == index)
return p->val;
else
p = p->next ;
}
return -1;
}
五 判空
int isEmpty(){
Node * q = head->next;
int flag = 1;
if(q == NULL)
{
flag = 0;
}
return flag;
}
六 排序
归并排序
Node* sortMerge(Node *head){
if(head == NULL || head->next == NULL)
return head;
//分割
//fast 设置为head->next 方便分割后前半部分后面设置NULL
Node *fast = head->next ,*slow = head;
// 找中间结点
while(fast && fast->next )
{
fast = fast->next->next ;
slow = slow->next ;
}
//后半部分的头节点
Node *nextHead = slow->next ;
slow->next = NULL;
Node* m1 = sortMerge(head);
Node* m2 = sortMerge(nextHead);
//排序
//虚拟头结点
Node* dummy = (Node*)malloc(sizeof(Node));
Node* prev = dummy;
while(m1 && m2)
{
if(m1->stu.id <= m2->stu.id )
{
prev->next = m1;
m1 = m1->next;
}
else
{
prev->next = m2;
m2 = m2->next ;
}
prev = prev->next;
}
//接上另半段链表
prev->next = m1 == NULL ? m2 : m1;
return dummy->next;
}
七 释放空间
void myLinkedListFree(MyLinkedList* obj) {
while(obj)
{
MyLinkedList *q = (MyLinkedList*) malloc(sizeof(MyLinkedList));
q = obj;
obj = obj->next;
free(q);
}
}
总结
多敲代码 多理解!
可以去leetCode 做设计链表问题