链表、队列、栈

单项链表(不带头结点)

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

//结点结构
typedef struct ListNode
{
	int nVal;
	struct ListNode* next;
}Node;

//创建新结点
void createNode(Node** newNode)
{
	*newNode = (Node*)malloc(sizeof(Node));

	if( NULL == (*newNode))
	{
		printf("创建结点失败\n");
		exit(-1);
	}
}

//创建链表
void createLink(Node** head)
{
	*head = NULL;
}


//插入结点  头插法
void insertNode(Node** head,Node** newNode)
{
	if( NULL == (*head))
	{
		*head = *newNode;
		(*head)->next = NULL;
	}
	else
	{
		(*newNode)->next = *head;
		*head = *newNode;
	}
}

//显示链表内容
void display(Node* head)
{
	Node* p = head;

	if( p == NULL)
	{
		printf("链表为空\n");
		return;
	}

	while( p != NULL)
	{
		printf("%d\n", p->nVal);
		p = p->next;
	}
}

void release_Link(Node **head)
{
	Node* p = *head;
	
	while( p != NULL)
	{
		(*head) = p->next;
		free(p);
		p = *head;
	}
}

int main()
{
	Node* head = NULL;
	Node* newNode = NULL;

	createNode(&newNode);
	newNode->nVal = 50;

	insertNode(&head, &newNode);

	createNode(&newNode);
	newNode->nVal = 40;
	insertNode(&head, &newNode);

	display(head);

	release_Link(&head);

	display(head);

	return 0;
}

​

笔试题:题目:请从已经排序好的链表中删除重复的节点,保证每个节点只出现一次。

示例1:
输入:
1->1->2
输出:
1->2

示例2:
输入:
1->1->2->3->3
输出:
1->2->3

struct ListNode
{
 int nVal;
 ListNode* pNext;
}

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

typedef struct ListNode
{
	int nVal;
	struct ListNode* next;
}Node;

//创建结点
void createNode( Node** newNode )
{
	*newNode = (Node*)malloc(sizeof(Node));
	
	if( NULL == (*newNode))
	{
		printf("结点创建失败\n");
		exit(-1);
	}
}

//创建链表
void createLink( Node** head )
{
	createNode(head);
	(*head)->next = NULL;
}

//尾插法
void insertNode(Node** head , Node** newNode)
{
	if( (*head)->next == NULL )
	{
		(*head)->next = *newNode;
		(*newNode)->next = NULL;
	}
	else
	{
		Node* p = (*head)->next;

		while(p->next != NULL)
		{
			p = p->next;
		}

		p->next = *newNode;
		(*newNode)->next = NULL;
	}
}

void dispaly(Node* head)
{
	if(head->next == NULL)
	{
		printf("链表为空\n");
	}
	else
	{
		Node* p = head->next;

		while( p != NULL)
		{
			printf("%d\n", p->nVal);

			p = p->next;
		}
	}
}

void delNode(Node *head)
{
	Node* p = head->next;
	Node* q = NULL;

	while( p != NULL)
	{
		if( p->nVal == p->next->nVal)
		{
			q = p->next;
			p->next = q->next;

			free(q);
		}
		p = p->next;
	}
}

int main()
{
	Node* head = NULL;
	Node* newNode = NULL;
	int num;
	int i;

	createLink(&head);

	for(i = 0; i < 5; i++)
	{
		createNode(&newNode);

		printf("请输入数字:\n");
		scanf("%d", &num);
		newNode->nVal = num;

		insertNode(&head, &newNode);
	}

	dispaly(head);

	delNode(head);

	printf("删除重复结点后\n");

	dispaly(head);

	return 0;
}

循环队列

//循化队列
#include <stdio.h>
#include <stdlib.h>

#define SIZE 10
#define YES 100
#define NO  101

//定义队列结构
typedef struct cque
{
    int data[SIZE];
    int front;
    int rear;
}Cque;


//创建队列,分配空间
int create_que(Cque **queue)
{
    *queue = (Cque*)malloc(sizeof(Cque));
    
    //判断空间分配是否成功
    if( NULL == *queue)
    {
        return NO;
    }
    return YES;
}

//初始化队列
int init_que(Cque *queue)
{
    if( NULL == queue)
    {
        return NO;
    }

    queue->front = queue->rear = 0;

    return  YES;
}

//判断队满
int is_full(Cque *queue)
{
    //为了防止和队空的判断条件重叠,留出一个空间不使用
    if( (queue->rear+1)%SIZE == queue->front)
    {
        return YES;
    }
    return NO;
}

//判断队空
int is_empty(Cque *queue)
{
    if(NULL == queue || queue->front == queue->rear)
    {
        return YES;
    }

    return NO;
}

//入队,需要先判断队列是否已满
int push_que(Cque *queue,int num)
{
    if( YES == is_full(queue))
    {
        return NO;
    }

    queue->data[queue->rear] = num;
    queue->rear = (queue->rear + 1) % SIZE;

    return YES;
}


//出队
int pop_que(Cque *queue,int *num)
{
    //先判断是否队空
    if( YES == is_empty(queue))
    {
        return NO;
    }

    *num = queue->data[queue->front];
    queue->front = (queue->front + 1) % SIZE;

    return YES;
}

//获取队首元素
int get_front(Cque *queue,int *num)
{
    //先判断是否队空
    if( YES == is_empty(queue))
    {
        return NO;
    }

    *num = queue->data[queue->front];

    return YES;
}


int main()
{
    int i;
    int num;
    //创建指向队列的指针
    Cque *queue = NULL;

    //创建队列,分配空间
    if( NO == create_que(&queue))
    {
        printf("创建队列失败!\n");
        return NO;
    }

    //初始化队列
    if( NO == init_que(queue))
    {
        printf("初始化失败!\n");
        return NO;
    }

    //判断是否队空
    if( is_empty(queue) == YES)
    {
        printf("队列为空!\n");
    }

    //入队
    for(i = 0; i < 15 ; i++)
    {
        if( NO == push_que(queue,i+1))
        {
            printf("入队失败!\n");
        }
    }

    //判断是否队满
    if( is_full(queue) == YES)
    {
        printf("队列已满!\n");
    }

    //出队
    for(i = 0; i < 5 ; i++)
    {
       if( NO == pop_que(queue,&num))
        {
            printf("出队失败!\n");
        }
        else
        {
            printf("data is %d\n",num);
        }   
    }
    
    //获得队首元素
    if( NO == get_front(queue,&num))
        {
            printf("获取队首元素失败!\n");
        }
        else
        {
            printf("front data is %d\n",num);
        }   

    return 0;
}

 栈

 

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

#define SIZE 10
#define OK 100
#define NO 101

typedef struct stack
{
	int data[SIZE];
	int top;
}Stack;


//给栈分配空间
int createStack(Stack** stack)
{
	*stack = (Stack*)malloc(sizeof(Stack));

	if (*stack == NULL)
	{
		return NO;
	}

	return OK;
}

//给栈初始化
void initStack(Stack* stack)
{
	stack->top = -1;
}


//判断栈满
int isFull(Stack* stack)
{
	if(stack->top + 1 == SIZE)
	{
		return OK;
	}

	return NO;
}

//判断栈空
int isEmpty(Stack* stack)
{
	if(NULL == stack || stack->top == -1)
	{
		return OK;
	}

	return NO;
}

//入栈
int pushStack(Stack* stack,int num)
{
	if(OK == isFull(stack))
	{
		return NO;
	}

	(stack->top)++;

	stack->data[stack->top] = num;

	return OK;
}

//出栈
int popStack(Stack* stack, int* num)
{
	if (OK == isEmpty(stack))
	{
		return NO;
	}

	*num = stack->data[stack->top];
	(stack->top)--;

	return OK;
}

//获取栈顶元素
int get_top(Stack* stack, int* num)
{
	if (OK == isEmpty(stack))
	{
		return NO;
	}

	*num = stack->data[stack->top];

	return OK;
}

int main()
{
	Stack* stack = NULL;
	int num;

	if(NO == createStack(&stack))
	{
		printf("创建栈失败\n");
		exit(-1);
	}

	initStack(stack);

	for(int i = 1; i <= 5; i++)
	{
		pushStack(stack, i);
	}

	get_top(stack, &num);
	printf("获取栈顶元素%d\n", num);

	printf("出栈\n");
	for (int i = 1; i <= 5; i++)
	{
		popStack(stack, &num);
		printf("%d\n", num);
	}

	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值