目录
第一章绪论
1、基本概念和术语
数据 | 一切可以输入到计算机中的如文本型、数值型、图像、表、树、图 |
数据元素 | 数据元素是构成数据的基本单位,在计算机程序中通常作为一个整体进行考虑和处理。 |
数据项 | 数据元素可分为诺干个数据项,数据项是具有独立单位的最小单元。 |
数据对象 | 是具有相同性子的数据元素集合,是数据的子集,如{1、2、3...}或者字母型{A,B,C...} |
数据结构 | 数据结构是相互间存在着1种或多种特定关系的数据元 素的集合,是带有结构的数据元素的集合,它指的是数据 元素之间的相互关系,即数据的组织形式。 |
单链表
单链表
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LinkNode;
void CreatList(LinkNode *&L,ElemType a[],int n){//头插法
LinkNode *s;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
for(int i=0;i<n;i++){
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=a[i];
s->next=L->next;
L->next=s;
}
}
void CreatListF(LinkNode *&L,ElemType a[],int n){//尾插法
LinkNode *s,*r;
L=(LinkNode *)malloc(sizeof(LinkNode));
r=L;//r先指向头节点
for(int i=0;i<n;i++){
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=a[i];//赋值
r->next=s;//
r=s;//r变回尾指针
}
}
void DispList(LinkNode *L){
LinkNode *p=L->next;
printf("当前链表:");
while(p!=NULL){
printf("%d、",p->data);
p=p->next;
}
printf("\n");
}
int ListLength(LinkNode *L){
int n=0;
LinkNode *p=L->next;
while(p!=NULL){
n++;
p=p->next;
}
return n;
}
bool ListInsert(LinkNode *&L,int i,ElemType e){
int j=0;
LinkNode *p=L,*s;
if(i<=0) return false;
while(j<i-1&&p!=NULL){
j++;
p=p->next;
}
if(p==NULL)//插入位置前面一个节点为空,
return false;
else {
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
}
bool ListDelet(LinkNode *&L,int i,ElemType &e){
int j=0;
LinkNode *p=L,*q;
if(i<=0) return false;
while(j<i-1&&p!=NULL){
j++;
p=p->next;
}
if(p==NULL) return false;
else{
q=p->next;//让q为第i个节点
if(q==NULL)//
return false;
e=q->data;
p->next=q->next;//删除第i(q)个节点
free(q);//释放内存
return true;
}
}
void ListInsertF( LinkNode *&L){
int i;
char n;
ElemType e;
printf("输入插入节点的位置i和元素e(i.2)");
scanf("%d.%d",&i,&e);
ListInsert(L,i,e);
n=getchar();
DispList(L);
}
void ListDeletF( LinkNode *&L){
int i;
ElemType e;
DispList(L);
printf("输入要删除的节点");
scanf("%d",&i);
ListDelet(L,i,e);
DispList(L);
}
int main(){
int number;
ElemType e;
ElemType i;
ElemType a[10];
LinkNode *L;
L=(LinkNode *)malloc(sizeof(LinkNode));//链表初始化;
L->next=NULL;
printf("请输入10个整数:");
for(int n=0;n<10;n++){
scanf("%d",&a[n]);
}
CreatList(L,a,10);
//printf("表长:%d\n",ListLength(L));
printf("创建链表完成链表为:");
DispList(L);
//prntf("输入插入节点的位置、元素(整形)");
do{
printf("输入以下数字对应功能\n1........打印当前链表。\n2........插入节点.\n3........删除节点。\n4........输入当前链表的长度。\n0........退出\n");
scanf("%d",&number);
switch(number){
case 1: DispList(L);break;
case 2: ListInsertF(L);break;
case 4: printf("表长:%d\n",ListLength(L));break;
case 3: ListDeletF(L);break;
default:printf("Over");break;
}
}while(number>0);
DispList(L);
}
顺序栈
#include<stdio.h>
#include<malloc.h>
#define MaxSize 10
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int top;
}SqStack;
int StackLength(SqStack *s){
if(s->top<0)
return 0;
else
return s->top++;
}
bool StackEmpty(SqStack *s){
if(s->top==-1)
return true;
else
return false;
}
bool PushStack(SqStack *&s,ElemType e){
if(s->top==MaxSize-1){
printf("栈已上溢");
return false;
}
else{
s->top++;
s->data[s->top]=e;
return true;
}
}
bool PopStack(SqStack *&s,ElemType &e){
if(s->top==-1){
printf("栈已经为空");
return false;
}
else{
e=s->data[s->top];
s->top--;
return true;
}
}
void GetPopStack(SqStack *s,ElemType &e){
if(s->top==-1)
printf("当前栈为空");
else{
e=s->data[s->top];
}
}
void GetPopStackF(SqStack *s){
ElemType e;
GetPopStack(s,e);
printf("栈顶元素为:%d\n",e);
}
void PushStackF(SqStack *s){
ElemType e;
printf("请输入要入栈的元素:");
scanf("%d",&e);
PushStack(s,e);
printf("%d如栈成功\n",e);
}
void PopStackF(SqStack *s){
ElemType e;
PopStack(s,e);
printf("出栈元素为:%d\n",e);
}
void DisStack(SqStack *s){
printf("当前栈为:");
for(int i=0;i<=s->top;i++)
printf("%d、",s->data[i]);
printf("\n");
}
int main(){
ElemType e;
SqStack *s;
int n;
s=(SqStack *)malloc(MaxSize*sizeof(SqStack));//初始化栈
s->top=-1;
printf("初始化栈成功");
printf("栈的长度为:%d\n",StackLength(s));
printf("输入以下数字对应功能\n");
do{
printf("1--------当前栈的长度\n2--------入栈\n3--------出栈\n4--------打印当前栈\n5--------取栈顶元素\n0--------退出\n");
scanf("%d",&n);
switch(n){
case 1:printf("当前栈长度为:%d\n",StackLength(s));break;
case 2:PushStackF(s);break;
case 3:PopStackF(s);break;
case 4:DisStack(s);break;
case 5:GetPopStackF(s);break;
default:printf("over");break;
}
}while(n>0);
return 0;
}
链栈
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef struct linknode{
ElemType data;
struct linknode * next;
}StackNode;
bool StackEmpty(StackNode *L){
return L->next==NULL;
}
void DisStack(StackNode *L){
StackNode *p;
p=(StackNode *)malloc(sizeof(StackNode));
p=L->next;
printf("当前栈为:");
while(p!=NULL){
printf("%d、",p->data);
p=p->next;
}
printf("\n");
}
void Push(StackNode *&L,ElemType e){
StackNode *p;
p=(StackNode *)malloc(sizeof(StackNode));
p->data=e;
p->next=L->next;
L->next=p;
}
bool Pop(StackNode *L,ElemType &e){
StackNode *p;
if(L->next==NULL)
return 0;
else{
p=L->next;
e=p->data;
L->next=p->next;//删除p节点
free(p);
return true;
}
}
bool GetTop(StackNode *L,ElemType &e){
if(L->next==NULL)
return false;
else{
e=L->next->data;
return true;
}
}
int main(){
ElemType e;
int n;
StackNode *L;
L=(StackNode*)malloc(sizeof(StackNode));
L->next=NULL;
printf("进栈:");
scanf("%d",&e);
Push(L,e);
DisStack(L);
Pop(L,e);
printf("出栈元素为:%d\n",e);
DisStack(L);
return 0;
}
顺序队列
#include<stdio.h>
#include<malloc.h>
#define MAXSIZE 6
typedef int ElemType;
typedef struct {
ElemType data[6];
int front;
int rear;
}SqQueue;
void DestroyQueue(SqQueue *&q){
free(q);
}
bool QueueEmpty(SqQueue *q){
return (q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e){
if((q->rear+1%)MAXSIZE==q->front){
printf("入队失败,队列已满");
return false;
}
else{
q->data[q->rear]=e;
q->rear++;
return true;
}
}
bool deQueue(SqQueue *&q,ElemType &e){
if(q->rear==q->front){
printf("队列为空");
return false;
}
else{
e=q->data[q->front];
q->front++;
return true;
}
}
void DisQueue(SqQueue *q){
if(q->front==q->rear)
printf("为空栈");
else{
printf("当前队列为:");
for(int j=q->front;j!=q->rear%MAXSIZE;j++)
printf("%d、",q->data[j]);
}
printf("\n");
}
int main(){
ElemType e;
SqQueue *q;
q=(SqQueue *)malloc(MAXSIZE*sizeof(SqQueue));
q->front=q->rear=0;
enQueue(q,1);
enQueue(q,2);
DisQueue(q);
deQueue(q,e);
printf("出队%d\n",e);
DisQueue(q);
return 0;
}
链队
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef struct qnode{//声明结构体;
ElemType data;
struct qnode *next;
}qnode,*DataNode;//链队数据结构的类型;;;结构体变量
//typedef qnode *DataNode;
typedef struct {
DataNode front;
DataNode rear;
}LinkQueue;//链队的类型
bool QueueEmpty(LinkQueue *q){
return (q->front==NULL);
}
void enQueue(LinkQueue *&p,ElemType e){
DataNode s;
s=(DataNode )malloc(sizeof(qnode));
s->data=e;
s->next=NULL;
if(p->rear==NULL){
p->front=p->rear=s;
}
else{
p->rear->next=s;
p->rear=s;
}
}
bool deQueue(LinkQueue *&p,ElemType &e){
DataNode s;
s=(DataNode )malloc(sizeof(qnode));
if(p->front==NULL){
printf("已经为空队列");
return false;
}
else{
e=p->front->data;
s=p->front;
p->front=p->front->next;
free(s);
return true;
}
}
void DisQueue(LinkQueue *p){
DataNode s;
s=(DataNode )malloc(sizeof(qnode));
s=p->front;
s->next=p->front->next;
printf("当前队列为:");
while(s!=NULL){
printf("%d、",s->data);
s=s->next;
}
printf("\n");
}
int main(){
LinkQueue *p;
ElemType e;
//char a=getchar();
p=(LinkQueue *)malloc(sizeof(LinkQueue));
//p->front=p->rear=(DataNode)malloc(sizeof(qnode));
p->rear=p->front=NULL;
//char a=getchar();
enQueue(p,2);
enQueue(p,3);
enQueue(p,4);
enQueue(p,0);
enQueue(p,222222);
enQueue(p,29999999);
deQueue(p,e);
printf("出队的元素为:%d",e);
DisQueue(p);
return 0;
}