数据结构 day2

思维导图

1.链表的基本操作

头文件

#ifndef __LINK_LIST_H__
#define __LINK_LIST_H__
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
	int data;
	struct node *next;
}node,*node_p;
node_p create_link();
node_p create_node(int data);
void insert_head(node_p H,int data);
int empty_link(node_p H);
void show_link(node_p H);
void delete_head(node_p H);
void insert_tail(node_p H,int data);
void delete_tail(node_p H);
void insert_location(node_p H,int data,int location);
void delete_location(node_p H,int location);
int search_value(node_p H,int value);
int search_location(node_p H,int location);
#endif

main函数

#include "link.h"
int main(int argc, const char *argv[])
{
	node_p H=create_link();
	printf("insert nums at the start\n");
	insert_head(H,5);
	insert_head(H,6);
	insert_head(H,3);
	insert_head(H,3);
	insert_head(H,4);
	insert_head(H,10);
	insert_head(H,20);
	insert_head(H,44);
	show_link(H);
	putchar(10);
	printf("delete the head\n");
	delete_head(H);
	show_link(H);
	putchar(10);
	printf("insert 666 after the end\n");
	insert_tail(H,666);
	show_link(H);
	putchar(10);
	printf("delete the end\n");
	delete_tail(H);
	show_link(H);
	putchar(10);
	printf("add 857 after the second num\n");
	insert_location(H,857,2);
	show_link(H);
	putchar(10);
	printf("delete the 4th num\n");
	delete_location(H,4);
	show_link(H);
	putchar(10);
	printf("the num 5 is in the location %d\n",search_value(H,5));
	putchar(10);
	printf("the num 7 is in the location:\n");
	search_value(H,7);
	putchar(10);
	printf("the location 3 is num %d\n",search_location(H,3));
	return 0;
}

自定义函数

#include "link.h"
node_p create_link()//创建链表头
{
	node_p H=(node_p)malloc(sizeof(node));
	if(H==NULL)
	{
		printf("apply fail\n");
		return NULL;
	}
	H->data=0;
	H->next=NULL;
	return H;
}
node_p create_node(int data)//创建节点
{
	node_p new=(node_p)malloc(sizeof(node));
	if(new==NULL)
	{
		printf("apply fail\n");
		return NULL;
	}
	new->data=data;
	new->next=NULL;
	return new;
}
void insert_head(node_p H,int data)//头插入
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	node_p new=create_node(data);
	new->next=H->next;
	H->next=new;
	H->data++;
}
int empty_link(node_p H)//判空
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return -1;
	}
	return H->next==NULL?1:0;
}
void show_link(node_p H)//输出链表
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(empty_link(H))
	{
		printf("link is empty\n");
		return;
	}
	node_p p=H->next;
	for(;p!=NULL;)
	{
		printf("%d->",p->data);
		p=p->next;
	}
	putchar(10);
}
void delete_head(node_p H)//头删除
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(empty_link(H))
	{
		printf("link is empty\n");
		return;
	}
	node_p p=H->next;
	H->next=H->next->next;
	free(p);
	H->data--;
}
void insert_tail(node_p H,int data)//尾插入
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	node_p new=create_node(data);
	node_p p=H->next;
	for(;p->next!=NULL;)
		p=p->next;
	p->next=new;
	H->data++;
}
void delete_tail(node_p H)//尾删除
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(empty_link(H))
	{
		printf("link is empty\n");
		return;
	}

	node_p p=H->next;
	for(;p->next->next!=NULL;)
		p=p->next;
	node_p p1=p->next;
	p->next=NULL;
	free(p1);
	H->data--;
}
void insert_location(node_p H,int data,int location)//位置插入
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(location<0||location>H->data)
	{
		printf("location wrong define\n");
		return;
	}
	node_p new=create_node(data);
	node_p p=H;
	for(int i=0;i<location;i++)
	{
		p=p->next;
	}
	new->next=p->next;
	p->next=new;
	H->data++;
}
void delete_location(node_p H,int location)//位置删除
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(empty_link(H))
	{
		printf("link is empty\n");
		return;
	}
	if(location<0||location>H->data)
	{
		printf("location wrong define\n");
		return;
	}
	node_p p=H;
	for(int i=0;i<location-1;i++)
	{
		p=p->next;
	}
	node_p p1=p->next;
	p->next=p->next->next;
	free(p1);
	H->data--;
}
int search_value(node_p H,int value)//值查找
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return -1;
	}
	if(empty_link(H))
	{
		printf("link is empty\n");
		return -1;
	}
	node_p p=H->next;
	int i=1;
	for(;p!=NULL&&p->data!=value;)
	{
		p=p->next;
		i++;
	}
	if(i==H->data+1)
	{
		printf("the value have not found\n");
		return -1;
	}
	return i;
}
int search_location(node_p H,int location)//位置查找
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return -1;
	}
	if(empty_link(H))
	{
		printf("link is empty\n");
		return -1;
	}
	if(location<0||location>H->data)
	{
		printf("location wrong define\n");
		return -1;
	}
	node_p p=H;
	for(int i=0;i<location;i++)
	{
		p=p->next;
	}
	return p->data;
}

功能实现

2.结构体大小

#pragma pack(2)    //指定两字节对齐
typedef struct
{
    char x;
    struct A
    {
        short a;
        int *b;
        char c;    
    }p;
    long b;
}T;
#pragma pack()

字节大小:22

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值