10.2-2
用一个单链表实现一个栈。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct node;
typedef struct node * Stack;
typedef Stack PtrToNode;
struct node{
int key;
PtrToNode next;
};
Stack CreateStack(void){
Stack S;
S = malloc(sizeof(struct node));
if(S == NULL)
printf("out of sapce\n");
S->next = NULL;
return S;
}
bool isEmpty(Stack S){
return S->next == NULL;
}
void Push(Stack S,int x){
PtrToNode temp;
temp = malloc(sizeof(struct node));
if(temp == NULL)
printf("out of space\n");
else{
temp->key = x;
temp->next = S->next;
S->next = temp;
}
}
int Pop(Stack S){
PtrToNode temp;
int num;
if(isEmpty(S)){
printf("the stack is empty\n");
return;
}
temp = S->next;
S->next = temp->next;
num = temp->key;
free(temp);
return num;
}
int main(void){
int i,num;
Stack S;
S = CreateStack();
for(i = 0;i< 5;i++){
scanf("%d",&num);
Push(S,num);
}
printf("the first data is %d\n",Pop(S));
return 0;
}
10.2-3
用一个单链表实现一个队列。
#include<stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct node;
typedef struct node * Heap;
typedef Heap PtrToNode;
struct node{
int key;
PtrToNode next;
};
Heap CreateHeap(void){
Heap S;
S = malloc(sizeof(struct node));
if(S == NULL){
printf("out of sapce\n");
exit(1);
}
else{
S->next = NULL;
return S;
}
}
bool IsEmpty(Heap heap){
return heap->next == NULL;
}
void Enqueue(Heap heap,int x){
PtrToNode H;
H = malloc(sizeof(struct node));
if(H == NULL){
printf("out of space\n");
exit(1);
}
H->key = x;
H->next = heap->next;
heap->next = H;
}
int Dequeue(Heap heap){
int num;
PtrToNode P;
if(IsEmpty(heap)){
printf("the heap is empty\n");
return ;
}
P = heap->next;
while(P->next->next != NULL)
P= P->next;
num = P->next->key;
free(P->next);
P->next = NULL;
return num;
}
int main(void){
int i,num;
Heap heap;
heap = CreateHeap();
for(i = 0;i< 5;i++){
scanf("%d",&num);
Enqueue(heap,num);
}
printf("the first data is %d",Dequeue(heap));
return 0;
}
10.2-5
单向循环链表
#include<stdio.h>
#include <stdlib.h>
struct T_list;
typedef struct T_list * List;
typedef List Position;
struct T_list{
int key;
Position next;
};
Position List_Search(List L,int x){
Position P;
P = L->next;
while(P != NULL && P->key != x)
P = P->next;
return P;
}
Position List_create( ){
Position head;
head = malloc(sizeof(struct T_list));
if(head == NULL){
printf("out of space\n");
exit(1);
}
head->next = head;
return head;
}
void List_insert(List L,int x){
Position temp;
temp = malloc(sizeof(struct T_list));
if(temp == NULL){
printf("out of space\n");
exit(1);
}
else{
temp->key = x;
temp->next = L->next;
L->next = temp;
}
}
void List_delete(List L,int x){
Position P;
P = L;
while(P->next != L && P->next->key != x)
P = P->next;
if(P->next == L){ //如果P成为最后一个元素,说明链表中没有关键值为x
printf("the list no %d",x);
return;
}
else{
Position de;
de = P->next;
P->next = de->next;
free(de);
}
}
void List_print(List L){
Position P;
P = L->next;
while(P != L){
printf("%d ",P->key);
P = P->next;
}
printf("\n");
}
int main(void){
int i,num;
List head;
head = List_create();
for(i = 0;i <5;i++){
scanf("%d",&num);
List_insert(head,num);
}
List_print(head);
List_delete(head,3);
List_print(head);
return 0;
}
10.2-6
将A集合放入一个链表,将B集合放入一个链表,A链表的尾指针指向B,就完成合并操作。
10.2-7
void reverse(List L){
Position P = NULL;
Position q = L;
position r;
while(1){
r = q->next;
q->next = p;
if(r == NULL){
L = q;
break;
}
p = q;
q = r;
}
}