#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
typedef struct Node
{
//定义连接链表的节点:
Node* next;
}node;
typedef struct Common_list
{
//定义一个空间域
node head;
int length;
}common_list;
//定义一个用户数据域:
typedef struct User
{
//用于链接数据的节点:
node head;
//用户数据:
char song_name[16];
int song_time;
char star[32];
}user;
//1. 建立空链表:
common_list* creat_list()
{
common_list*temp_creat;
temp_creat = (common_list*)malloc(sizeof(common_list));
if (temp_creat == nullptr)
{
printf("空间开辟错误!!!\n");
return nullptr;
}
printf("已成功创建空列表!!1\n");
temp_creat->head.next = (node*)&temp_creat;//双向链表
temp_creat->length = 0;
printf("空列表初始化成功!!1\n");
return temp_creat;
}
//2. 开始在old中的pos位置处插入元素input:
void insert_list(common_list**old,node*input, int pos)
{
common_list*temp_insert = *old;
if (temp_insert == nullptr)
{
printf("传入空间错误!!!\n\n");
return ;
}
else if (pos<0)
{
printf("位置号不能小于0");
return;
}
else if (pos > temp_insert->length)
{
pos = temp_insert->length;
}
//定义一个辅助指针:
node*current = &temp_insert->head;
for (int i = 0; i < pos; i++)
{
//开始移动到插入的位置:
current = current->next;
}
//进行插入操作:
input->next = current->next;
current->next = input;
temp_insert->length++;
printf("歌曲插入成功!!!\n\n");
}
//查找pos处的元素:
node* find_list(common_list*old, int pos)
{
common_list*temp_find = old;
if (temp_find == nullptr)
{
printf("传入空间错误!!!\n\n");
return nullptr;
}
else if (pos < 0 || pos>=temp_find->length)
{
printf("该位置处无歌曲!!!\n\n");
return nullptr;
}
//定义一个辅助指针便于寻找:
node* current = temp_find->head.next;
for (int i = 0; i < pos; i++)
{
current = current->next;
}
printf("找到歌曲啦!!!\n\n");
return current;
}
//定义删除函数:
void delete_list(common_list**old, int pos)
{
common_list*temp_del = *old;
if (temp_del == nullptr)
{
printf("传入空间错误!!!\n\n");
return ;
}
else if (pos < 0 || pos >= temp_del->length)
{
printf("该位置处无歌曲!!!\n\n");
return ;
}
//定义一个辅助指针定位当前位置:
node* current = &temp_del->head;
for (int i = 0; i < pos; i++)
{
current = current->next;
}
//定义一个辅助指针用于缓存要删除的节点:
node*del = current->next;
current->next = del->next;
temp_del->length--;
printf("歌曲删除成功!!!\n\n");
}
void clear_list(common_list**old)
{
common_list* temp_cls = *old;
if (temp_cls == nullptr)
{
printf("传入空间错误!!!\n\n");
return;
}
free(temp_cls);
temp_cls = nullptr;
printf("歌曲已经清空!!!\n\n");
}
//自动遍历链表函数
void show_list(common_list*old)
{
printf("开始自动搜索歌曲!!!\n\n");
common_list* temp_s = old;
if (temp_s == nullptr)
{
printf("传入空间错误!!!\n\n");
return;
}
node* current = &temp_s->head;
while (current !=nullptr)
{
user* get = (user*)current;
printf("你的歌曲如下:\n歌手:%s,歌曲:%s,时间长:%d\n\n", get->star, get->song_name, get->song_time);
current = current->next;
system("pause");
}
}
int main()
{
common_list*old = creat_list();
user* get;
user u2;
strcpy_s(u2.star, "周杰伦");
strcpy_s(u2.song_name,"告白气球");
u2.song_time = 111;
insert_list(&old,(node*)&u2,0);
user u3;
strcpy_s(u3.star, "周杰伦");
strcpy_s(u3.song_name, "mojito");
u3.song_time = 222;
insert_list(&old, (node*)&u3, 0);
user u1;
strcpy_s(u1.star, "夏婉安");
strcpy_s(u1.song_name, "新年好");
u1.song_time = 333;
insert_list(&old, (node*)&u1, 0);
user u4;
strcpy_s(u4.star, "格子兮");
strcpy_s(u4.song_name, "秋殇别恋");
u4.song_time = 444;
insert_list(&old, (node*)&u4, 0);
for (int i = 0; i < old->length; i++)
{
get = (user*)find_list(old, i);
printf("你的歌曲如下:\n歌手:%s,歌曲:%s,时间长:%d\n\n",get->star,get->song_name,get->song_time);
}
//delete_list(&old,1);
for (int i = 0; i < old->length; i++)
{
get = (user*)find_list(old, i);
printf("你的歌曲如下:\n歌手:%s,歌曲:%s,时间长:%d\n\n", get->star, get->song_name, get->song_time);
}
show_list(old);
return 0;
}
循环链表(c语言版)
于 2021-03-13 22:00:13 首次发布