小记
笔记
想着回顾一下C的基础知识,然后看到C的结构体数组和链表的部分,动手敲了一下,下面附上代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct s_node
{
char name[256];
int age;
struct s_node *next;
};
struct s_node * create_number(int n){
/*
note:创建有n个节点的链表
*/
int i;
struct s_node *head,*new,*p;
if(n>=1){
/*
note:生成头结点
*/
new=malloc(sizeof(struct s_node));
new->age=1;
new->next=NULL;
head=new;
p=new;
}
for(i=2;i<n;i++){
new=malloc(sizeof(struct s_node));
new->age=i;
new->next=NULL;
p->next=new;
p=new;
}
if(n>=1)
return (head);
else
return NULL;
}
void out_list(struct s_node *head){
struct s_node *p;
if(head!=NULL){
p=head;
while (p!=NULL)
{
printf("%d",p->age);
p=p->next;
printf("\n");
}
}
}
int main(){
//这里敲一下结点的赋值
struct s_node *p ,*q;
p=malloc(sizeof(struct s_node));
//p->name="zhangsan"; note:尽量规避这种写法,这种写法是错误的。
strcpy(p->name,"zhangsan");
p->age=18;
printf("%s\n",p->name);
printf("%d\n",p->age);
q=create_number(20);
out_list(q);
}
打印如下:
关于C的rand()生成随机数
引入:
#include <stdlib.h>
#include <time.h>
写法:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
srand((unsigned int)time(0));//初始化种子为随机值
int i = 0;
for(;i < 5;++i){
int num = rand() % 50 + 1;//产生一个1-50之间的数
printf("%d ",num);
}
printf("\n");
return 0;
}
若不设置srand或设置srand(1)的话只会产生相同的随机数
C头插法尾插法
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct linknode
{
int value;
struct linknode *next;
}*Node;
//初始化,创建头结点
struct linknode *linkinit(){
struct linknode *head;
head = malloc(sizeof(struct linknode));
head->next = NULL;
return head;
}
//头插法
void add(struct linknode *head,int value){
struct linknode *node;
node = malloc(sizeof(struct linknode));
node->value = value;
node->next = head->next;
head->next = node;
}
//尾插法单个数
void add2(struct linknode *head,int value){
struct linknode *node,*new;
new = malloc(sizeof(struct linknode));
node = head;
while(node->next!=NULL){
node = node->next;
}
new->next = NULL;
new->value = value;
node->next = new;
}
//尾插法插入n个数
void add21(Node head,int value){
Node node,new;
node = head;
int j;
for(j = 0; j < value; j++){
new = malloc(sizeof(struct linknode));
new->next = NULL;
new->value = j;
node->next = new;
node = new;
}
}
/**
* @brief 链表翻转:迭代翻转
* @note 参考博文:http://c.biancheng.net/view/8105.html
* 四种算法思想实现链表翻转
* - 迭代翻转:该算法的实现思想非常直接,就是从当前链表的首元节点开始,一直遍历至链表的最后一个节点,这期间会逐个改变所遍历到的节点的指针域,另其指向前一个节点。
* - 递归反转链表
* - 头插法反转链表
* - 就地逆置法反转链表:就地逆置法和头插法的实现思想类似,唯一的区别在于,头插法是通过建立一个新链表实现的,而就地逆置法则是直接对原链表做修改,从而实现将原链表反转。
* **/
Node fliplist1(Node head){
Node beg,mid,end;
if(head->next == NULL||head == NULL){
return NULL;
}
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;
}
//输出
void outlist(struct linknode *head){
while (head->next!=NULL)
{
head = head->next;
printf("value: %d\n",head->value);
}
}
//释放
void link_free(struct linknode *head)
{
struct linknode *p;
while(head!=NULL)
{
p = head;
head = p->next;
free(p);
}
}
int main(){
struct linknode *linklist;
Node head;
linklist = linkinit();
add2(linklist,1);
add2(linklist,2);
add2(linklist,3);
add2(linklist,4);
add2(linklist,5);
// add21(linklist,5);
outlist(linklist);
printf("翻转后:\n");
head = fliplist1(linklist);
outlist(head);
// outlist(linklist);
link_free(&linklist);
}