大作业水平...
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node* next;
int c;
};
struct node* init_node(int input)
{
struct node* p;
p = malloc(sizeof(struct node));
p->c = input;
return p;
}
void insert_node(struct node *head, int new)
{
struct node *temp = (struct node*)(malloc(sizeof(struct node)));
// struct node *temp;
while(head->next != NULL)
head = head->next;
head->next = temp;
temp->c = new;
temp->next = NULL;
}
void display_list(struct node* head){
if(head == NULL)
{
printf("error.\n");
return;
}
struct node *p = head;
while(p->next != NULL)
{
printf("%d ",p->c);
p = p->next;
}
printf("%d ", p->c);
printf("NULL\n");
}
void destroy_list(struct node *head)
{
if (NULL != head) {
destroy_list(head->next);
// free(head->c);
free(head);
printf("destroy a list...\n");
}
}
struct node* copy_list(struct node* head)
{
struct node* p = (struct node*)(malloc(sizeof(struct node)));
p->next = NULL;
if(head == NULL)
{
printf("head is NULL.\n");
return;
}
p->c = head->c;
head = head->next;
while(head != NULL)
{
printf("copying... head->c: %d\n", head->c);
insert_node(p, head->c);
head = head->next;
}
// insert_node(p, head->c);
return p;
}
struct node* reverse_list(struct node* head)
{
struct node* p;
struct node* t;
p = (struct node*)(malloc(sizeof(struct node)));
p->c = head->c;
p->next = NULL;
while(head->next != NULL)
{
head = head->next;
t = p;
p = (struct node*)(malloc(sizeof(struct node)));
p->next = t;
p->c = head->c;
}
return p;
}
int main(int argc, char** argv)
{
struct node *head = init_node(1);
long long i;
insert_node(head, 2);
insert_node(head, 3);
insert_node(head, 4);
insert_node(head, 5);
insert_node(head, 6);
insert_node(head, 7);
display_list(head);
struct node* copy = copy_list(head);
printf("copy done.\n");
struct node* reve = reverse_list(head);
display_list(reve);
for (i = 0; i < 0x1000000; i++) reve = reverse_list(head);
display_list(reve);
display_list(copy);
destroy_list(head);
destroy_list(copy);
return 0;
}
分享到:
相关推荐
选择合适的存储方式实现线性表。其中,必须实现的线性表基本操作为:InitList、 ClearList、ListEmpty、ListLength、GetElem、PriorElem、ListInsert、ListDelete这8个基本操作,其余的可以选作。
C语言实现链表通讯录 主要就是链表的应用实例 入门用的 仔细阅读能有不少收获哦 加油~~
C语言实现链表操作,包括增加节点,删除节点,显示链表
C语言实现链表基本操作
C语言实现链表HuffmanTree
用c语言实现链表的合并,是数据结构中重要的基础实验,调试后可以运行
用c语言实现链表排序,利用选择排序的思想,可以供大家学习。
C语言实现 链表操作,链表 建立 输出 删除 插入,整个程序代码,看完后会让你对链表操作有更深的体悟
c语言实现链表,链表是数据结构第一课,希望对你有帮助 实现链表
VC6.0下 用C语言实现单向链表的创建、插入,删除节点,和2个链表合并等操作
链表综合系统链表综合系统链表综合系统链表综合系统
C语言实现多种链表快速排序
c语言实现链表的基本操作,链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态...
这篇文档介绍了C语言中链表的实现,一点个人心得,希望对你能有所帮助。。。
这是利用c语言中的链表来解决的问题,有利于你对C语言链表的更好的了解
window平台,用clion实现c语言链表
自己写的,能够使用,对c语言的指针用的淋漓精致,有需要的小伙伴可以下载看下
是用C语言来实现对链表的各种操作,数据结构课程一般会用到。保证程序正确性