基于linux的C语言

一,栈

        1,栈

        一种特殊的受限的线性表,限制线性表只能在一端进行操作。且满足先进后出,后进先出的原则。

        2,栈顶,栈底和空栈

        栈顶:能够被允许操作的一端。

        栈底:不能被允许操作的一端。

        空栈:栈中没有任何数据时称为空栈。

注:能被操作的一定是栈的最后一个数据,即栈顶的数据。

        3,入栈和出栈

        入栈:从栈顶添加一个数据就称为入栈。

        出栈:从栈顶删除一个数据就称为出栈。

        4,栈顶指针

                用于存储栈顶指针的地址,只需操作栈顶指针就能使用整个栈。

 二,顺序栈

        逻辑结果为线性结构(栈),存储结构为顺序存储 顺序表,限制对数据的操作只能在顺序表的⼀端进⾏。 固定栈底位置,栈顶位置从栈底位置开始移动,⽤来表⽰当前能够操作的位置

        1,顺序栈的创建

        在顺序表末尾操作 申请⼀段连续的空间,⽤地址来表⽰先后顺序,存储数据,限制只能在⼀端 进⾏操作

struct stack
{
	int data[10];	//栈的数据大小
	
	int top;	//栈顶指针

};

struct stack * createstack()	//创建一个顺序栈
{
	struct stack *p = malloc(sizeof(struct stack));	//p代表顺序栈的地址。
	
	p -> top = 0;	//使得当前栈顶的位置为栈底位置,代表的含义为空栈
	
	return p;
}

           2,入栈

        入栈的两个操作:一,判断栈中的数据是否存满;二,移动栈顶的位置。

//要向栈中存入一个数据首先要判断栈中的数据是否存满。
int full(struct stack * head)
{
	if (head -> top==10)	//顺序栈数据的大小,首先创建时是空栈,没有存储任何数据,所以下标要和顺序栈的数据的大小保持一致。
	{
		printf("is flull\n");
		
		return 1;	//如果栈中的数据已经存满,则返回1.
	}
	else
		return 0;	//如果栈中的数据没有存满,则返回0.

}

//入栈
void pushstack(struct stack * head , int data)	//是整个栈中的元素命名为head,存入的数据为data.
{	
	if(full(head))
	{
		return ;
	}
	printf("push data is %d:\n",head -> data[head->top] = data);	//数据的存入。
	head -> top++;		//每当存入一个数据,栈顶的位置也就随着变更。

}
//函数调用
//函数调用
int main()
{
	struct stack * p = createstack();
	
	pushstack(p,10);    //入栈
	pushstack(p,11);
	pushstack(p,112);

	return 0;
}

            3,出栈

//进行出栈也就是删除数据时首先得判断栈中的数据是否为空。

int empty(struct stack *head)
{
	if(head->top==0)
	{
		printf("is empty\n");
		return 1;
	}
	else
		return 0;
}
void popstack(struct stack *head)
{
	if(empty(head))
	{
		return ;
	}
	head -> top--;
	printf("delete data is %d:\n",head -> data[head ->top]);
}

//函数调用
int main()
{
	struct stack * p = createstack();
	popstack(p);    //代表出栈
	
	pushstack(p,10);
	pushstack(p,11);
	pushstack(p,112);

	return 0;
}

        栈的基本操作:


#include<stdio.h>
#include<stdlib.h>

struct stack
{
	int data[10];	//栈的数据大小
	
	int top;	//栈顶指针

};

struct stack * createstack()	//创建一个顺序栈
{
	struct stack *p = malloc(sizeof(struct stack));	//p代表顺序栈的地址。
	
	p -> top = 0;	//使得当前栈顶的位置为栈底位置,代表的含义为空栈
	
	return p;
}

//要向栈中存入一个数据首先要判断栈中的数据是否存满。
int full(struct stack * head)
{
	if (head -> top==10)	//顺序栈数据的大小,首先创建时是空栈,没有存储任何数据,所以下标要和顺序栈的数据的大小保持一致。
	{
		printf("is flull\n");
		
		return 1;	//如果栈中的数据已经存满,则返回1.
	}
	else
		return 0;	//如果栈中的数据没有存满,则返回0.

}

//入栈
void pushstack(struct stack * head , int data)	//是整个栈中的元素命名为head,存入的数据为data.
{	
	if(full(head))
	{
		return ;
	}
	printf("push data is %d:\n",head -> data[head->top] = data);	//数据的存入。
	head -> top++;		//每当存入一个数据,栈顶的位置也就随着变更。

}

//进行出栈也就是删除数据时首先得判断栈中的数据是否为空。

int empty(struct stack *head)
{
	if(head->top==0)
	{
		printf("is empty\n");
		return 1;
	}
	else
		return 0;
}
void popstack(struct stack *head)
{
	if(empty(head))
	{
		return ;
	}
	head -> top--;
	printf("delete data is %d:\n",head -> data[head ->top]);
}

int main()
{
	struct stack * p = createstack();
	popstack(p);
	
	pushstack(p,10);
	pushstack(p,11);
	pushstack(p,112);

	return 0;
}

三,链式栈

        逻辑结构是栈(特殊线性关系),存储结构为链式存储 链表,限制在链表的⼀端进⾏操作。

        1,链式栈的创建

                链式栈的创建一般在头节点后作为链表的开始位置。

struct node
{
	int data;
	struct nodde * next;
}


//链式栈的创建。在头节点后作为链表的开始位置。
struct node * create()
{
	struct node * head = malloc(sizeof(struct node));
	if(head == NULL)
	{
		printf("create error\n");	//判断头节点位置的数据
		return	NULL;
	}
	head ->next = NULL;
	
	return head;
}

        2,入栈

                头节点始终指向栈顶的数据的地址。

//入栈
void pushstact(struct node * head ,int data)
{
	struct node * p = malloc(sizeof(struct node));
	
	p->data = data;
	
	p->next = head->next;
	head->next = p;
}

//函数调用
int main()
{

	
	struct node * head = create();

	pushstack(head,10);
	pushstack(head,20);
	pushstack(head,30);
	pushstack(head,40);
	
	return 0;
}

        3,出栈

        

//判断是否有数据
int empty(struct node * head)
{
	
	if( head->next == NULL )
	{
		printf("is empty\n");
		return 1;
	}
	else
		return 0;
}

//出栈
void popstact(struct node * head)
{
	if(empty(head))
		return ;
		
	struct node * p = head -> next;
	
	head->next = p->next;
	
	printf("delete data is %d:\n",p->data);

}

int main()
{

	
	struct node * head = create();

    popstack(head);

	pushstack(head,10);
	pushstack(head,20);
	pushstack(head,30);
	pushstack(head,40);
	
	popstack(head);
	popstack(head);
	
	return 0;
}

        链式栈的综合操作

//链式栈
#include<stdio.h>
#include<stdlib.h>

struct node
{
	int data;
	struct node * next;
};


//链式栈的创建。在头节点后作为链表的开始位置。
struct node * create()
{
	struct node * head = malloc(sizeof(struct node));
	if(head == NULL)
	{
		printf("create error\n");	//判断头节点位置的数据
		return	NULL;
	}
	head ->next = NULL;
	
	return head;
}
//入栈
void pushstack(struct node * head ,int data)
{
	struct node * p = malloc(sizeof(struct node));
	
	p->data = data;
	
	p->next = head->next;
	head->next = p;
}

//判断是否有数据
int empty(struct node * head)
{
	
	if( head->next == NULL )
	{
		printf("is empty\n");
		return 1;
	}
	else
		return 0;
}

//出栈
void popstack(struct node * head)
{
	if(empty(head))
		return ;
		
	struct node * p = head -> next;
	
	head->next = p->next;
	
	printf("delete data is %d:\n",p->data);

}
int main()
{

	
	struct node * head = create();

	pushstack(head,10);
	pushstack(head,20);
	pushstack(head,30);
	pushstack(head,40);
	
	popstack(head);
	popstack(head);
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于您所提到的问题,我能够给您一些基本的思路和实现方法。管道通信是Linux系统中一种非常常见的进程间通信方式,可以实现不同进程之间数据的传输和共享。下面是一份基于Linux C语言的管道通信例程,您可以参考一下: 1. 父进程创建一个管道,通过fork()函数创建一个子进程来读取管道中的数据。 ``` #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(){ int fd[2]; pid_t pid; char buffer[20]; //创建管道 if(pipe(fd)<0){ printf("Create pipe error!\n"); exit(1); } //创建子进程 pid = fork(); if(pid < 0){ printf("Create process error!\n"); exit(1); }else if(pid == 0){ //子进程读取管道中的数据 close(fd[1]); int n = read(fd[0], buffer, sizeof(buffer)); printf("Child process received: %s\n", buffer); close(fd[0]); }else{ //父进程向管道中写入数据 close(fd[0]); char* data = "Hello, child process!"; write(fd[1], data, sizeof(data)); printf("Parent process sent: %s\n", data); close(fd[1]); } return 0; } ``` 2. 父进程向管道中写入数据,子进程进行读取并输出。 以上面的代码为例,首先父进程通过pipe()函数创建了一个管道fd,接着通过fork()函数创建了一个子进程,并通过pid变量来判断当前进程是否为父进程或子进程。在父进程中,我们先关闭了管道的读端,然后通过write()函数向管道中写入了数据"data",并输出了发送成功的信息。在子进程中,我们先关闭了管道的写端,然后通过read()函数从管道中读取数据到buffer缓冲区中,并输出读取的结果。 这就是一个简单的基于Linux C语言的管道通信例程实现方法。当然,具体实现方法还需要根据实际情况进行调整,但是我们通过这个例子可以清晰地了解到管道通信的基础原理和实现方法,希望能够帮到您。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值