C语言 链表基本函数
#include <stdio.h>
#include <malloc.h>
typedef struct my_node mynode;
struct my_node{
struct my_node *next;
int data;
};
void push_node(mynode **head,int data)
{
mynode *temp = *head;
if(*head == NULL)
{
*head = (mynode *)malloc(sizeof(mynode));
(*head) ->data = data;
(*head) ->next = NULL;
}
else
{
while(temp ->next != NULL)
temp = temp ->next;
temp ->next = (mynode *)malloc(sizeof(mynode));
temp ->next ->data = data;
temp ->next ->next = NULL;
}
}
void pop_node(mynode **head)
{
mynode *temp = *head, *prev;
while(temp ->next != NULL)
{
prev = temp;
temp = temp ->next;
}
free(prev ->next);
prev ->next = NULL;
}
void del_node(mynode **head,int index)
{
mynode *temp = *head, *tmp;
while(index -- != 1)
temp = temp ->next;
tmp = temp ->next;
temp ->next = temp ->next->next;
free(tmp);
}
void ins_node(mynode **head,int index,int data)
{
mynode *temp = *head, *tmp;
while(index -- != 1)
temp = temp ->next;
tmp = temp ->next;
temp ->next = NULL;
temp ->next = (mynode *)malloc(sizeof(mynode));
temp ->next ->next = tmp;
temp ->next ->data = data;
}
int find_data(mynode *head,int data)
{
mynode *temp = head;
int i =0;
while(temp ->next != NULL)
{
if(data == temp ->data)
return i;
temp = temp ->next;
i++;
}
return 0;
}
void back_ward(mynode **head)
{
mynode *now = *head, *temp, *prev;
while(now ->next != NULL)
{
temp = now->next;
if(now == *head)
now->next = NULL;
else
now ->next = prev;
prev = now;
now = temp;
}
now ->next = prev;
*head = now;
}
void sort_node(mynode **head)
{
mynode *curNode = (*head) ->next,*prevNode,*nextNode,*tempNode;
(*head) ->next =NULL;//有序节点尾为null
while(curNode != NULL)
{
tempNode = curNode->next;
if(curNode ->data > (*head) ->data)//大于头结点,新头结点
{
curNode->next = *head;
(*head) = curNode;
}
else
{
prevNode = *head;
nextNode = (*head) ->next;
while(nextNode != NULL && curNode ->data < nextNode->data) //定位需要插入的位置
{
prevNode = nextNode;
nextNode = nextNode ->next;
}
curNode ->next = prevNode ->next;
prevNode ->next =curNode;
}
curNode = tempNode;
}
}
int main()
{
mynode *head = NULL;
// head = (mynode *)malloc(sizeof(mynode));
// head ->data =10;
// head ->next = NULL;
push_node(&head,10);
push_node(&head,100);
push_node(&head,101);
push_node(&head,102);
push_node(&head,103);
pop_node(&head);
push_node(&head,103);
del_node(&head,2);
ins_node(&head,2,101);
find_data(head,102);
back_ward(&head);
back_ward(&head);
sort_node(&head);
return 0;
}
C语言 链表基本函数的更多相关文章
- C语言 链表
原文:C语言 链表 最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的. 自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将要实现19个功能. ...
- C语言链表操作模板(添加,删除,遍历,排序)
C语言链表操作模板,摘自郝斌的C语言视频教程,简单的修改成了纯C格式.当年照着视频学习的时候记录下来的,在使用的时候直接拿来修改修改修改能节约不少时间的. /********************* ...
- ZT C语言链表操作(新增单向链表的逆序建立)
这个不好懂,不如看 转贴:C语言链表基本操作http://www.cnblogs.com/jeanschen/p/3542668.html ZT 链表逆序http://www.cnblogs.com/ ...
- C语言链表结构体(学习笔记)
#include <stdio.h> #define LENTEST 100 // 采取逐步删除的方法求的素数 //先假设1-100都是素数,然后剔除2的倍数, //3的倍数,直到剔除所有 ...
- C语言链表实例--玩转链表
下图为最一简单链表的示意图: 第 0 个结点称为头结点,它存放有第一个结点的首地址,它没有数据,只是一个指针变量.以下的每个结点都分为两个域,一个是数据域,存放各种实际的数据,如学号 num,姓名 n ...
- c语言-链表VS数组
数组和链表的区别 数组是将元素在内存中连续存放,由于每个元素占用内存相同,可以通过下标迅速访问数组中任何元素.但是如果要在数组中增加一个元素,需要移动大量元素,在内存中空出一个元素的空间,然后将要 ...
- 再次复习数据结构:c语言链表的简单操作
最近呢,又要面临多次的数据结构与算法方面的试题了,而我呢,大概也重新温习c语言的基本要点快一个月了,主要是针对指针这货的角度在研究c语言,感觉又学到了不少. 现在c指针感觉知道点了,也就匆忙开展数据结 ...
- [数据结构]C语言链表实现
我学数据结构的时候也是感觉很困难,当我学完后我发现了之所以困难时因为我没有系统的进行学习,而且很多教授都只是注重数据结构思想,而忽略了代码方面,为此我写了这些博文给那些试图自学数据结构的朋友,希望你们 ...
- [C语言]链表实现贪吃蛇及部分模块优化
在继上篇[C语言]贪吃蛇_结构数组实现大半年后,链表实现的版本也终于出炉了.两篇隔了这么久除了是懒癌晚期的原因外,对整个游戏流程的改进,模块的精简也花了一些时间(都是借口). 优化模块的前沿链接: · ...
随机推荐
- Ingress.yaml
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: test-ingress namespace: default annotat ...
- apo 简单参考
参考: https://www.cnblogs.com/Geyoung/p/6927905.html @Aspect @Component public class TimeAspect { //通过 ...
- JAVA 连接 Redis 并进行操作
1, 这里以maven项目为例 <!-- Redis NoSQL 操作依赖 --> <dependency> <groupId>redis.clients</ ...
- HTML5 Canvas ( 线段端点的样式 ) lineCap
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- HTML5 Canvas ( 填充图形的绘制 ) closePath, fillStyle, fill
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- leetcode179
class Solution { public: string largestNumber(vector<int>& nums) { int n=nums.size(); vect ...
- mysql使用一条sql删除多条数据
使用in delete from course where chour in(55,56,57); course:表名 chour:字段 55,56,57数据
- 【转】oracle 体系结构
前几天面试的时候面试官才问过我Oracle的体系结构,让我在一张白纸上画出来.回头想想当时答得还不错,大部分内容都描述出来了,呵呵,刚才在网上看到一篇讲解ORACLE体系结构的文章,觉得不错,转过来存 ...
- sql server判断是否为null
sql server 替换null:isnull(arg,value) 如:select isnull(price,0.0) from orders ,如果price为null的话,用0.0替换 与n ...
- How to Pronounce INTERNATIONAL
How to Pronounce INTERNATIONAL Share Tweet Share Tagged With: Dropped T How do you pronounce this lo ...