数据结构-第二讲 线性结构-学习笔记(MOOC 浙江大学 陈越 何钦铭)


视频网址

https://www.icourse163.org/course/ZJU-93001#/info

https://www.bilibili.com/video/BV1H4411N7oD?p=12



第二讲 线性结构



2.1 线性表及其实现



2.1.1 引子:多项式表示

f ( x ) = a 0 + a 1 x + . . . + a n − 1 x n − 1 + a n x n f(x) = a_{0} + a_{1}x + ... + a_{n-1}x^{n-1} + a_{n}x^n f(x)=a0+a1x+...+an1xn1+anxn
一元多项式
主要运算:多项式相加、相减、相乘等

方法1:顺序存储结构直接表示
数组

方法2:顺序存储结构表示非零项
结构体数组

方法3:链表结构存储非零项

typedef struct PolyNode *Polynomial;
struct PolyNode {
	int coef;
	int expon;
	Polynomial link;
}


2.1.2 线性表及顺序存储


“线性表(Linear List)”:由同类型数据元素构成有序序列的线性结构

  • 表中元素个数称为线性表的长度
  • 线性表没有元素时,称为空表
  • 表起始位置称表头,表结束位置称表尾

线性表的抽象数据类型描述

类型名称:线性表(List)
数据对象集:线性表是 n (0)个元素构成的有序序列( a1, a2, ... ,an)
操作集:线性表L ∈ List,整数i表示位置,元素X ∈ ElementType,
 线性表基本操作主要有:
 
1、List MakeEmpty():初始化一个空线性表L;
2、ElementType FindKth( int K, List L ):根据位序K,返回相应元素 ;
3int Find( ElementType X, List L ):在线性表L中查找X的第一次出现位置;
4void Insert( ElementType X, int i, List L):在位序i前插入一个新元素X;
5void Delete( int i, List L ):删除指定位序i的元素;
6int Length( List L ):返回线性表L的长度n。

线性表的顺序存储实现

利用数组的连续存储空间顺序存放线性表的各元素

typedef struct LNode *List;
struct LNode{
	ElementType Data[MAXSIZE];
 	int Last;// 数组中最末一个有效元素的位置
} ;
struct LNode L;
List PtrL;

访问下标为 i 的元素:L.Data[i] 或 PtrL->Data[i]
线性表的长度:L.Last+1 或 PtrL->Last+1

初始化(建立空的顺序表)

List MakeEmpty( )
{ 
	List PtrL;
 	PtrL = (List )malloc( sizeof(struct LNode) );
 	PtrL->Last = -1;
 	return PtrL;
}

查找

int Find( ElementType X, List PtrL )
{ 
	int i = 0;
 	while( i <= PtrL->Last && PtrL->Data[i]!= X )
 		i++;
 	if (i > PtrL->Last) 
 		return -1; /* 如果没找到,返回-1 */
 	else 
 		return i; /* 找到后返回的是存储位置 */
} 

查找成功的平均比较次数为(n +1)/2,平均时间性能为O(n)。



2.1.3 顺序存储的插入和删除


插入(第 i (1≤i≤n+1)个位置上插入一个值为X的新元素)

由于存储在数组中(下标从0开始),所以插到第i个位置上,其实是插到下标为i-1的位置上
先把array[i-1]…array[n]后移为array[i]…array[n+1]
然后array[i-1]=x 并把 最后一个有效下标标志数+1

插入操作实现

void Insert( ElementType X, int i, List PtrL )
{ 
	int j;
 	if ( PtrL->Last == MAXSIZE-1 ){ /* 表空间已满,不能插入*/
 		printf("表满");
 		return;
 	}
 	if ( i < 1 || i > PtrL->Last+2) { /*检查插入位置的合法性*/
 		printf("位置不合法");
 		return;
 	}
 	for ( j = PtrL->Last; j >= i-1; j-- )
 		PtrL->Data[j+1] = PtrL->Data[j]; /*将 ai~ an倒序向后移动*/
 	PtrL->Data[i-1] = X; /*新元素插入*/
	PtrL->Last++; /*Last仍指向最后元素*/
 	return;
} 

平均移动次数为 n /2,平均时间性能为 O(n)


删除(删除表的第 i (1≤i≤n)个位置上的元素)

删除第i个位置上的元素,也就是删除下标为i-1的元素

void Delete( int i, List PtrL )
{ 
	int j;
 	if( i < 1 || i > PtrL->Last+1 ) { /*检查空表及删除位置的合法性*/
 		printf (“不存在第%d个元素”, i );
 		return ;
 	}
 	for ( j = i; j <= PtrL->Last; j++ )
 		PtrL->Data[j-1] = PtrL->Data[j]; /*将 ai+1~ an顺序向前移动*/
 	PtrL->Last--; /*Last仍指向最后元素*/
 	return;
}

平均移动次数为 (n-1) /2,平均时间性能为 O(n)


顺序存储-数组实现-code
typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last;
};

/* 初始化 */
List MakeEmpty()
{
    List L;

    L = (List)malloc(sizeof(struct LNode));
    L->Last = -1;

    return L;
}

/* 查找 */
#define ERROR -1

Position Find( List L, ElementType X )
{
    Position i = 0;

    while( i <= L->Last && L->Data[i]!= X )
        i++;
    if ( i > L->Last )  return ERROR; /* 如果没找到,返回错误信息 */
    else  return i;  /* 找到后返回的是存储位置 */
}

/* 插入 */
/*注意:在插入位置参数P上与课程视频有所不同,课程视频中i是序列位序(从1开始),这里P是存储下标位置(从0开始),两者差1*/
bool Insert( List L, ElementType X, Position P ) 
{ /* 在L的指定位置P前插入一个新元素X */
    Position i;

    if ( L->Last == MAXSIZE-1) {
        /* 表空间已满,不能插入 */
        printf("表满"); 
        return false; 
    }  
    if ( P<0 || P>L->Last+1 ) { /* 检查插入位置的合法性 */
        printf("位置不合法");
        return false; 
    } 
    for( i=L->Last; i>=P; i-- )
        L->Data[i+1] = L->Data[i]; /* 将位置P及以后的元素顺序向后移动 */
    L->Data[P] = X;  /* 新元素插入 */
    L->Last++;       /* Last仍指向最后元素 */
    return true; 
} 

/* 删除 */
/*注意:在删除位置参数P上与课程视频有所不同,课程视频中i是序列位序(从1开始),这里P是存储下标位置(从0开始),两者差1*/
bool Delete( List L, Position P )
{ /* 从L中删除指定位置P的元素 */
    Position i;

    if( P<0 || P>L->Last ) { /* 检查空表及删除位置的合法性 */
        printf("位置%d不存在元素", P ); 
        return false; 
    }
    for( i=P+1; i<=L->Last; i++ )
        L->Data[i-1] = L->Data[i]; /* 将位置P+1及以后的元素顺序向前移动 */
    L->Last--; /* Last仍指向最后元素 */
    return true;   
}


2.1.4 链式存储及查找


线性表的链式存储实现

不要求逻辑上相邻的两个元素物理上也相邻;通过“链”建立起数据元素之间的逻辑关系。

插入、删除不需要移动数据元素,只需要修改“链”。

typedef struct LNode *List;
struct LNode{
 	ElementType Data;
 	List Next;
};
struct Lnode L;
List PtrL;

求表长

int Length ( List PtrL )
{ 
	List p = PtrL; /* p指向表的第一个结点*/
 	int j = 0;
 	while ( p ) {
 		p = p->Next;
 		j++; /* 当前p指向的是第 j 个结点*/
 	}
 	return j;
}

时间性能为 O(n)。


(1)按序号查找: FindKth;

List FindKth( int K, List PtrL )
{ 
	List p = PtrL;
 	int i = 1;
 	while (p !=NULL && i < K ){
 		p = p->Next;
 		i++;
 	}
 	if ( i == K ) 
 		return p; 	/* 找到第K个,返回指针 */
 	else 
 		return NULL; /* 否则返回空 */
}

(2)按值查找: Find

List Find( ElementType X, List PtrL )
{
 	List p = PtrL;
	while ( p!=NULL && p->Data != X )
 		p = p->Next;
 	return p;
} 

平均时间性能为 O(n)



2.1.5 链式存储的插入和删除


插入(在第 i-1(1≤i≤n+1)个结点后插入一个值为X的新结点)

(1)先构造一个新结点,用s指向;
(2)再找到链表的第 i-1个结点,用p指向;
(3)然后修改指针,插入结点 ( p之后插入新结点是 s)
在这里插入图片描述
插入操作实现

List Insert( ElementType X, int i, List PtrL )
{ 
	List p, s;
 	if ( i == 1 ) { /* 新结点插入在表头 */
 		s = (List)malloc(sizeof(struct LNode)); /*申请、填装结点*/
 		s->Data = X;
 		s->Next = PtrL;
 		return s; /*返回新表头指针*/
 	}
 	p = FindKth( i-1, PtrL ); /* 查找第i-1个结点 */
 	if ( p == NULL ) { /* 第i-1个不存在,不能插入 */
 		printf("参数i错");
 		return NULL;
 	}else {
 		s = (List)malloc(sizeof(struct LNode)); /*申请、填装结点*/
 		s->Data = X;
 		s->Next = p->Next; /*新结点插入在第i-1个结点的后面*/
 		p->Next = s;
 		return PtrL;
 	}
}


PtrL = Insert(...)

平均查找次数为 n /2,平均时间性能为 O(n)


删除(删除链表的第 i (1≤i≤n)个位置上的结点)

(1)先找到链表的第 i-1个结点,用p指向;
(2)再用指针s指向要被删除的结点(p的下一个结点);
(3)然后修改指针,删除s所指结点;
(4)最后释放s所指结点的空间。
在这里插入图片描述
删除操作实现

List Delete( int i, List PtrL )
{ 
	List p, s;
 	if ( i == 1 ) { /* 若要删除的是表的第一个结点 */
 		s = PtrL; /*s指向第1个结点*/
 		if (PtrL!=NULL) 
 			PtrL = PtrL->Next; /*从链表中删除*/
 		else 
 			return NULL;
 		free(s); /*释放被删除结点 */
 		return PtrL;
 	}
 	
 	p = FindKth( i-1, PtrL ); /*查找第i-1个结点*/
 	if ( p == NULL ) {
 		printf(“第%d个结点不存在”, i-1); 
 		return NULL;
 	} else if ( p->Next == NULL ){
 		printf(“第%d个结点不存在”, i); 
 		return NULL;
 	} else {
 		s = p->Next; /*s指向第i个结点*/
 		p->Next = s->Next; /*从链表中删除*/
 		free(s); /*释放被删除结点 */
 		return PtrL;
 	}

平均查找次数为 n /2,平均时间性能为 O(n)



2.1.6 广义表与多重链表


在这里插入图片描述


广义表(Generalized List)

  1. 广义表是线性表的推广
  2. 对于线性表而言, n个元素都是基本的单元素
  3. 广义表中,这些元素不仅可以是单元素也可以是另一个广义表
typedef struct GNode *GList;
struct GNode{
 	int Tag; /*标志域:0表示结点是单元素,1表示结点是广义表 */
 	union { /*子表指针域Sublist与单元素数据域Data复用,即共用存储空间*/
 		ElementType Data;
 		GList SubList;
 	} URegion;
 	GList Next; /* 指向后继结点 */
};


多重链表

多重链表:链表中的节点可能同时隶属于多个链

  1. 多重链表中结点的指针域会有多个,如前面例子包含了Next和SubList两个指针域;
  2. 但包含两个指针域的链表并不一定是多重链表,比如在双向链表不是多重链表

多重链表有广泛的用途:基本上如树、图这样相对复杂的数据结构都可以采用多重链表方式实现存储。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

顺序存储-链表实现-code
typedef struct LNode *PtrToLNode;
struct LNode {
    ElementType Data;
    PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;

/* 查找 */
#define ERROR NULL

Position Find( List L, ElementType X )
{
    Position p = L; /* p指向L的第1个结点 */

    while ( p && p->Data!=X )
        p = p->Next;

    /* 下列语句可以用 return p; 替换 */
    if ( p )
        return p;
    else
        return ERROR;
}

/* 带头结点的插入 */
/*注意:在插入位置参数P上与课程视频有所不同,课程视频中i是序列位序(从1开始),这里P是链表结点指针,在P之前插入新结点 */
bool Insert( List L, ElementType X, Position P )
{ /* 这里默认L有头结点 */
    Position tmp, pre;

    /* 查找P的前一个结点 */        
    for ( pre=L; pre&&pre->Next!=P; pre=pre->Next ) ;            
    if ( pre==NULL ) { /* P所指的结点不在L中 */
        printf("插入位置参数错误\n");
        return false;
    }
    else { /* 找到了P的前一个结点pre */
        /* 在P前插入新结点 */
        tmp = (Position)malloc(sizeof(struct LNode)); /* 申请、填装结点 */
        tmp->Data = X; 
        tmp->Next = P;
        pre->Next = tmp;
        return true;
    }
}

/* 带头结点的删除 */
/*注意:在删除位置参数P上与课程视频有所不同,课程视频中i是序列位序(从1开始),这里P是拟删除结点指针 */
bool Delete( List L, Position P )
{ /* 这里默认L有头结点 */
    Position pre;

    /* 查找P的前一个结点 */        
    for ( pre=L; pre&&pre->Next!=P; pre=pre->Next ) ;            
    if ( pre==NULL || P==NULL) { /* P所指的结点不在L中 */
        printf("删除位置参数错误\n");
        return false;
    }
    else { /* 找到了P的前一个结点pre */
        /* 将P位置的结点删除 */
        pre->Next = P->Next;
        free(P);
        return true;
    }
}


2.2 堆栈



2.2.1 什么是堆栈


什么是堆栈
计算机如何进行表达式求值?后缀表达式

前缀表达式:运算符号位于运算数之前。比如 a+b*c的前缀表达式是+a*bc;a+b*c-d/e的前缀表达式是 -+a*bc/de
中缀表达式:运算符号位于两个运算数之间。如 ,a + b * c - d / e
后缀表达式:运算符号位于两个运算数之后。如, a b c * + d e / -

后缀表达式求值策略:从左向右“扫描”,逐个处理运算数和运算符号

  1. 遇到运算数怎么办?如何“记住”目前还不未参与运算的数?
  2. 遇到运算符号怎么办?对应的运算数是什么?

启示:需要有种存储方法,能顺序存储运算数,并在需要时“倒序”输出!堆栈!
T ( N ) = O ( N ) T( N ) = O ( N ) T(N)=O(N)


堆栈的抽象数据类型描述

堆栈(Stack):具有一定操作约束的线性表
只在一端(栈顶,Top)做 插入、删除

插入数据:入栈(Push)
删除数据:出栈(Pop)
后入先出:Last In First Out(LIFO)

类型名称: 堆栈(Stack)  
数据对象集:一个有0个或多个元素的有穷线性表。
操作集:长度为MaxSize的堆栈S ∈ Stack,堆栈元素item ∈ ElementType

1、Stack CreateStack( int MaxSize ): 生成空堆栈,其最大长度为MaxSize;
2int IsFull( Stack S, int MaxSize ):判断堆栈S是否已满;
3void Push( Stack S, ElementType item ):将元素item压入堆栈;
4int IsEmpty ( Stack S ):判断堆栈S是否为空;
5、ElementType Pop( Stack S ):删除并返回栈顶元素;

在这里插入图片描述



2.2.2 堆栈的顺序存储实现(数组实现)


栈的顺序存储实现

栈的顺序存储结构通常由一个一维数组和一个记录栈顶元素位置的变量组成。

#define MaxSize <储存数据元素的最大个数>
typedef struct SNode *Stack;
struct SNode{
	ElementType Data[MaxSize];
	int Top;
};

入栈

void Push( Stack PtrS, ElementType item )
{
 	if ( PtrS->Top == MaxSize-1 ) {
 		printf(“堆栈满”); return;
 	}else {
 		PtrS->Data[++(PtrS->Top)] = item;//(PtrS->Top)++; PtrS->Data[PtrS->Top] = item;
 		return;
 	}
}

出栈

ElementType Pop( Stack PtrS )
{
 	if ( PtrS->Top == -1 ) {
 		printf(“堆栈空”);
 		return ERROR; /* ERROR是ElementType的特殊值,标志错误*/
 	} else
 		return ( PtrS->Data[(PtrS->Top)--] );
}

[例] 请用一个数组实现两个堆栈,要求最大地利用数组空间,使数组只要有空间入栈操作就可以成功。
【分析】 一种比较聪明的方法是使这两个栈分别从数组的两头开始向中间生长;当两个栈的栈顶指针相遇时,表示两个栈都满了。

#define MaxSize <存储数据元素的最大个数>
struct DStack {
 	ElementType Data[MaxSize];
 	int Top1; /* 堆栈1的栈顶指针 */
 	int Top2; /* 堆栈2的栈顶指针 */
} S;
S.Top1 = -1;		/* 堆栈1空 */
S.Top2 = MaxSize;	/* 堆栈2 空 */
void Push( struct DStack *PtrS, ElementType item, int Tag )/* Tag作为区分两个堆栈的标志,取值为1和2 */
{ 
 	if ( PtrS->Top2 – PtrS->Top1 == 1) { /*堆栈满*/
		printf(“堆栈满”); return ;
 	}
 	
 	if ( Tag == 1 ) /* 对第一个堆栈操作 */
 		PtrS->Data[++(PtrS->Top1)] = item;
 	else /* 对第二个堆栈操作 */
 		PtrS->Data[--(PtrS->Top2)] = item;
}

ElementType Pop( struct DStack *PtrS, int Tag )/* Tag作为区分两个堆栈的标志,取值为1和2 */
{ 
 	if ( Tag == 1 ) { /* 对第一个堆栈操作 */
 		if ( PtrS->Top1 == -1 ) { /*堆栈1空 */
 			printf(“堆栈1空”); 
 			return NULL;
 		} 
 		else 
 			return PtrS->Data[(PtrS->Top1)--];
 	} 
 	else 
 	{ 
 		/* 对第二个堆栈操作 */
 		if ( PtrS->Top2 == MaxSize ) { /*堆栈2空 */
 			printf(“堆栈2空”); 
 			return NULL;
 		} 
 		else 
 			return PtrS->Data[(PtrS->Top2)++];
	}
}

堆栈的顺序存储实现-数组实现-code
typedef int Position;
struct SNode {
    ElementType *Data; /* 存储元素的数组 */
    Position Top;      /* 栈顶指针 */
    int MaxSize;       /* 堆栈最大容量 */
};
typedef struct SNode *Stack;

Stack CreateStack( int MaxSize )
{
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
    S->Top = -1;
    S->MaxSize = MaxSize;
    return S;
}

bool IsFull( Stack S )
{
    return (S->Top == S->MaxSize-1);
}

bool Push( Stack S, ElementType X )
{
    if ( IsFull(S) ) {
        printf("堆栈满");
        return false;
    }
    else {
        S->Data[++(S->Top)] = X;
        return true;
    }
}

bool IsEmpty( Stack S )
{
    return (S->Top == -1);
}

ElementType Pop( Stack S )
{
    if ( IsEmpty(S) ) {
        printf("堆栈空");
        return ERROR; /* ERROR是ElementType的特殊值,标志错误 */
    }
    else 
        return ( S->Data[(S->Top)--] );
}


2.2.3 堆栈的链式存储实现(链表实现)


堆栈的链式存储实现

栈的链式存储结构实际上就是一个单链表,叫做链栈。插入和删除操作只能在链栈的栈顶进行。
栈顶指针Top应该在链表的哪一头? 表头!

typedef struct SNode *Stack;
struct SNode{
	ElementType Data;
	struct SNode *Next;
} ; 

(1) 堆栈初始化(建立空栈)

Stack CreateStack()
{ 
	/* 构建一个堆栈的头结点,返回指针 */
 	Stack S;
 	S =(Stack)malloc(sizeof(struct SNode));
 	S->Next = NULL;
 	return S;
}

(2) 判断堆栈S是否为空

int IsEmpty(Stack S)
{ 
	/*判断堆栈S是否为空,若为空函数返回整数1,否则返回0 */
	return ( S->Next == NULL );
}

Push

void Push( ElementType item, Stack S)
{ 
	/* 将元素item压入堆栈S */
	struct SNode *TmpCell;
	TmpCell=(struct SNode *)malloc(sizeof(struct SNode));
	TmpCell->Element = item;
	TmpCell->Next = S->Next;
	S->Next = TmpCell;
}

Pop

ElementType Pop(Stack S)
{ 
	/* 删除并返回堆栈S的栈顶元素 */
 	struct SNode *FirstCell;
	ElementType TopElem;
	if( IsEmpty( S ) ) {
		printf(“堆栈空”); 
		return NULL;
	} else {
		FirstCell = S->Next;
		S->Next = FirstCell->Next;
		TopElem = FirstCell ->Element;
		free(FirstCell);
		return TopElem;
	}
}

堆栈的链式存储实现-链表实现-code
typedef struct SNode *PtrToSNode;
struct SNode {
    ElementType Data;
    PtrToSNode Next;
};
typedef PtrToSNode Stack;

Stack CreateStack( ) 
{ /* 构建一个堆栈的头结点,返回该结点指针 */
    Stack S;

    S = (Stack)malloc(sizeof(struct SNode));
    S->Next = NULL;
    return S;
}

bool IsEmpty ( Stack S )
{ /* 判断堆栈S是否为空,若是返回true;否则返回false */
    return ( S->Next == NULL );
}

bool Push( Stack S, ElementType X )
{ /* 将元素X压入堆栈S */
    PtrToSNode TmpCell;

    TmpCell = (PtrToSNode)malloc(sizeof(struct SNode));
    TmpCell->Data = X;
    TmpCell->Next = S->Next;
    S->Next = TmpCell;
    return true;
}

ElementType Pop( Stack S )  
{ /* 删除并返回堆栈S的栈顶元素 */
    PtrToSNode FirstCell;
    ElementType TopElem;

    if( IsEmpty(S) ) {
        printf("堆栈空"); 
        return ERROR;
    }
    else {
        FirstCell = S->Next; 
        TopElem = FirstCell->Data;
        S->Next = FirstCell->Next;
        free(FirstCell);
        return TopElem;
    }
}


2.2.4 堆栈应用:表达式求值


堆栈应用:表达式求值
回忆:应用堆栈实现后缀表达式求值的基本过程:从左到右读入后缀表达式的各项(运算符或运算数);

  1. 运算数:入栈;
  2. 运算符:从堆栈中弹出适当数量的运算数,计算并结果入栈;
  3. 最后,堆栈顶上的元素就是表达式的结果值。

中缀表达式求值

基本策略:将中缀表达式转换为后缀表达式,然后求值

如何将中缀表达式转换为后缀?


观察一个简单例子: 2+9/3-5 → 2 9 3 / + 5 -

  1. 运算数相对顺序不变
  2. 运算符号顺序发生改变
    需要存储“等待中”的运算符号
    要将当前运算符号与“等待中”的最后一个运算符号比较

有括号怎么办?

 a * ( b + c ) / d = ?     
 a b c + *  d /

在这里插入图片描述
在这里插入图片描述


堆栈的其他应用:

  • 函数调用及递归实现
  • 深度优先搜索
  • 回溯算法
  • 。。。


2.3 队列


2.3.1 队列及顺序存储实现


什么是队列

队列(Queue):具有一定操作约束的线性表
插入和删除操作:只能在一端插入,而在另一端删除。

数据插入:入队列(AddQ)
数据删除:出队列(DeleteQ)
先来先服务
先进先出:FIFO


队列的抽象数据类型描述

类型名称:队列(Queue)
数据对象集:一个有0个或多个元素的有穷线性表。
操作集:长度为MaxSize的队列Q ∈ Queue,队列元素item ∈ ElementType

1、Queue CreatQueue( int MaxSize ):生成长度为MaxSize的空队列;
2int IsFullQ( Queue Q, int MaxSize ):判断队列Q是否已满;
3void AddQ( Queue Q, ElementType item ): 将数据元素item插入队列Q中;
4int IsEmptyQ( Queue Q ): 判断队列Q是否为空;
5、ElementType DeleteQ( Queue Q ):将队头数据元素从队列中删除并返回。

队列的顺序存储实现
队列的顺序存储结构通常由一个一维数组和一个记录队列头元素位置的变量front以及一个记录队列尾元素位置的变量rear组成。

#define MaxSize <储存数据元素的最大个数>
struct QNode {
	ElementType Data[ MaxSize ];
	int rear;  // 队尾 入队
	int front; // 队头 出队
};
typedef struct QNode *Queue;

顺环队列:

队列为空:front == rear 
队满:(rear+1) % MaxSize == front 
void AddQ( Queue PtrQ, ElementType item)
{
 	if ( (PtrQ->rear+1) % MaxSize == PtrQ->front ) {
 		printf(“队列满”);
 		return;
 	}
 	PtrQ->rear = (PtrQ->rear+1)% MaxSize;
 	PtrQ->Data[PtrQ->rear] = item;
}


ElementType DeleteQ ( Queue PtrQ )
{
 	if ( PtrQ->front == PtrQ->rear ) {
 		printf(“队列空”);
 		return ERROR;
 	} else {
 		PtrQ->front = (PtrQ->front+1)% MaxSize;
 		return PtrQ->Data[PtrQ->front];
 	}
}

顺环队列-数组实现-code
typedef int Position;
struct QNode {
    ElementType *Data;     /* 存储元素的数组 */
    Position Front, Rear;  /* 队列的头、尾指针 */
    int MaxSize;           /* 队列最大容量 */
};
typedef struct QNode *Queue;

Queue CreateQueue( int MaxSize )
{
    Queue Q = (Queue)malloc(sizeof(struct QNode));
    Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
    Q->Front = Q->Rear = 0;
    Q->MaxSize = MaxSize;
    return Q;
}

bool IsFull( Queue Q )
{
    return ((Q->Rear+1)%Q->MaxSize == Q->Front);
}

bool AddQ( Queue Q, ElementType X )
{
    if ( IsFull(Q) ) {
        printf("队列满");
        return false;
    }
    else {
        Q->Rear = (Q->Rear+1)%Q->MaxSize;
        Q->Data[Q->Rear] = X;
        return true;
    }
}

bool IsEmpty( Queue Q )
{
    return (Q->Front == Q->Rear);
}

ElementType DeleteQ( Queue Q )
{
    if ( IsEmpty(Q) ) { 
        printf("队列空");
        return ERROR;
    }
    else  {
        Q->Front =(Q->Front+1)%Q->MaxSize;
        return  Q->Data[Q->Front];
    }
}

2.3.2 队列的链式存储实现


队列的链式存储实现

队列的链式存储结构也可以用一个单链表实现。插入和删除操作分别在链表的两头进行;队列指针front和rear应该分别指向链表的哪一头?

单向链表

表头:插入 / 删除都行
表尾:插入新,删除不行(因为单链表无法直接拿到上一个节点的指针)

所以
front在队头 负责删除
rear在队尾 负责插入

struct Node{
 	ElementType Data;
 	struct Node *Next;
};
struct QNode{ /* 链队列结构 */
 	struct Node *rear; /* 指向队尾结点 */
 	struct Node *front; /* 指向队头结点 */
};
typedef struct QNode *Queue;
Queue PtrQ;

在这里插入图片描述

不带头结点的链式队列-code
typedef struct Node *PtrToNode;
struct Node { /* 队列中的结点 */
    ElementType Data;
    PtrToNode Next;
};
typedef PtrToNode Position;

struct QNode {
    Position Front, Rear;  /* 队列的头、尾指针 */
    int MaxSize;           /* 队列最大容量 */
};
typedef struct QNode *Queue;

bool IsEmpty( Queue Q )
{
    return ( Q->Front == NULL);
}

// 不带头结点的链式队列**出队操作**的一个示例:
ElementType DeleteQ( Queue Q )
{
    Position FrontCell; 
    ElementType FrontElem;
    
    if  ( IsEmpty(Q) ) {
        printf("队列空");
        return ERROR;
    }
    else {
        FrontCell = Q->Front;
        if ( Q->Front == Q->Rear ) /* 若队列只有一个元素 */
            Q->Front = Q->Rear = NULL; /* 删除后队列置为空 */
        else                     
            Q->Front = Q->Front->Next;
        FrontElem = FrontCell->Data;

        free( FrontCell );  /* 释放被删除结点空间  */
        return  FrontElem;
    }
}


2.4 应用实例:多项式加法运算


在这里插入图片描述
在这里插入图片描述

struct PolyNode {
	int coef; // 系数
	int expon; // 指数
	struct PolyNode *link; // 指向下一个节点的指针
};
typedef struct PolyNode *Polynomial;
Polynomial P1, P2;

在这里插入图片描述

多项式加法运算(合并有序链表)-code
Polynomial PolyAdd (Polynomial P1, Polynomial P2)
{
	Polynomial front, rear, temp;
	int sum;
	
// 为方便表头插入,先产生一个临时空结点(充当Head 没有数据域 只有指针域)作为结果多项式链表头
	rear = (Polynomial) malloc(sizeof(struct PolyNode));
	front = rear; /* 由front 记录结果多项式链表头结点 */
	
	while ( P1 && P2 ) /* 当两个多项式都有非零项待处理时 */
		switch ( Compare(P1->expon, P2->expon) ) {
		case 1: // P1中的数据项指数较大
			Attach( P1->coef, P1->expon, &rear);
			P1 = P1->link;
			break;
		case -1: // P2中的数据项指数较大
			Attach(P2->coef, P2->expon, &rear);
			P2 = P2->link;
			break;
		case 0: // 两数据项指数相等
			sum = P1->coef + P2->coef;
			 if ( sum ) // 注意判断系数和是否为0
			 	Attach(sum, P1->expon, &rear);
			 P1 = P1->link;
			 P2 = P2->link;
			 break;
		}
		
	/* 将未处理完的另一个多项式的所有节点依次复制到结果多项式中去 */
	for ( ; P1; P1 = P1->link ) Attach(P1->coef, P1->expon, &rear);
	for ( ; P2; P2 = P2->link ) Attach(P2->coef, P2->expon, &rear);
	rear->link = NULL;
	
	temp = front;
	front = front->link; /*令front指向结果多项式第一个非零项 */
	free(temp); /* 释放临时空表头结点 */
	return front;
} 



void Attach( int c, int e, Polynomial *pRear )
{ 
	/* 由于在本函数中需要改变当前结果表达式尾项指针的值, */
	/* 所以函数传递进来的是结点指针的地址,*pRear指向尾项*/
	 Polynomial P;
	 
	 P=(Polynomial)malloc(sizeof(struct PolyNode));/*申请新结点*/
	 P->coef = c; /* 对新结点赋值 */
	 P->expon = e;
	 P->link=NULL;
	 
	 /* 将P指向的新结点插入到当前结果表达式尾项的后面 */
	 (*pRear)->link = P;
	 *pRear = P; /* 修改pRear值 */
}

在这里插入图片描述



2.5 小白专场:一元多项式乘法与加法运算-C实现


2.5.1 题意理解与多项式表示


在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


求解思路
1.多项式表示
2. 程序框架
3. 读多项式
4. 加法实现
5. 乘法实现
6. 多项式输出


多项式的表示:仅表示非零项

数组:
 编程简单、调试容易
 需要事先确定数组大小
一种比较好的实现方法是: 动态数组

链表:
动态性强
编程略为复杂、调试比较困难


下面介绍链表表示:

1、多项式的表示

typedef struct PolyNode *Polynomial;
struct PolyNode {
	int coef;
	int expon;
	Polynomial link;
};

在这里插入图片描述



2.5.2 程序框架及读入多项式


2、程序框架搭建
在这里插入图片描述


3、读入多项式

Polynomial ReadPoly()
{
	 ……
	 scanf("%d", &N);
	 ……
	 while ( N-- ) {
		scanf("%d %d", &c, &e);
		Attach(c, e, &Rear);
	 }..
	 return P;	
}

Rear初值是多少?
两种处理方法:
	1.Rear初值为NULL在 
		Attach函数中根据Rear是否为NUL做不同处理
			==NULL 创建一个 开始插入
			!=NULL 直接插入即可
	2.Rear指向一个空结点
		申请空节点(新的 临时的 小链表),操作完,合并这些链表

在这里插入图片描述
在这里插入图片描述

void Attach( int c, int e, Polynomial *pRear )
{ 
	Polynomial P;
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	
	P->coef = c; /* 对新结点赋值 */
	P->expon = e;
	P->link = NULL;
	
	(*pRear)->link = P;
	*pRear = P; /* 修改pRear值 */
}
Polynomial ReadPoly()
{
 	Polynomial P, Rear, t;
 	int c, e, N;
 	
 	scanf("%d", &N);
 	
 	P = (Polynomial)malloc(sizeof(struct PolyNode)); /* 链表头空结点 */
 	P->link = NULL;
 	Rear = P;
 	
 	while ( N-- ) {
		scanf("%d %d", &c, &e);
		Attach(c, e, &Rear); /* 将当前项插入多项式尾部 */
 	}
 	
 	t = P; P = P->link; free(t); /* 删除临时生成的头结点 */
 	return P;
}

记得释放P(没释放前 如下:)
在这里插入图片描述



2.5.3 加法、乘法运算及多项式输出


多项式加法实现-见2.4节
Polynomial Add( Polynomial P1, Polynomial P2 )
{ 
	……
	t1 = P1; t2 = P2;
	
	P = (Polynomial)malloc(sizeof(struct PolyNode)); 
	P->link = NULL;
	Rear = P;
	
	while (t1 && t2) {
		if (t1->expon == t2->expon) {..
		}
		else if (t1->expon > t2->expon) {
			……
		}
		else {
		……
		}
	}
	while (t1) {
		……
	}
	while (t2) {..
	}
	……..
	return P;
}

多项式乘法实现

如何将两个多项式相乘
方法(步骤如下):

1.将乘法运算转换为加法运算
 	将P1当前项(ci,ei)乘P2多项式,再加到结果多项式里
 	
 	t1 = P1; t2 = P2;
 	
 	P = (Polynomial)malloc(sizeof(struct PolyNode)); 
 	P->link = NULL;
 	Rear = P;
 	
 	while (t2) {
		Attach(t1->coef*t2->coef, t1->expon+t2->expon, &Rear);
		t2 = t2->link;
 	}
2.逐项插入
 	将P1当前项(c1i,e1i)乘P2当前项(c2i,e2i),并插入到结果多项式中。关键是要找到插入位置
 	初始结果多项式可由P1第一项乘P2获得(如上)

Polynomial Mult( Polynomial P1, Polynomial P2 )
{ 
//--------------------------------------//

	Polynomial P, Rear, t1, t2, t;
	int c, e;
	
	if (!P1 || !P2) return NULL;
	
	t1 = P1; t2 = P2;
	P = (Polynomial)malloc(sizeof(struct PolyNode)); 
	P->link = NULL;
	Rear = P;
	
	while (t2) { /* 先用P1的第1项乘以P2,得到P */
		Attach(t1->coef*t2->coef, t1->expon+t2->expon, &Rear);
		t2 = t2->link;
	}
 	t1 = t1->link;

//--------------------------------------//

	while (t1) {
		t2 = P2; Rear = P;
		while (t2) {
			e = t1->expon + t2->expon;
			c = t1->coef * t2->coef;
			
			 while (Rear->link && Rear->link->expon > e)
				Rear = Rear->link;
				
			if (Rear->link && Rear->link->expon == e) //指数相等
			{
				if (Rear->link->coef + c)// 相加后系数!=0
					Rear->link->coef += c;
				else // 相加后系数==0
				{
					t = Rear->link;
					Rear->link = t->link;
					free(t);
				}
			}
			else {
				t = (Polynomial)malloc(sizeof(struct PolyNode));
				t->coef = c; t->expon = e;
				t->link = Rear->link;
				
				Rear->link = t; 
				Rear = Rear->link;
			}
 			t2 = t2->link
		}
		t1 = t1->link;
	}
	t2 = P; P = P->link; free(t2);
}

将多项式输出
void PrintPoly( Polynomial P )
{ 	/* 输出多项式 */

	int flag = 0; /* 辅助调整输出格式用 */
	/*
		第1项输出:系数 指数
		第2项输出:空格 系数 指数
		第3项输出:空格 系数 指数
		第4项输出:空格 系数 指数
		...
	*/
	
	if (!P) {printf("0 0\n"); return;}
	
	while ( P ) {
		if (!flag) flag = 1;
		else printf(" ");
			
		printf("%d %d", P->coef, P->expon);
		P = P->link;
	}
	printf("\n");
}


第二周的编程作业:

  1. 两个有序链表序列的合并: 这是一道C语言函数填空题,训练最基本的链表操作。如果会用C编程的话,一定要做;

  2. 一元多项式的乘法与加法运算: 在“小白专场”里,我们会详细讨论C语言实现的方法。对于不会C语言而不能做第1题的同学,本题一定要做;

  3. Reversing Linked List: 根据某大公司笔试题改编的2014年春季PAT真题,不难,可以尝试;

  4. Pop Sequence: 是2013年PAT春季考试真题,考察队堆栈的基本概念的掌握,应可以一试。



02-线性结构1 两个有序链表序列的合并 (15分)


题目链接

https://pintia.cn/problem-sets/1302953266564911104/problems/1307710853618270208


题目描述

本题要求实现一个函数,将两个链表表示的递增整数序列合并为一个非递减的整数序列。


函数接口定义:

List Merge( List L1, List L2 );

其中List结构定义如下:

typedef struct Node *PtrToNode;
struct Node {
    ElementType Data; /* 存储结点数据 */
    PtrToNode   Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */

L1和L2是给定的带头结点的单链表,其结点存储的数据是递增有序的;函数Merge要将L1和L2合并为一个非递减的整数序列。应直接使用原序列中的结点,返回归并后的带头结点的链表头指针。

裁判测试程序样例:

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

typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
    ElementType Data;
    PtrToNode   Next;
};
typedef PtrToNode List;

List Read(); /* 细节在此不表 */
void Print( List L ); /* 细节在此不表;空链表将输出NULL */

List Merge( List L1, List L2 );

int main()
{
    List L1, L2, L;
    L1 = Read();
    L2 = Read();
    L = Merge(L1, L2);
    Print(L);
    Print(L1);
    Print(L2);
    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

3
1 3 5
5
2 4 6 8 10

输出样例:

1 2 3 4 5 6 8 10 
NULL
NULL

code

List Merge( List L1, List L2 ) // 输入的是带头结点的
{
    List mergeList = (List)malloc(sizeof(struct Node));
    mergeList->Next = NULL;

    List ptr = mergeList;

    while(L1->Next && L2->Next)
    {
        // 合并为一个非递减的整数序列
        // 所以优先追加小的
        if(L1->Next->Data < L2->Next->Data)
        {
            ptr->Next = L1->Next;
            ptr = ptr->Next;
            L1->Next = L1->Next->Next;
        }
        else if(L2->Next->Data < L1->Next->Data)
        {
            ptr->Next = L2->Next;
            ptr = ptr->Next;
            L2->Next = L2->Next->Next;
        }
        else if(L1->Next->Data == L2->Next->Data)
        {
            ptr->Next = L1->Next;
            ptr = ptr->Next;
            L1->Next = L1->Next->Next;

            ptr->Next = L2->Next;
            ptr = ptr->Next;
            L2->Next = L2->Next->Next;
        }
    }
    if(L1->Next)
        ptr->Next = L1->Next;
    if(L2->Next)
        ptr->Next = L2->Next;

    L1->Next = NULL; // 记得置NULL
    L2->Next = NULL;
    return mergeList; // 输出也要求带头结点
}


02-线性结构2 一元多项式的乘法与加法运算 (20分)


题目链接

https://pintia.cn/problem-sets/1302953266564911104/problems/1307710853618270209


题目描述

设计函数分别求两个一元多项式的乘积与和。


输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。


输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。


输入样例:

4 	3 4 -5 2  6 1  -2 0
3 	5 20  -7 4  3 1

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

code

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

//多项式的表示
struct PolyNode{
    int coef;   //系数
    int expon;  //指数
    struct PolyNode* link; //指针:指向下一个节点
};
typedef struct PolyNode * Polynomial;
Polynomial P1,P2;

// 读入多项式
Polynomial ReadPoly()
{
 	Polynomial P, Rear, t;
 	int c, e, N;

 	scanf("%d", &N);

 	P = (Polynomial)malloc(sizeof(struct PolyNode)); /* 链表头空结点 */
 	P->link = NULL;
 	Rear = P;

 	while ( N-- ) {
		scanf("%d %d", &c, &e);
		Attach(c, e, &Rear); /* 将当前项插入多项式尾部 */
 	}

 	t = P; P = P->link; free(t); /* 删除临时生成的头结点 */
 	return P;
}


int Compare(int a,int b)
{
    if(a > b)
        return 1;
    else if(a < b)
        return -1;
    else
        return 0;
}

void Attach(int c,int e,Polynomial *mergeList)
{
    /* 由于在本函数中需要改变当前结果表达式尾项指针的值, */
	/* 所以函数传递进来的是结点指针的地址,*mergeList指向尾项*/

    Polynomial P;
    P = (Polynomial)malloc(sizeof(struct PolyNode));/*申请新结点*/
    P->coef = c;/* 对新结点赋值 */
    P->expon = e;
    P->link = NULL;

    /* 将P指向的新结点插入到当前结果表达式尾项的后面 */
    (*mergeList)->link = P;/* 修改mergeList值 */
    (*mergeList) = P;
}

Polynomial PolyAdd(Polynomial P1,Polynomial P2)
{
// 为方便表头插入,先产生一个临时空结点(充当Head 没有数据域 只有指针域)作为结果多项式链表头
    Polynomial merList = (Polynomial)malloc(sizeof(struct PolyNode));//负责:返回
    Polynomial ptr1 = merList;//负责:移动

    while(P1 && P2)/* 当两个多项式都有非零项待处理时 */
    {
        switch(Compare(P1->expon,P2->expon))
        {
        case 1:// P1中的数据项指数较大
            Attach(P1->coef,P1->expon,&ptr1);//在内部完成对ptr1的移动
            P1 = P1->link;
            break;
        case -1:// P2中的数据项指数较大
            Attach(P2->coef,P2->expon,&ptr1);//在内部完成对ptr1的移动
            P2 = P2->link;
            break;
        case 0: // 两数据项指数相等
            int sum = P1->coef + P2->coef;
            if(sum)// 注意判断系数和是否为0
                Attach(sum,P1->expon,&ptr1);//在内部完成对ptr1的移动
            P1 = P1->link;
            P2 = P2->link;
            break;
        }
    }

    /* 将未处理完的另一个多项式的所有节点追加到结果多项式中去 */
    if(P1)
        ptr1->link = P1;
    if(P2)
        ptr1->link = P2;

    ptr1->link = NULL;

    Polynomial ptr2 = merList;//记录结果多项式链表头结点,用来free头结点
    merList = merList->link;//此时没有Head,上来就是第一个有效节点
    free(ptr2);//释放临时的 空表头节点

    return merList;
}

//将两个多项式相乘
Polynomial Mult( Polynomial P1, Polynomial P2 )
{
//--------------------------------------//

	Polynomial P, Rear, t1, t2, t;
	int c, e;

	if (!P1 || !P2) return NULL;

	t1 = P1; t2 = P2;
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	P->link = NULL;
	Rear = P;

	while (t2) { /* 先用P1的第1项乘以P2,得到P */
		Attach(t1->coef*t2->coef, t1->expon+t2->expon, &Rear);
		t2 = t2->link;
	}
 	t1 = t1->link;

//--------------------------------------//

	while (t1) {
		t2 = P2; Rear = P;
		while (t2) {
			e = t1->expon + t2->expon;
			c = t1->coef * t2->coef;

			 while (Rear->link && Rear->link->expon > e)
				Rear = Rear->link;

			if (Rear->link && Rear->link->expon == e) //指数相等
			{
				if (Rear->link->coef + c)// 相加后系数!=0
					Rear->link->coef += c;
				else // 相加后系数==0
				{
					t = Rear->link;
					Rear->link = t->link;
					free(t);
				}
			}
			else {
				t = (Polynomial)malloc(sizeof(struct PolyNode));
				t->coef = c; t->expon = e;
				t->link = Rear->link;

				Rear->link = t;
				Rear = Rear->link;
			}
 			t2 = t2->link
		}
		t1 = t1->link;
	}
	t2 = P; P = P->link; free(t2);
}

//将多项式输出
void PrintPoly( Polynomial P )
{ 	/* 输出多项式 */

	int flag = 0; /* 辅助调整输出格式用 */
	/*
		第1项输出:系数 指数
		第2项输出:空格 系数 指数
		第3项输出:空格 系数 指数
		第4项输出:空格 系数 指数
		...
	*/

	if (!P) {printf("0 0\n"); return;}

	while ( P ) {
		if (!flag) flag = 1;
		else printf(" ");

		printf("%d %d", P->coef, P->expon);
		P = P->link;
	}
	printf("\n");
}


int main()
{

    return 0;
}




02-线性结构3 Reversing Linked List (25分)


题目链接

https://pintia.cn/problem-sets/1302953266564911104/problems/1307710853618270210


题目描述

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.


Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N ( ≤ 1 0 5 ​ ​ ) N (≤10^5​​ ) N(105) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is an integer, and Next is the position of the next node.


Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.


Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

在这里插入图片描述



code


input

10000 8 4
40000 4 50000
10000 1 20000
60000 6 70000
30000 3 40000
50000 5 60000
20000 2 30000
70000 7 80000
80000 8 -1

output

40000 4 30000
30000 3 20000
20000 2 10000
10000 1 80000
80000 8 70000
70000 7 60000
60000 6 50000
50000 5 -0001

input

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

output

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

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

struct node
{
    int Address;
    int Data;
    int Next;
};
typedef struct node Node;
typedef struct node *List;

//交换数组中[i],[j]的值
void switchElements(Node arr[], int i, int j);

//打印数组中元素的值
void printArr(Node arr[], int start, int end);

// 排序(按照数值大小)
// 1.数组名、2.数组第一个有效下标、3.数组最后一个有效下标、4.第一个节点的地址
List orderTheArray(Node arr[], int start, int end, int FirstNodeAddress);

/***
 *  翻转子链表 每K个节点组成的子链表,首尾翻转
 *  交换子链表节点,在数组中的位置,比如K=4时[1][4]互换[2][3]互换
*/
void Reverse(Node arr[], int N, int K);

int main()
{
    // 输入的第一行数据,包含三项:
    int FirstNodeAddress; //1.输入的第一个节点的地址
    int N;                //2.节点的个数
    int K;                //3.每K个节点翻转一下
    scanf("%d %d %d", &FirstNodeAddress, &N, &K);

    // 录入N个节点
    Node *arr_in = (Node *)malloc(sizeof(struct node) * N);
    int start = 1, end = N;
    for (int i = 1; i <= N; ++i) //[0]充当head
    {
        scanf("%d %d %d",
              &arr_in[i].Address,
              &arr_in[i].Data,
              &arr_in[i].Next);
    }
    printf("\n\n刚录入的数据如下:\n");
    printArr(arr_in, 1, N);

    //先排序(按照数值大小)
    orderTheArray(arr_in, start, end, FirstNodeAddress);
    printf("\n\n排序后的数据如下:\n");
    printArr(arr_in, 1, N);

    //每K个节点翻转一下,不足K个节点不翻转
    Reverse(arr_in, N, K);
    printf("\n\n翻转和交换后的数据如下:\n");
    printArr(arr_in, 1, N);

    return 0;
}

//交换数组中[i],[j]的值
void switchElements(Node arr[], int i, int j)
{
    Node tmp;

    tmp.Address = arr[i].Address;
    tmp.Data = arr[i].Data;
    tmp.Next = arr[i].Next;

    arr[i].Address = arr[j].Address;
    arr[i].Data = arr[j].Data;
    arr[i].Next = arr[j].Next;

    arr[j].Address = tmp.Address;
    arr[j].Data = tmp.Data;
    arr[j].Next = tmp.Next;
}

//打印数组中元素的值
void printArr(Node arr[], int start, int end)
{
    //printf("\n\n");
    for (int i = start; i <= end; ++i)
    {
        printf("%05d %d %05d\n", arr[i].Address, arr[i].Data, arr[i].Next);
    }
}

// 排序(按照数值大小)
// 1.数组名、2.数组第一个有效下标、3.数组最后一个有效下标、4.第一个节点的地址
List orderTheArray(Node arr[], int start, int end, int FirstNodeAddress)
{
    //找到第一个Node,并放置在[1]
    for (int i = start; i <= end; ++i)
    {
        if (arr[i].Address == FirstNodeAddress)
        {
            switchElements(arr, i, start);
            break;
        }
    }
    //依次摆放在[2]...[n]
    for (int i = start; i <= end; ++i)
    {
        for (int j = i + 1; j <= end; ++j)
        {
            if (arr[i].Next == arr[j].Address)
            {
                switchElements(arr, i + 1, j);
                break;
            }
        }
    }
}

/***
 *  翻转子链表 每K个节点组成的子链表,首尾翻转
 *  交换子链表节点,在数组中的位置,比如K=4时[1][4]互换[2][3]互换
*/
void Reverse(Node arr[], int N, int K)
{
    int cycles = N / K; //总共翻转几圈

    if (cycles > 0)
    {
        for (int cycle = 1; cycle <= cycles; cycle++)
        {
            //交换子链表节点,在数组中的位置,比如K=4时[1][4]互换[2][3]互换
            int i = (cycle - 1) * K + 1;
            int j = (cycle)*K;
            while (i <= j)
            {
                switchElements(arr, i++, j--);
            }
        }

        int newAdd, oldAdd, tmpAdd;
        for (int cycle = 1; cycle <= cycles; cycle++)
        {
            //翻转子链表 每K个节点组成的子链表,首尾翻转
            for (int cnt = 1; cnt < K; cnt++)
            {
                arr[(cycle - 1) * K + cnt].Next = arr[(cycle - 1) * K + cnt + 1].Address;
            }
        }

        // 搞定[K]...[n*K]..[N]的指向
        //1.如果只翻转了一次
        if (cycles == 1)
        {
            arr[cycles * K].Next = arr[cycles * K + 1].Address;
        }
        else if (cycles > 1)
        {
            for (int cycle = 1; cycle < cycles; cycle++)
            {
                arr[(cycle)*K].Next = arr[(cycle)*K + 1].Address;
            }
        }
        arr[N].Next = -1;
    }
}




02-线性结构4 Pop Sequence (25分)(未做,有时间整理上)


题目链接

https://pintia.cn/problem-sets/1302953266564911104/problems/1307710853618270211

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值