C语言 链表、队列、栈

链表

通用结点

typedef struct _node{//结点 
  	int value;
  	struct _node *next;
}Node;

头结点和尾结点

typedef struct list{//链表 
 	Node* head;
 	Node* tail;
}List;

基本链表

存入一连串整数遇到-1结束

#include<stdio.h>
#include<stdlib.h>
//节点声明 
typedef struct _node{
	 int value;
	 struct _node *next;
}Node;
int main()
{
	 Node *head=NULL;					//结点头 
	 int number;
	 do{
		  scanf("%d",&number);				//number存储输入 
		  if(number!=-1){
		   	Node*p=(Node*)malloc(sizeof(Node));	//临时节点 
		   	p->value=number;
		   	p->next=NULL;
		   
		   if(head!=NULL){				//是否是第一个节点 
		    						//链接之前的最后一个节点 
		    	Node*last=head;
		    	while(last->next){
		     	last=last->next;
		    	}
		    last->next=p;				//完成节点链接 
		    }else{
		    head=p;
		    }
		 }
	 }while(number!=-1); 
	 
	 Node*q=head;
	 while((q->next)!=NULL){
	   	printf("%d\n",q->value);
	  	q=q->next;
	 }
	 printf("%d\n",q->value);
	 return 0;
} 

多功能链表

功能:新建链表,删除指定值(链表中只有一个),打印链表,清空链表

#include<stdio.h>
#include<stdlib.h>
typedef struct _node{			//结点 
	 int value;
	 struct _node *next;
}Node;
typedef struct list{			//链表 
	 Node* head;
	 Node* tail;
}List;
void _add(int number,List *pList);	//将number链在链表末尾 
void _print(List*pList);		//打印链表 
void _remove(List*pList,int number);	//移除链表中的number 
void _free(List*pList);			//清空链表 
int main()
{
	 List list;
	 list.head=list.tail=NULL;
	 int number;
	 do{
	  scanf("%d",&number);
	  _add(number,&list);
	 }while(number!=-1);
	 _print(&list);
	  scanf("%d",&number);
	 _remove(&list,number);
	 _print(&list);
	 _free(&list);
	 return 0;
 } 
 void _add(int number,List *pList)
 {
	  if(number!=-1){
		   Node*p=(Node*)malloc(sizeof(Node));
		   p->value=number;
		   p->next=NULL;
		   if(pList->head){
			    pList->tail->next=p;
			    pList->tail=p;
		   }else{
		   	 pList->head=pList->tail=p;
		   }
	 };
}
void _print(List*pList)
 {
	  Node*p;
	  for(p=pList->head;p;p=p->next){
	   	printf("%d ",p->value);
	  }
	  printf("\n");
 }
 void _remove(List*pList,int number)
 { 
	 Node*p=pList->head;
	 Node*q=NULL;
	 for(;p;q=p,p=p->next){
	  	if(p->value==number){
		   	if(q){
		    		q->next=p->next;

		   	}else{
		    		pList->head=p->next;
		   	}
		 	free(p);
			break;
		}
	 }
}
void _free(List*pList)
 {
  	Node*q=pList->head;
 	Node*p=q->next;
 	for(;q;q=p,p=p->next){
  		free(q);
 	}
 }

队列

特点:先进先出(First In First Out)

结构

#include<stdio.h>
typedef struct {					//队结构
	 int a[10];
	 int head;
	 int tail;
}Queue;
int main()
{
	 Queue qu;
	 qu.head=0;
	 qu.tail=0;
	 int cnt;
	 	scanf("%d",&cnt);
	 int i;
	 for(i=0;i<cnt;i++){
	  	scanf("%d",&(qu.a[qu.tail]));		//入队
	  	qu.tail++;				
	 }
	 for(i=qu.head;i<qu.tail;i++){			//查看队列成员
	  	printf("%d ",qu.a[i]);			
	 }
	 printf("\n");
	 for(i=0;i<2;i++){				//出队(前两个数)
	  	qu.head++;
	 }
	 for(i=qu.head;i<qu.tail;i++){			//查看队列成员
	  	printf("%d ",qu.a[i]);
	 }
	 return 0;
} 

特点:先进后出

结构

#include<stdio.h>
typedef struct {					//栈结构 
	 int data[10];
	 int top;
}Stack;
int main()
{
	 Stack stack;
	 stack.top=0;
	 int cnt;
	 	scanf("%d",&cnt);
	 int i;
	 for(i=0;i<cnt;i++){
	  	scanf("%d",&stack.data[stack.top]);	//入栈 
	  	stack.top++;
	 }
	 do{
	  	stack.top--;				//出栈 
	  	printf("%d ",stack.data[stack.top]);
	 }while(stack.top>0);
 	 return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值