本文章为博主考研为巩固知识所写,有不足帮忙指出!!!加油!必上岸!!!
顺序表
#include <cstdio>
#include <stdlib.h>
#define MaxSize 100
#define test printf("测试\n");
typedef struct node{
int data[ MaxSize] ;
int length;
} SqList;
SqList InitList ( SqList L) {
for ( int i= 0 ; i< 100 ; i++ ) {
L. data[ i] = 0 ;
}
L. length= 0 ;
}
SqList BuildList ( SqList L) {
int x, i= 1 ;
scanf ( "%d" , & x) ;
while ( x!= 0 ) {
L. data[ i++ ] = x;
L. length++ ;
scanf ( "%d" , & x) ;
}
return L;
}
void PrintList ( SqList L) {
for ( int i= 1 ; i<= L. length; i++ ) {
printf ( "%d " , L. data[ i] ) ;
}
}
SqList InsertList ( SqList L, int i, int e) {
if ( i< 1 || i> L. length) {
printf ( "插入失败" ) ;
exit ( 1 ) ;
}
for ( int j= L. length; j>= i; j-- ) {
L. data[ j+ 1 ] = L. data[ j] ;
}
L. data[ i] = e;
L. length++ ;
return L;
}
SqList DeleteList ( SqList L, int i) {
if ( i< 1 || i> L. length) {
printf ( "删除失败" ) ;
exit ( 1 ) ;
}
for ( int j= i+ 1 ; j<= L. length; j++ ) {
L. data[ j- 1 ] = L. data[ j] ;
}
L. length-- ;
return L;
}
int SelectList ( SqList L, int e) {
for ( int i= 0 ; i< L. length; i++ ) {
if ( L. data[ i] == e) {
printf ( "%d的值在%d位/n" , e, i) ;
return 0 ;
}
}
}
int main ( ) {
SqList L;
InitList ( L) ;
L = BuildList ( L) ;
L= InsertList ( L, 3 , 10 ) ;
L= DeleteList ( L, 3 ) ;
SelectList ( L, 3 ) ;
PrintList ( L) ;
return 0 ;
}
单链表
#include <stdio.h>
#include <stdlib.h>
#define test_NULL if(L->next==NULL){printf("链表为空");exit(1);}
typedef struct node{
int data;
struct node * next;
} LNode, * SqList;
SqList InitList ( ) {
SqList L = ( SqList) malloc ( sizeof ( LNode) ) ;
if ( L== NULL ) {
printf ( "分配空间失败" ) ;
exit ( 1 ) ;
}
L- > next= NULL ;
return L;
}
SqList SequenceCreatList ( SqList L) {
int x;
scanf ( "%d" , & x) ;
while ( x!= 0 ) {
LNode * NewNode = ( LNode* ) malloc ( sizeof ( LNode) ) ;
if ( NewNode== NULL ) {
printf ( "分配空间失败" ) ;
exit ( 1 ) ;
}
NewNode- > data= x;
NewNode- > next= L- > next;
L- > next= NewNode;
scanf ( "%d" , & x) ;
}
return L;
}
SqList inSequenceCreatList ( SqList L) {
int x;
LNode* p= L;
scanf ( "%d" , & x) ;
while ( x!= 0 ) {
LNode* NewNode = ( LNode* ) malloc ( sizeof ( LNode) ) ;
if ( NewNode== NULL ) {
printf ( "分配空间失败" ) ;
exit ( 1 ) ;
}
NewNode- > data= x;
p- > next= NewNode;
p= NewNode;
scanf ( "%d" , & x) ;
}
p- > next= NULL ;
return L;
}
void PrintList ( SqList L) {
test_NULL;
printf ( "\n链表为:" ) ;
LNode* p;
p= L- > next;
while ( p!= NULL ) {
printf ( "%d " , * p) ;
p= p- > next;
}
printf ( "\n\n" ) ;
}
int GetLength ( SqList L) {
test_NULL;
int length= 0 ;
LNode* p= L- > next;
while ( p) {
length++ ;
p= p- > next;
}
return length;
}
LNode* GetElem ( SqList L, int x) {
test_NULL;
int i= 0 ;
LNode* p = L;
if ( x< 1 || x> GetLength ( L) ) {
printf ( "输入异常" ) ;
return ;
}
while ( i< x) {
p= p- > next;
i++ ;
}
return p;
}
int GetIndex ( SqList L, int x) {
test_NULL;
int i= 0 ;
LNode * p = L- > next;
while ( x!= p- > data&& p) {
i++ ;
p= p- > next;
}
return i+ 1 ;
}
void InsertElem ( SqList L, int x, int num) {
LNode* newNode = ( LNode* ) malloc ( sizeof ( LNode) ) ;
LNode* p= GetElem ( L, x- 1 ) ;
newNode- > data= num;
newNode- > next= p- > next;
p- > next= newNode;
}
void DeleteElem ( SqList L, int x) {
test_NULL;
LNode* newNode = ( LNode* ) malloc ( sizeof ( LNode) ) ;
LNode* p= GetElem ( L, x- 1 ) ;
LNode* q= p- > next;
p- > next= q- > next;
}
int main ( ) {
SqList L;
L = InitList ( L) ;
PrintList ( L) ;
printf ( "表长为:%d\n\n" , GetLength ( L) ) ;
printf ( "请输入查询带查询值的索引:" ) ;
int x1;
scanf ( "%d" , & x1) ;
printf ( "第%d个节点值为:%d\n\n" , x1, GetElem ( L, x1) - > data) ;
printf ( "请输入待查询索引的值:" ) ;
int x2;
scanf ( "%d" , & x2) ;
printf ( "值为%d的索引为:%d\n\n" , x2, GetIndex ( L, x2) ) ;
printf ( "输入插入位置,插入值:" ) ;
int t, x3;
scanf ( "%d %d" , & t, & x3) ;
InsertElem ( L, t, x3) ;
printf ( "插入之后链表:" ) ;
PrintList ( L) ;
printf ( "请输入删除位置:" ) ;
int x4;
scanf ( "%d" , & x4) ;
DeleteElem ( L, x4) ;
printf ( "删除之后的链表:" ) ;
PrintList ( L) ;
return 0 ;
}
无头节点单链表
#include <stdio.h>
#include <stdlib.h>
#define test_NULL if(L==NULL){printf("链表为空");exit(1);}
typedef struct node{
int data;
struct node * next;
} LNode, * SqList;
SqList InitSqList ( SqList L) {
L= NULL ;
return L;
}
SqList headBuildList ( SqList L) {
int x;
scanf ( "%d" , & x) ;
while ( x!= 0 ) {
LNode* newNode = ( LNode* ) malloc ( sizeof ( LNode) ) ;
if ( newNode== NULL ) {
printf ( "分配空间失败" ) ;
exit ( 1 ) ;
}
newNode- > data= x;
newNode- > next= L;
L= newNode;
scanf ( "%d" , & x) ;
}
return L;
}
SqList behindBuildList ( SqList L) {
int x;
LNode* p= L;
scanf ( "%d" , & x) ;
while ( x!= 0 ) {
LNode* newNode = ( LNode* ) malloc ( sizeof ( LNode) ) ;
if ( newNode== NULL ) {
printf ( "分配空间失败" ) ;
exit ( 1 ) ;
}
newNode- > data= x;
if ( p) {
p- > next= newNode;
} else {
L= newNode;
}
p= newNode;
scanf ( "%d" , & x) ;
}
p- > next= NULL ;
return L;
}
void PrintList ( SqList L) {
test_NULL;
LNode * q = L;
while ( q) {
printf ( "%d " , q- > data) ;
q= q- > next;
}
}
int lengthList ( SqList L) {
test_NULL;
int length= 0 ;
LNode* p = L;
while ( p) {
p= p- > next;
length++ ;
}
return length;
}
LNode* FindLocate ( SqList L, int i) {
test_NULL;
LNode * p = L;
int j= 1 ;
while ( j!= i) {
p= p- > next;
j++ ;
}
return p;
}
SqList InsertList ( SqList L, int i, int e) {
test_NULL;
if ( i< 1 || i> lengthList ( L) ) {
printf ( "输入不合法\n" ) ;
exit ( 1 ) ;
}
LNode* newNode = ( LNode* ) malloc ( sizeof ( LNode) ) ;
if ( newNode== NULL ) {
printf ( "分配空间失败" ) ;
exit ( 1 ) ;
}
newNode- > data= e;
if ( i== 1 ) {
newNode- > next= L;
L= newNode;
return L;
}
LNode* frontNode = FindLocate ( L, i- 1 ) ;
newNode- > next= frontNode- > next;
frontNode- > next= newNode;
return L;
}
SqList DeleteNode ( SqList L, int i) {
test_NULL;
if ( i== 1 ) {
LNode* q = L;
L = q- > next;
free ( q) ;
return L;
}
LNode* frontNode = FindLocate ( L, i- 1 ) ;
LNode* q = frontNode- > next;
frontNode- > next = frontNode- > next- > next;
free ( q) ;
return L;
}
SqList UpdateNode ( SqList L, int i, int x) {
test_NULL;
LNode* Node = FindLocate ( L, i) ;
Node- > data = x;
return L;
}
int main ( ) {
SqList L;
L = InitSqList ( L) ;
L = behindBuildList ( L) ;
printf ( "表长为:%d\n" , lengthList ( L) ) ;
UpdateNode ( L, 1 , 100 ) ;
PrintList ( L) ;
return 0 ;
}
双链表
#include <stdio.h>
#include <stdlib.h>
#define test_NULL if(L->next==NULL){printf("链表为空");exit(1);}
typedef struct Node{
int data;
struct Node * prior, * next;
} LNode, * SqList;
SqList InitList ( ) {
LNode* L = ( LNode* ) malloc ( sizeof ( LNode) ) ;
if ( L== NULL ) {
printf ( "分配空间失败" ) ;
exit ( 1 ) ;
}
L- > next= NULL ;
L- > prior= NULL ;
}
SqList BuildList ( SqList L) {
int x;
scanf ( "%d" , & x) ;
LNode* p = L;
while ( x!= 0 ) {
LNode* newNode = ( LNode* ) malloc ( sizeof ( LNode) ) ;
newNode- > data = x;
p- > next= newNode;
newNode- > prior= p;
p= newNode;
scanf ( "%d" , & x) ;
}
p- > next= NULL ;
return L;
}
void PrintfList ( SqList L) {
test_NULL;
LNode* p = L- > next;
while ( p) {
printf ( "%d " , p- > data) ;
p = p- > next;
}
}
int LengthList ( SqList L) {
test_NULL;
int length = 0 ;
LNode* p = L- > next;
while ( p) {
length++ ;
p = p- > next;
}
return length;
}
LNode* FindLocate ( SqList L, int i) {
test_NULL;
int j= 1 ;
LNode* p = L- > next;
while ( i!= j) {
p = p- > next;
j++ ;
}
return p;
}
SqList InsertList ( SqList L, int i, int e) {
test_NULL;
LNode* newNode = ( LNode* ) malloc ( sizeof ( LNode) ) ;
if ( newNode== NULL ) {
printf ( "空间分配失败" ) ;
exit ( 1 ) ;
}
newNode- > data= e;
LNode* p = FindLocate ( L, i) ;
newNode- > next = p;
p- > prior- > next= newNode;
newNode- > prior = p- > prior;
p- > prior = newNode;
return L;
}
SqList DeleteList ( SqList L, int i) {
test_NULL;
LNode* p = FindLocate ( L, i) ;
p- > prior- > next = p- > next;
p- > next- > prior = p- > prior;
free ( p) ;
return L;
}
int main ( ) {
SqList L = InitList ( ) ;
L = BuildList ( L) ;
printf ( "表长为%d\n" , LengthList ( L) ) ;
L = InsertList ( L, 2 , 100 ) ;
L = DeleteList ( L, 2 ) ;
PrintfList ( L) ;
return 0 ;
}
循环双链表
#include <stdio.h>
#include <stdlib.h>
#define test_NULL if(L->next==NULL){printf("链表为空");exit(1);}
typedef struct Node{
int data;
struct Node * prior, * next;
} LNode, * SqList;
SqList InitList ( ) {
LNode* L = ( LNode* ) malloc ( sizeof ( LNode) ) ;
if ( L== NULL ) {
printf ( "分配空间失败" ) ;
exit ( 1 ) ;
}
L- > next= L;
L- > prior= L;
}
SqList BuildList ( SqList L) {
int x;
scanf ( "%d" , & x) ;
LNode* p = L;
while ( x!= 0 ) {
LNode* newNode = ( LNode* ) malloc ( sizeof ( LNode) ) ;
newNode- > data = x;
p- > next= newNode;
newNode- > prior= p;
p= newNode;
scanf ( "%d" , & x) ;
}
p- > next= L;
L- > prior= p;
return L;
}
void PrintfList ( SqList L) {
test_NULL;
LNode* p = L- > next;
while ( p!= L) {
printf ( "%d " , p- > data) ;
p = p- > next;
}
}
int LengthList ( SqList L) {
test_NULL;
int length = 0 ;
LNode* p = L- > next;
while ( p!= L) {
length++ ;
p = p- > next;
}
return length;
}
LNode* FindLocate ( SqList L, int i) {
test_NULL;
int j= 1 ;
LNode* p = L- > next;
while ( i!= j) {
p = p- > next;
j++ ;
}
return p;
}
SqList InsertList ( SqList L, int i, int e) {
test_NULL;
LNode* newNode = ( LNode* ) malloc ( sizeof ( LNode) ) ;
if ( newNode== NULL ) {
printf ( "空间分配失败" ) ;
exit ( 1 ) ;
}
newNode- > data= e;
LNode* p = FindLocate ( L, i) ;
newNode- > next = p;
p- > prior- > next= newNode;
newNode- > prior = p- > prior;
p- > prior = newNode;
return L;
}
SqList DeleteList ( SqList L, int i) {
test_NULL;
LNode* p = FindLocate ( L, i) ;
p- > prior- > next = p- > next;
p- > next- > prior = p- > prior;
free ( p) ;
return L;
}
int main ( ) {
SqList L = InitList ( ) ;
L = BuildList ( L) ;
printf ( "表长为%d\n" , LengthList ( L) ) ;
L = InsertList ( L, 2 , 100 ) ;
L = DeleteList ( L, 2 ) ;
PrintfList ( L) ;
return 0 ;
}
顺序栈
#include <cstdio>
#include <stdlib.h>
#define MaxSize 100
#define test_NULL if(S.top==-1){printf("栈空");exit(1);}
typedef struct {
int data[ MaxSize] ;
int top;
} SqStack;
SqStack InitStack ( SqStack S) {
S. top = - 1 ;
}
int StackEmpty ( SqStack S) {
return S. top= - 1 ? 1 : 0 ;
}
SqStack PushStack ( SqStack S, int x) {
if ( S. top== MaxSize- 1 ) {
printf ( "栈满\n" ) ;
exit ( 1 ) ;
}
S. data[ ++ S. top] = x;
return S;
}
SqStack popStack ( SqStack S) {
test_NULL;
S. top-- ;
return S;
}
int GetTop ( SqStack S) {
test_NULL;
return S. data[ S. top] ;
}
int main ( ) {
SqStack S;
S = InitStack ( S) ;
int x;
scanf ( "%d" , & x) ;
while ( x!= 0 ) {
S= PushStack ( S, x) ;
scanf ( "%d" , & x) ;
}
printf ( "栈顶元素为:%d\n" , GetTop ( S) ) ;
S= popStack ( S) ;
printf ( "栈顶元素为:%d\n" , GetTop ( S) ) ;
return 0 ;
}
链栈
#include <stdio.h>
#include <stdlib.h>
typedef struct LinkNode{
int data;
struct LinkNode * next;
} LNode, * LiStack;
LiStack InitTsack ( ) {
LiStack S = ( LiStack) malloc ( sizeof ( LNode) ) ;
if ( S== NULL ) {
printf ( "内存分配失败\n" ) ;
exit ( 1 ) ;
}
S- > next = NULL ;
}
int EmptyStack ( LiStack S) {
return S- > next== NULL ? 1 : 0 ;
}
LiStack PushStack ( LiStack S, int x) {
LNode* newNode = ( LNode* ) malloc ( sizeof ( LNode) ) ;
if ( S== NULL ) {
printf ( "内存分配失败\n" ) ;
exit ( 1 ) ;
}
newNode- > data = x;
newNode- > next= S- > next;
S- > next= newNode;
return S;
}
LiStack PopStack ( LiStack S) {
if ( EmptyStack ( S) ) {
printf ( "栈空\n" ) ;
exit ( 1 ) ;
}
LNode* p = S- > next;
S- > next= S- > next- > next;
free ( p) ;
return S;
}
int GetTop ( LiStack S) {
if ( EmptyStack ( S) ) {
printf ( "栈空\n" ) ;
exit ( 1 ) ;
}
return S- > next- > data;
}
int LengthSTack ( LiStack S) {
LNode* p = S- > next;
int length = 0 ;
while ( p) {
length++ ;
p = p- > next;
}
return length;
}
int main ( ) {
LiStack S;
S = InitTsack ( ) ;
int x;
scanf ( "%d" , & x) ;
while ( x!= 0 ) {
S= PushStack ( S, x) ;
scanf ( "%d" , & x) ;
}
printf ( "栈的大小:%d\n" , LengthSTack ( S) ) ;
PopStack ( S) ;
printf ( "栈顶元素为:%d\n" , GetTop ( S) ) ;
return 0 ;
}
循环队列
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 100
typedef struct {
int data[ MaxSize] ;
int front, rear;
} SqQueue;
SqQueue InitQueue ( SqQueue Q) {
Q. front= Q. rear= 0 ;
}
SqQueue EnQueue ( SqQueue Q, int x) {
if ( ( Q. rear+ 1 ) % MaxSize== Q. front) {
printf ( "队满" ) ;
exit ( 1 ) ;
}
Q. data[ Q. rear] = x;
Q. rear = ( Q. rear+ 1 ) % MaxSize;
return Q;
}
int IsEmpty ( SqQueue Q) {
return Q. front== Q. rear? 1 : 0 ;
}
SqQueue Dequeue ( SqQueue Q) {
if ( IsEmpty ( Q) ) {
printf ( "队空\n" ) ;
exit ( 1 ) ;
}
Q. front= ( Q. front+ 1 ) % MaxSize;
return Q;
}
int GetFront ( SqQueue Q) {
if ( IsEmpty ( Q) ) {
printf ( "队空\n" ) ;
exit ( 1 ) ;
}
return Q. data[ Q. front] ;
}
int LengthQueue ( SqQueue Q) {
if ( IsEmpty ( Q) ) {
printf ( "队空\n" ) ;
exit ( 1 ) ;
}
return ( Q. rear- Q. front+ MaxSize) % MaxSize;
}
int main ( ) {
SqQueue Q;
Q = InitQueue ( Q) ;
int x;
scanf ( "%d" , & x) ;
while ( x!= 0 ) {
Q= EnQueue ( Q, x) ;
scanf ( "%d" , & x) ;
}
printf ( "队首元素为:%d 队列长度为%d\n\n" , GetFront ( Q) , LengthQueue ( Q) ) ;
Q = Dequeue ( Q) ;
printf ( "队首元素为:%d 队列长度为%d\n\n" , GetFront ( Q) , LengthQueue ( Q) ) ;
return 0 ;
}
链式队列
#include <stdio.h>
#include <stdlib.h>
typedef struct LinkNode{
int data;
struct LinkNode * next;
} LinkNode;
typedef struct {
LinkNode * front, * rear;
} LinkQueue;
LinkQueue InitQueue ( LinkQueue Q) {
Q. front = Q. rear = ( LinkNode* ) malloc ( sizeof ( LinkNode) ) ;
Q. front- > next= NULL ;
return Q;
}
int IsEmpty ( LinkQueue Q) {
return Q. front == Q. rear? 1 : 0 ;
}
LinkQueue EnQueue ( LinkQueue Q, int x) {
LinkNode* s = ( LinkNode* ) malloc ( sizeof ( LinkNode) ) ;
s- > data= x; s- > next= NULL ;
Q. rear- > next= s;
Q. rear= s;
return Q;
}
LinkQueue DeQueue ( LinkQueue Q) {
if ( IsEmpty ( Q) ) {
printf ( "队空" ) ;
exit ( 1 ) ;
}
LinkNode* p = Q. front- > next;
Q. front- > next= p- > next;
free ( p) ;
return Q;
}
int LengthQueue ( LinkQueue Q) {
if ( IsEmpty ( Q) ) {
printf ( "队空" ) ;
exit ( 1 ) ;
}
int length = 0 ;
LinkNode* p= Q. front- > next;
while ( p) {
length++ ;
p= p- > next;
}
return length;
}
int GetFront ( LinkQueue Q) {
if ( IsEmpty ( Q) ) {
printf ( "队空" ) ;
exit ( 1 ) ;
}
return Q. front- > next- > data;
}
int main ( ) {
LinkQueue Q;
Q = InitQueue ( Q) ;
int x;
scanf ( "%d" , & x) ;
while ( x!= 0 ) {
Q= EnQueue ( Q, x) ;
scanf ( "%d" , & x) ;
}
printf ( "队首元素为:%d 队列长度为%d\n\n" , GetFront ( Q) , LengthQueue ( Q) ) ;
Q = DeQueue ( Q) ;
printf ( "队首元素为:%d 队列长度为%d\n\n" , GetFront ( Q) , LengthQueue ( Q) ) ;
return 0 ;
}