单链表
1、不带头结点
(头文件)
#ifndef __LINKEDLIST_H__
#define __LINKEDLIST_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
typedef int DataType;
struct LinkedNode
{
DataType data;
struct LinkedNode *next;
};
extern struct LinkedNode *CreateNode(DataType data);
extern struct LinkedNode *CreateList();
extern void PrintList(struct LinkedNode *first);
extern bool DestroyList(struct LinkedNode *first);
extern struct LinkedNode *UpdataList(struct LinkedNode *first,DataType olddata,DataType newdata);
extern struct LinkedNode *DeleteList(struct LinkedNode *first,DataType data);
extern struct LinkedNode *InsertNode(struct LinkedNode *first, DataType scrdata,DataType newdata);
#endif
(函数文件)
#include "LinkedList.h"
// 创建节点
struct LinkedNode *CreateNode(DataType data)
{
struct LinkedNode *pnew = malloc(sizeof(*pnew));
if (pnew == NULL)
return NULL;
pnew->data = data;
pnew->next = NULL;
return pnew;
}// 创建链表
struct LinkedNode *CreateList()
{
struct LinkedNode *first = NULL;
struct LinkedNode *last = NULL;while (1)
{
DataType d;
scanf("%d", &d);
if (d == 0)
break;struct LinkedNode *pnew = CreateNode(d);
if (pnew == NULL)
return NULL;
// 头插法
if (first == NULL && last == NULL)
first = last = pnew;
else
{
pnew->next = first;
first = pnew;
}
/*
//尾插法
if(first == NULL && last == NULL)
first = last = pnew;
else
{
last->next = pnew;
last = pnew;
}
*/
}
return first;
}// 输出链表
void PrintList(struct LinkedNode *first)
{
if (first == NULL)
{
printf("=======================\n");
return;
}
struct LinkedNode *p = first;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}// 修改节点数据
struct LinkedNode *UpdataList(struct LinkedNode *first, DataType olddata, DataType newdata)
{
struct LinkedNode *p = first;
while (p)
{
if (p->data == olddata)
{
p->data = newdata;
}
else
{
p = p->next;
}
}
return first;
}//插入一个数据(按照从大到小的顺序插入)
struct LinkedNode *InsertNode(struct LinkedNode *first, DataType scrdata,DataType newdata)
{
struct LinkedNode *pnew = malloc(sizeof(*pnew));
if (pnew == NULL)
return NULL;
pnew->data = newdata;
pnew->next = NULL;
//查找
struct LinkedNode *p = first;
struct LinkedNode *pre = NULL;
while(p)
{
if(p->data == scrdata)
break;
pre = p;
p = p->next;
}
if(first == NULL)
first = pnew;else if(p == NULL)
{
pre->next = pnew;
}
else
{
pnew->next = p->next;
p->next = pnew;
}
return first;
}
// 删除节点(只删除一个)
struct LinkedNode *DeleteList(struct LinkedNode *first, DataType data)
{
if (first == NULL)
return NULL;// 查找
struct LinkedNode *p = first;
struct LinkedNode *pre = NULL;
while (p)
{
if (p->data == data)
break;
pre = p;
p = p->next;
}if (p == NULL)
return first;// 删除
if (p == first)
{
first = first->next;
p->next = NULL;
free(p);
p = NULL;
}
else
{
pre->next = p->next;
p->next = NULL;
free(p);
p = NULL;
}
return first;
}// 销毁
bool DestroyList(struct LinkedNode *first)
{
if (first == NULL)
return false;
struct LinkedNode *p = first;
while (p)
{
first = first->next;
p->next = NULL;
free(p);
p = first;
}
return true;
}
2、带头节点
(头文件)
#ifndef __LINKEDLISTWITHHEAD_H__
#define __LINKEDLISTWITHHEAD_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef int DataType;struct Node
{
DataType data;
struct Node *next;
};struct HeadNode
{
struct Node *first;
struct Node *last;
int NodeNum;
};extern struct HeadNode *InitHead();
extern struct HeadNode *CreateList();
extern void PrintList(struct HeadNode *head);
extern struct HeadNode *InsertList(struct HeadNode *head, DataType d);
extern struct HeadNode *DeleteList(struct HeadNode *head, DataType d);
extern struct HeadNode *UpdateData(struct HeadNode *head, DataType x,DataType y);
extern bool DestroyList(struct HeadNode *head);
#endif
(函数文件)
#include "LinkedListWithHead.h"
// 初始化
struct HeadNode *InitHead()
{
struct HeadNode *head = malloc(sizeof(*head));
if (head == NULL)
return NULL;head->first = NULL;
head->last = NULL;
head->NodeNum = 0;
return head;
}// 创建链表
struct HeadNode *CreateList()
{
struct HeadNode *head = InitHead();
if (head == NULL)
return NULL;while (1)
{
DataType d;
scanf("%d", &d);
if (d == 0)
break;
struct Node *pnew = malloc(sizeof(*pnew));
if (pnew == NULL)
return head;pnew->data = d;
pnew->next = NULL;
if (head->first == NULL && head->last == NULL && head->NodeNum == 0)
{
head->first = head->last = pnew;
head->NodeNum++;
}
else
{
//尾插
head->last->next = pnew;
head->last = pnew;
head->NodeNum++;
/*
//头插
pnew->next = head->first;
head->first =pnew;
*/
}
}
return head;
}// 增加节点(从小到大顺序插入)
struct HeadNode *InsertList(struct HeadNode *head, DataType d)
{
if (head == NULL)
return NULL;
struct Node *pnew = malloc(sizeof(*pnew));
if (pnew == NULL)
return head;
pnew->data = d;
pnew->next = NULL;// 查找
struct Node *p = head->first;
struct Node *pre = NULL;
while (p)
{
if (p->data > d)
break;
pre = p;
p = p->next;
}
// 插入
if (head->first == NULL && head->last == NULL && head->NodeNum == 0)
{
head->first = head->last = pnew;
}
else if(p == NULL)
{
head->last->next = pnew;
head->last = pnew;
}
else if (p == head->first)
{
pnew->next = head->first;
head->first = pnew;
}
else
{
pre->next = pnew;
pnew->next = p;
}
head->NodeNum++;
return head;
}// 删除节点(删除表内所有与所删数相同的数)
struct HeadNode *DeleteList(struct HeadNode *head, DataType d)
{
if(head == NULL)
return NULL;
struct Node *p = head->first;
struct Node *pre = NULL;
while(p)
{
if(p->data == d)
{
//删除
if(p == head->first)
{
head->first = head->first->next;
if(head->first == NULL)
return NULL;
p->next = NULL;
free(p);
p = head->first;
}
else
{
pre->next = p->next;
p->next = NULL;
free(p);
p = head->first;
}
head->NodeNum--;
}
else
{
pre = p;
p = p->next;
}
}
return head;
}
// 修改节点(将链表中的数字x修改成y)
struct HeadNode *UpdateData(struct HeadNode *head, DataType x,DataType y)
{
if(head == NULL)
return NULL;
struct Node *p = head->first;
while (p)
{
if(p->data == x)
{
p->data = y;
}
p = p->next;
}
return head;
}
// 销毁链表
bool DestroyList(struct HeadNode *head)
{
if(head == NULL)
return false;
struct Node *p = head->first;
while(p)
{
head->first = head->first->next;
p->next = NULL;
free(p);
p = head->first;
}
free(head);
head = NULL;
return true;
}// 打印链表
void PrintList(struct HeadNode *head)
{
if (head == NULL)
{
printf("=====================\n");
return;
}
printf("head->first = %d head->last = %d head->NodeNum = %d\n",head->first->data,head->last->data,head->NodeNum);
struct Node *p = head->first;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
双向链表(循环链表)
(头文件)
#ifndef __LOOPDOUBLELINKEDLISTWITHHEAD_H__
#define __LOOPDOUBLELINKEDLISTWITHHEAD_H__
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
typedef int DataType;typedef struct Node
{
DataType data;
struct Node *pre;
struct Node *next;
}node;typedef struct HeadNode
{
struct Node *first;
struct Node *last;
int NodeNum;
}Head;extern struct HeadNode *InitList();
extern struct HeadNode *CreateList();
extern void PrintList(struct HeadNode *head);
extern struct HeadNode *UpdateNode(struct HeadNode *head, DataType olddata, DataType newdata);
extern struct HeadNode *InsertNode(struct HeadNode *head, DataType x, DataType y);
extern struct HeadNode *DeleteNode(struct HeadNode *head, DataType data);
extern bool DestoryList(struct HeadNode *head);
#endif
(函数文件)
#include "LoopDoubleLinkedListWithHead.h"
// 初始化
struct HeadNode *InitList()
{
struct HeadNode *head = malloc(sizeof(*head));
if (head == NULL)
return NULL;
head->first = head->last = NULL;
head->NodeNum = 0;
return head;
}// 创建新链表
struct HeadNode *CreateList()
{
struct HeadNode *head = InitList();
if (head == NULL)
return NULL;
DataType d;
while (1)
{
scanf("%d", &d);
if (d == 0)
break;
// 创建新节点struct Node *pnew = malloc(sizeof(*pnew));
if (pnew == NULL)
return head;
pnew->data = d;
pnew->next = pnew->pre = NULL;// 插入
if (head->first == NULL && head->last == NULL && head->NodeNum == 0)
{
head->first = head->last = pnew;
}else
{
// 头插
// pnew->next = head->first;
// head->first->pre = pnew;
// head->first = pnew;// 尾插
head->last->next = pnew;
pnew->pre = head->last;
head->last = pnew;
}
head->NodeNum++;
}
if(head->NodeNum)
{
head->first->pre = head->last;
head->last->next = head->first;
} //与双向链表唯一不同之处,实现首位相连
return head;
}
// 增加节点
// 将y插入到第一个出现的x前面,如果找不到x,将y插入链表末尾
struct HeadNode *InsertNode(struct HeadNode *head, DataType x, DataType y)
{
if (head == NULL)
return NULL;
struct Node *pnew = malloc(sizeof(*pnew));
if (pnew == NULL)
return head;pnew->data = y;
pnew->next = pnew->pre = NULL;// 查找x
int num = head->NodeNum;
struct Node *p = head->first;
while (num)
{
if (p->data == x)
break;
p = p->next;
num--;
}
if (head->first == NULL && head->last == NULL && head->NodeNum == 0)
head->first = head->last = pnew;
else if (!num)
{
head->last->next = pnew;
pnew->pre = head->last;
head->last = pnew;
}
else
{
if (p == head->first)
{
pnew->next = head->first;
head->first->pre = pnew;
head->first = pnew;
}
else
{
pnew->next = p;
pnew->pre = p->pre;
p->pre->next = pnew;
p->pre = pnew;
}
}
head->NodeNum++;
return head;
}
// 修改节点
struct HeadNode *UpdateNode(struct HeadNode *head, DataType olddata, DataType newdata)
{
if (head == NULL)
return NULL;
struct Node *p = head->first;
int num = head->NodeNum;
while (num)
{
if (p->data == olddata)
p->data = newdata;p = p->next;
num--;
}
return head;
}
// 删除节点(全部)
struct HeadNode *DeleteNode(struct HeadNode *head, DataType data)
{
if (head == NULL)
return NULL;
struct Node *p = head->first;
int num = head->NodeNum;
while (num)
{
if (p->data == data)
{
// 删除
if (p == head->first)
{
head->first = head->first->next;
if (head->first == NULL)
{
free(p);
p = NULL;
head->NodeNum--;
return head;
}
head->first->pre = NULL;
p->next = NULL;
p->pre = NULL;
free(p);
p = head->first;
}
else if(p == head->last)
{
p->pre->next = NULL;
head->last = p->pre;
p->pre = NULL;
p->next = NULL;
free(p);
p = head->first;
}
else
{
p->pre->next = p->next;
p->next->pre = p->pre;
p->next = NULL;
p->pre = NULL;
free(p);
p = head->first;
}
head->NodeNum--;
}
else
{
p = p->next;
num--;
}
}
return head;
}// 打印
void PrintList(struct HeadNode *head)
{
int num = head->NodeNum;
if (head == NULL)
{
printf("=============\n");
return ;
}
if(head->NodeNum == 0)
{
printf("List is NULL\n");
return ;
}
struct Node *p = head->first;
printf("从左往右:");
while (num)
{
printf("%d ", p->data);
p = p->next;
num--;
}
printf("\n");
printf("从右往左:");p = head->last;
num = head->NodeNum;
while (num)
{
printf("%d ", p->data);
p = p->pre;
num--;
}
printf("\n");printf("head->NodeNum = %d\n", head->NodeNum);
}// 销毁链表
bool DestoryList(struct HeadNode *head)
{
if(head == NULL)
return false;
struct Node *p = head->first;
int num = head->NodeNum;
while(num)
{
head->first = head->first->next;
p->pre = NULL;
p->next = NULL;
free(p);
p = head->first;
num--;
}
free(head);
head = NULL;
return true;
}
栈(先进后出)
1、顺序栈
(头文件)
#ifndef __SEQSTACK_H__
#define __SEQSTACK_H__#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef char DataType;
struct SeqStack
{
int capacity;
int top;
DataType *data;
};
extern struct SeqStack *InitStack();
extern void Destroy_Stack(struct SeqStack*s);
extern bool Clear_Stack(struct SeqStack*s);
extern bool IsEmpty(struct SeqStack*s);
extern bool IsFull(struct SeqStack*s);
extern bool Push(struct SeqStack*s, DataType d);
extern bool Pop(struct SeqStack*s, DataType *d);
extern bool Get_Top(struct SeqStack*s, DataType *d);
extern int Get_Length(struct SeqStack*s);
#endif
(函数文件)
#include "SeqStack.h"
//初始化
struct SeqStack *InitStack(int capacity)
{
struct SeqStack *s = malloc(sizeof(*s));
if(s == NULL)
return NULL;
s->data = calloc(capacity,sizeof(*s->data));
if(s->data == NULL)
{
free(s);
return NULL;
}
s->capacity = capacity;
s->top = -1;
return s;
}
//销毁
void Destroy_Stack(struct SeqStack*s)
{
if(s == NULL)
return;
if(s->data)
free(s->data);
free(s);
s = NULL;
}//清空
bool Clear_Stack(struct SeqStack*s)
{
if(s == NULL)
return false;
s->top = -1;
return true;
}
//判断是否为空
bool IsEmpty(struct SeqStack*s)
{
if(s == NULL)
return false;
return s->top == -1 ? true : false;
}//判断是否为满
bool IsFull(struct SeqStack*s)
{
if(s == NULL)
return false;
return s->capacity == s->top+1 ? true : false;
}//入栈
bool Push(struct SeqStack*s, DataType d)
{
if(s == NULL || IsFull(s))
return false;
s->data[s->top + 1] = d;
s->top++;
return true;
}//出栈
bool Pop(struct SeqStack*s, DataType *d)
{
if(s == NULL || IsEmpty(s))
return false;
*d = s->data[s->top];
s->top--;return true;
}//获取栈顶元素
// Get_Top
bool Get_Top(struct SeqStack*s, DataType *d)
{
if(s == NULL || IsEmpty(s))
return false;
*d = s->data[s->top];
return true;
}//获取栈的长度
int Get_Length(struct SeqStack*s)
{
if(s == NULL)
return -1;
return s->top+1;
}
2、链式栈
(头文件)
#ifndef __LINKEDSTACK_H__
#define __LINKEDSTACK_H__#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int DataType;
struct Node
{
DataType data;
struct Node *next;
};
struct LinkedStack
{
int size;
struct Node *top;
};
extern struct LinkedStack *CreateStack();
extern bool Is_Empty(struct LinkedStack *s);
extern bool Clear_Stack(struct LinkedStack *s);
extern bool Destroy_Stack(struct LinkedStack *s);
extern int Get_Length(struct LinkedStack *s);
extern bool Pop(struct LinkedStack *s,DataType *d);
extern void Dec_to_Oct(int num);
#endif
(函数文件)
#include "LinkedStack.h"
//创建空栈
struct LinkedStack *CreateStack()
{
struct LinkedStack *s = malloc(sizeof(*s));
if(s == NULL)
return NULL;
s->size = 0;
s->top = NULL;
return s;
}
// 判断栈是否为空
// Is_Empty
bool Is_Empty(struct LinkedStack *s)
{
if(s == NULL)
return NULL;
return s->size == 0 && s->top == NULL ? true : false;
}
//清空
bool Clear_Stack(struct LinkedStack *s)
{
if(s == NULL)
return false;
if(Is_Empty(s))
return true;
struct Node *p = s->top;
while(p)
{
s->top = s->top->next;
p->next = NULL;
free(p);
p = s->top;
}
s->size = 0;
s->top = NULL;
return true;
}
//销毁栈
bool Destroy_Stack(struct LinkedStack *s)
{
if(!Clear_Stack(s))
return false;
free(s);
s = NULL;
return true;
}
// 获取栈的长度:元素个数
// Get_Length
int Get_Length(struct LinkedStack *s)
{
if(s == NULL)
return -1;
return s->size;
}
// 出栈
// Pop
bool Pop(struct LinkedStack *s,DataType *d)
{
if(s == NULL || Is_Empty(s))
return false;
struct Node *p = s->top;
if(d)
*d = s->top->data;
s->top = s->top->next;
p->next = NULL;
free(p);
p = NULL;
s->size--;
return true;
}// 入栈
// Push
bool Push(struct LinkedStack *s,DataType d)
{
if(s == NULL)
return false;
struct Node *pnew = malloc(sizeof(*pnew));
if(pnew == NULL)
return s;
pnew->data = d;
pnew->next = NULL;if(s->size == 0 && s->top == NULL)
{
s->top = pnew;
}
else
{
pnew->next = s->top;
s->top = pnew;
}
s->size++;
return true;
}
// 获取栈顶元素,但不出栈
// Get_Top
bool Get_Top(struct LinkedStack *s,DataType *d)
{
if(s == NULL || Is_Empty(s))
return false;
if(d)
*d = s->top->data;
return true;
}//十进制转八进制
void Dec_to_Oct(int num)
{
struct LinkedStack *s = CreateStack();
if(s == NULL)
return ;
while(num)
{
Push(s,num % 8);
num /= 8;
}
DataType d;
while (!Is_Empty(s))
{
Pop(s,&d);
printf("%d",d);
}
printf("\n");
Destroy_Stack(s);
}
队列(先进先出)
1、顺序队
(头文件)
#ifndef __SEQQUEUE_H__
#define __SEQQUEUE_H__#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>typedef char DataType;
struct SeqQueue
{
DataType *data;
int front;
int rear;
int capacity;
};extern struct SeqQueue*InitQueue(int num);
extern bool DestroyQueue(struct SeqQueue *q);
extern bool ClearQueue(struct SeqQueue *q);
extern bool QueueIsEmpty(struct SeqQueue *q);
extern bool QueueIsFull(struct SeqQueue *q);
extern int QueueLength(struct SeqQueue *q);
extern bool EnQueue(struct SeqQueue *q, DataType d);
extern bool DeQueue(struct SeqQueue *q, DataType *d);
extern bool GetHead(struct SeqQueue *q, DataType *d);
extern void ShowQueue(struct SeqQueue *q);
#endif
(函数文件)
#include "SeqQueue.h"
// InitQueue: 分配并初始化一个队列:创建一个空顺序队列
struct SeqQueue *InitQueue(int num)
{
struct SeqQueue *q = malloc(sizeof(*q));
if (q == NULL)
return NULL;
q->data = calloc(num, sizeof(DataType));
if (q->data == NULL)
{
free(q);
return NULL;
}
q->front = q->rear = 0;
q->capacity = num;
return q;
}// DestroyQueue: 销毁一个队列
bool DestroyQueue(struct SeqQueue *q)
{
if (q == NULL)
return false;
if (q->data)
{
free(q->data);
q->data = NULL;
}
free(q);
q = NULL;
return true;
}// ClearQueue: 清空队列 -> 变成一个空队列
bool ClearQueue(struct SeqQueue *q)
{
if (q == NULL)
return false;
q->front = q->rear = 0;
return true;
}// QueueIsEmpty: 判断一个队列是否为空
bool QueueIsEmpty(struct SeqQueue *q)
{
if (q == NULL)
return false;
return q->rear == q->front ? true : false;
}// QueueIsFull: 判断一个队列是否为满
bool QueueIsFull(struct SeqQueue *q)
{
if(q == NULL)
return false;
return (q->rear+1)%q->capacity == q->front ? true :false;
}
// QueueLength: 返回队列中元素的个数,即队列的长度
int QueueLength(struct SeqQueue *q)
{
if(q == NULL)
return -1;
if(q->front <= q->rear)
return q->rear - q->front;
else
return q->capacity-(q->front-q->rear);
}// EnQueue: 入队
bool EnQueue(struct SeqQueue *q, DataType d)
{
if(q == NULL || QueueIsFull(q))
return false;
q->data[q->rear] = d;
q->rear = (q->rear+1)%q->capacity;
return true;
}// DeQueue: 出队
bool DeQueue(struct SeqQueue *q, DataType *d)
{
if(q == NULL || QueueIsEmpty(q))
return false;
*d = q->data[q->front];
q->front = (q->front+1)%q->capacity;
return true;
}// GetHead: 获取队头的元素,但不出队。
bool GetHead(struct SeqQueue *q, DataType *d)
{
if(q == NULL || QueueIsEmpty(q))
return false;
*d = q->data[q->front];
return true;
}//显示队中元素
void ShowQueue(struct SeqQueue *q)
{
if(q == NULL)
{
printf("===================\n");
return;
}
if(QueueIsEmpty(q))
{
printf("Queue is NULL\n");
return;
}
else
{
int i = 0;
while(i < QueueLength(q))
{
printf("%c ",q->data[(q->front+i)%q->capacity]);
i++;
}
printf("\n");
}
}
2、链式队
(头文件)
#ifndef __LINKEDQUEUE_H__
#define __LINKEDQUEUE_H__
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef char DataType;struct Node
{
DataType data;
struct Node *next;
};struct LinkedQueue
{
int size;
struct Node *front;
struct Node *rear;
};
extern struct LinkedQueue *InitQueue();
extern bool ClearQueue(struct LinkedQueue*q);
extern bool DestroyQueue(struct LinkedQueue*q);
extern bool QueueIsEmpty(struct LinkedQueue*q);
extern int QueueLength(struct LinkedQueue*q);
extern bool EnQueue(struct LinkedQueue*q, DataType d);
extern bool DeQueue(struct LinkedQueue*q, DataType *d);
extern bool GetHead(struct LinkedQueue*q, DataType *d);
extern void ShowQueue(struct LinkedQueue*q);
extern void fun();
#endif
(函数文件)
#include "LinkedQueue.h"
// InitQueue: 分配并初始化一个队列:创建一个空顺序队列
struct LinkedQueue *InitQueue()
{
struct LinkedQueue *q = malloc(sizeof(*q));
if(q == NULL)
return NULL;
q->front = q->rear = NULL;
q->size = 0;
return q;
}// ClearQueue: 清空队列 -> 变成一个空队列
bool ClearQueue(struct LinkedQueue*q)
{
if(q == NULL)
return false;
struct Node *p = q->front;
while (p)
{
q->front = q->front->next;
p->next = NULL;
free(p);
p = q->front;
}
q->rear = NULL;
q->size = 0;
return true;
}
// DestroyQueue: 销毁一个队列
bool DestroyQueue(struct LinkedQueue*q)
{
if(q == NULL)
return false;
ClearQueue(q);
free(q);
q = NULL;
return true;
}
// QueueIsEmpty: 判断一个队列是否为空
bool QueueIsEmpty(struct LinkedQueue*q)
{
if(q == NULL)
return NULL;
return q->front == NULL && q->rear == NULL && q->size == 0 ? true : false;
}// QueueLength: 返回队列中元素的个数,即队列的长度
int QueueLength(struct LinkedQueue*q)
{
if(q == NULL)
return -1;
return q->size;
}// EnQueue: 入队
bool EnQueue(struct LinkedQueue*q, DataType d)
{
if(q == NULL)
return false;
struct Node *pnew = malloc(sizeof(*pnew));
if(pnew == NULL)
return false;
pnew->data = d;
pnew->next = NULL;if(q->front == NULL && q->rear == NULL && q->size == 0)
q->front = q->rear =pnew;
else
{
q->rear->next = pnew;
q->rear = pnew;
}
q->size++;
return true;
}// DeQueue: 出队
bool DeQueue(struct LinkedQueue*q, DataType *d)
{
if(q == NULL || QueueIsEmpty(q))
return false;
struct Node *p = q->front;
if(d)
*d = q->front->data;
q->front = q->front->next;
if(q->front == NULL)
q->rear = NULL;
p->next = NULL;
free(p);
p = NULL;
q->size--;
return true;
}// GetHead: 获取队头的元素,但不出队。
bool GetHead(struct LinkedQueue*q, DataType *d)
{
if(q == NULL || QueueIsEmpty(q))
return false;
struct Node *p = q->front;
if(d)
*d = q->front->data;
return true;
}//显示队中元素
void ShowQueue(struct LinkedQueue *q)
{
if(q == NULL)
{
printf("=================\n");
return ;
}
if(QueueIsEmpty(q))
{
printf("Queue is NULL\n");
return ;
}
else
{
struct Node *p = q->front;
while (p)
{
printf("%c ",p->data);
p = p->next;
}
printf("\n");
}
}//构建一个链式队列,当用户输入数字时,将数字入队,当用户输入字母时,将队头元素出队。每次操作队列之后,将队列中的元素显示出来。
void fun()
{
struct LinkedQueue *q = InitQueue();
char c;
while (1)
{
scanf("%c",&c);
if(c == '#')
break;
if(c >='0' && c <='9')
{
EnQueue(q,c);
ShowQueue(q);
}
else
{
DataType d;
DeQueue(q,&d);
ShowQueue(q);
}
}
}
Leetcode - 2、两数相加
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头
示例 1:
输入:l1 = [2,4,3], l2 = [5,6,4] 输出:[7,0,8] 解释:342 + 465 = 807.
示例 2:
输入:l1 = [0], l2 = [0] 输出:[0]
示例 3:
输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] 输出:[8,9,9,9,0,0,0,1]
提示:
- 每个链表中的节点数在范围
[1, 100]
内 0 <= Node.val <= 9
- 题目数据保证列表表示的数字不含前导零
思路:建立新的链表存储每位相加的结果,用col存储相加后的进位,题目是逆序存储数据,所以每次的结果采用尾插法就行了
注意:在每次数据遍历结束时,要考虑col是否为0,如果不为0表示相加后的结果进位了,要把col单独插入新的链表。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode *l3=NULL;
struct ListNode *last=NULL;
int col=0;
//尾插法
while(l1||l2)
{
struct ListNode *p=malloc(sizeof(*p));
if(p==NULL)
return NULL;
if(l1==NULL)
{
p->val=(l2->val+col)%10;
col=(l2->val+col)/10;
}
else if(l2==NULL)
{
p->val=(l1->val+col)%10;
col=(l1->val+col)/10;
}
else
{
p->val=(l1->val+l2->val+col)%10;
col=(l1->val+l2->val+col)/10;
}
p->next=NULL;
if(l3==NULL&&last==NULL)
l3=last=p;
else
{
last->next=p;
last=p;
}
if(l1&&l2)
{
l1=l1->next;
l2=l2->next;
}
else if(l1==NULL)
{
l2=l2->next;
}
else if(l2==NULL)
{
l1=l1->next;
}
}
if(col)
{
struct ListNode *p=malloc(sizeof(*p));
if(p==NULL)
return NULL;
p->val=col;
p->next=NULL;
if(l3==NULL&&last==NULL)
{
l3=last=p;
}
else
{
last->next=p;
last=p;
}
}
return l3;
}