1、顺序表、单链和双向链表的定义字符串,初始化、插入和删除操作函数,并利用这些操作完成: 2、编写一个读入一个字符串,把它顺序存入一个顺序表,并按逆序打印的程序。(线性表) 3、编写一个读入一个字符串,把它顺序存入一个双向链表,并按逆序打印的程序。(线性表)4、写出主函数验证函数功能
时间: 2023-06-19 19:08:40 浏览: 136
顺序表的定义:
```c++
#define MaxSize 100
typedef struct{
char data[MaxSize];
int length;
}SqList;
```
单链表的定义:
```c++
typedef struct LNode{
char data;
struct LNode *next;
}LNode, *LinkList;
```
双向链表的定义:
```c++
typedef struct DLNode{
char data;
struct DLNode *prior, *next;
}DLNode, *DLinkList;
```
顺序表的初始化:
```c++
void InitList(SqList &L){
L.length = 0;
}
```
单链表的初始化:
```c++
void InitList(LinkList &L){
L = (LNode*)malloc(sizeof(LNode));
L->next = NULL;
}
```
双向链表的初始化:
```c++
void InitList(DLinkList &L){
L = (DLNode*)malloc(sizeof(DLNode));
L->prior = L->next = NULL;
}
```
顺序表的插入:
```c++
bool ListInsert(SqList &L, int i, char e){
if(i < 1 || i > L.length+1) return false;
if(L.length >= MaxSize) return false;
for(int j = L.length; j >= i; j--){
L.data[j] = L.data[j-1];
}
L.data[i-1] = e;
L.length++;
return true;
}
```
单链表的插入:
```c++
bool ListInsert(LinkList &L, int i, char e){
if(i < 1) return false;
LNode *p;
int j = 0;
p = L;
while(p != NULL && j < i-1){
p = p->next;
j++;
}
if(p == NULL) return false;
LNode *s = (LNode*)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
```
双向链表的插入:
```c++
bool ListInsert(DLinkList &L, int i, char e){
if(i < 1) return false;
DLNode *p;
int j = 0;
p = L;
while(p != NULL && j < i-1){
p = p->next;
j++;
}
if(p == NULL) return false;
DLNode *s = (DLNode*)malloc(sizeof(DLNode));
s->data = e;
s->next = p->next;
s->prior = p;
if(p->next != NULL) p->next->prior = s;
p->next = s;
return true;
}
```
顺序表的删除:
```c++
bool ListDelete(SqList &L, int i, char &e){
if(i < 1 || i > L.length) return false;
e = L.data[i-1];
for(int j = i; j < L.length; j++){
L.data[j-1] = L.data[j];
}
L.length--;
return true;
}
```
单链表的删除:
```c++
bool ListDelete(LinkList &L, int i, char &e){
if(i < 1) return false;
LNode *p;
int j = 0;
p = L;
while(p != NULL && j < i-1){
p = p->next;
j++;
}
if(p == NULL || p->next == NULL) return false;
LNode *q = p->next;
e = q->data;
p->next = q->next;
free(q);
return true;
}
```
双向链表的删除:
```c++
bool ListDelete(DLinkList &L, int i, char &e){
if(i < 1) return false;
DLNode *p;
int j = 0;
p = L;
while(p != NULL && j < i-1){
p = p->next;
j++;
}
if(p == NULL || p->next == NULL) return false;
DLNode *q = p->next;
e = q->data;
p->next = q->next;
if(q->next != NULL) q->next->prior = p;
free(q);
return true;
}
```
顺序表读入字符串并逆序打印:
```c++
void ReversePrint(SqList L){
for(int i = L.length-1; i >= 0; i--){
printf("%c", L.data[i]);
}
}
```
双向链表读入字符串并逆序打印:
```c++
void ReversePrint(DLinkList L){
DLNode *p = L->next;
while(p != NULL){
p = p->next;
}
while(p != L){
printf("%c", p->data);
p = p->prior;
}
}
```
主函数:
```c++
int main(){
//顺序表测试
SqList L;
InitList(L);
char str[MaxSize];
printf("请输入字符串:");
scanf("%s", str);
for(int i = 0; str[i] != '\0'; i++){
ListInsert(L, i+1, str[i]);
}
printf("逆序输出:");
ReversePrint(L);
printf("\n");
//双向链表测试
DLinkList DL;
InitList(DL);
printf("请输入字符串:");
scanf("%s", str);
int len = strlen(str);
for(int i = 0; i < len; i++){
ListInsert(DL, i+1, str[i]);
}
printf("逆序输出:");
ReversePrint(DL);
printf("\n");
return 0;
}
```
阅读全文