代码(前三章)

今天花了几个小时才弄出来了,还是有点不熟练,不过万幸的是终于可以自己手写了,哈哈,功夫不负有心人,直接进入正题。

 

顺序存储结构的单链表:

typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode *next;
int flag;
}LNode,*List;

List CreateList (ElemType n);
void EnList (List L,ElemType x,int Position);
int DeList(List L,int Position);
void PrintList (List L);

int main ()
{
int n = 5;
List L = CreateList (n);

PrintList(L);
return 0;
}

void PrintList (List L)
{
List p = L;
while(p->flag){
printf("%d ",DeList(p,p->flag));
}
printf("\n");
}
List CreateList (ElemType n)
{
List L = (List)malloc(sizeof(LNode));
L->next = NULL;
L->flag = 0;
ElemType x;
int i;
for(i = 0;i < n;i++){
printf("Please enter a number:");
scanf("%d",&x);
EnList(L,i+1,i+1);
}

return L;
}
void EnList (List L,ElemType x,int Position)
{
if(Position < 1 || Position - 1 > L->flag ){
printf("Illegal Insert!\n");
return;
}

List p = L;
while(Position - 1){
p = p->next;
Position--;
}
List temp = (LNode*)malloc(sizeof(LNode));
temp->data = x;
temp->next = p->next;
p->next = temp;
L->flag++;
printf("L->flag is %d\n",L->flag);
}
int DeList (List L,int Position)
{
List p = L;
if(Position < 1 || Position > L->flag) {
printf("Illegal Delete!\n");
return -1;
}

while (Position -1 ){
p = p->next;
Position--;
}

List temp = p->next;
p->next = temp->next;
int x = temp->data;
L->flag--;
free(temp);
return x;
}

链式存储的单链表:

typedef struct LNode {
int *data;
int length;
int maxsize;
}LNode,*List;

List CreateList (int maxsize);
void EnList (List L,int x,int i );
int DeList (List L,int i);
void PrintList (List L);

int main ()
{
List L;
L = CreateList (4);
PrintList(L);
DeList (L,1);
DeList(L,3);
PrintList (L);

return 0;
}

void PrintList (List L)
{
int i;
for(i = 0;i < L->length;i++){
printf("%d ",L->data[i]);
}

printf("\n");
}

List CreateList (int maxsize)
{
List L = (List)malloc(sizeof(LNode));
L->data = (int*)malloc(sizeof(maxsize));
L->maxsize = maxsize;
L->length = 0;
int i = 0;
int n;
for(i = 0;i < maxsize;i++) {
scanf("%d",&n);
EnList(L,n,i+1);
}

return L;
}

void EnList (List L,int x,int i)
{
if(i < 1 || i -1 > L->length){
printf("Illegal Insert!\n");
return;
}

int j;
for(j = L->length;j > i-1;j--){
L->data[j] = L->data[j--];
}

L->data[i-1] = x;
L->length++;
}

int DeList (List L,int i)
{
List p = L;
if(i < 1 || i > p->length){
printf("Illegal Delete!\n");
return -1;
}

int j;
int x = p->data[i -1];
for(j = i -1;j < L->length;j++){
L->data[j] = L->data[j+1];
}
p->length--;

return x;
}

顺序存储的栈:

typedef struct SNode {
int *data;
int top;
int maxsize;
}SNode,*Stack;

Stack CreateStack (int maxsize);
void Push (Stack S, int x);
int Pop (Stack S);
void PrintS (Stack S);


int main ()
{
Stack S;
S = CreateStack (5);
PrintS(S);
return 0;
}

void PrintS (Stack S)
{
while(S->top + 1) {
printf("%d ",Pop(S));
}

printf("\n");
}
Stack CreateStack (int maxsize)
{
Stack S;
S = (Stack)malloc(sizeof(SNode));
S->data = (int*)malloc(sizeof(int)*maxsize);
S->top = -1;
S->maxsize = maxsize;
int i ;
for(i = 0 ;i <maxsize;i++){
Push(S,i+1);
}

return S;
}

void Push (Stack S, int x)
{
if (S->top == S->maxsize-1) {
printf("the stack is full!\n");
return;
}

S->top++;
S->data[S->top] = x;
}

int Pop (Stack S)
{
if (S->top == -1) {
printf("The stack is empty!\n");
return -1;
}else {
int x = S->data[S->top];
S->top--;
return x;
}
链式存储的栈:

typedef struct SNode {
int data;
struct SNode *next;
}SNode,*Stack;

Stack CreateStack (int n);
void Push (Stack S,int x);
int Pop (Stack S);
void PrintS (Stack S);

int main ()
{
Stack S = CreateStack(5);
PrintS(S);

return 0;
}

void PrintS (Stack S)
{
Stack p = S;
while (p->next) {
printf("%d ",Pop(S));
}

printf("\n");
}

Stack CreateStack (int n)
{
Stack S;
S = (SNode*)malloc(sizeof(SNode));
S->next = NULL;

int i ;
for(i = 0 ;i < n;i++){
Push(S,i);
}

return S;
}

void Push (Stack S,int x)
{
Stack p = S;
Stack temp;
temp = (SNode*)malloc(sizeof(SNode));
temp->data = x;
temp->next = p->next;
p->next = temp;
}

int Pop(Stack S)
{
if(!S->next) {
printf("The stack is empty!\n");
return -1;
}
Stack p = S;
Stack temp = p->next;
p->next = temp->next;
int x = temp->data;
free(temp);

return x;
}

顺序存储的队列:

typedef struct node {
int *data;
int front;
int rear;
int maxsize;
}Node,*Queue;

Queue CreateQueue (int maxsize);
void EnQueue (Queue Q,int x);
int DeQueue (Queue Q);
void PrintQ (Queue Q);
int IsFull (Queue Q);
int IsEmpty (Queue Q);


int main ()
{
Queue Q = CreateQueue(6);
//PrintQ(Q);
EnQueue(Q,10);
PrintQ(Q);
EnQueue(Q,10);
printf("%d ",DeQueue(Q));

return 0;
}

void PrintQ(Queue Q)
{
int i;
Queue q = Q;
for(i = 0; i < q->maxsize - 1;i++){
printf("%d ",DeQueue(q));

}

printf("\n");
}
Queue CreateQueue (int maxsize)
{
Queue q;
q = (Queue)malloc(sizeof(Node));
q->data = (int*)malloc(sizeof(int)*maxsize);
q->front = 0;
q->rear = 0;
q->maxsize = maxsize;

int i;
for(i = 0;i < maxsize - 1;i++) {
EnQueue(q,i+1);
}

return q;
}

void EnQueue (Queue Q,int x)
{
Queue q = Q;
if (IsFull(q)) {
printf("The queue is full!\n");
}else {
q->data[q->rear] = x;
q->rear = (q->rear+1) % q->maxsize;
}
}

int DeQueue (Queue Q)
{
Queue q = Q;
if (IsEmpty(q)){
printf("The queue is empty!\n");
return -1;
}else {
int x = q->data[q->front];
q->front = (q->front+1) % q->maxsize;
return x;
}
}

int IsFull (Queue Q)
{
Queue q = Q;
return ((q->rear + 1) % q->maxsize) == q->front;
}

int IsEmpty (Queue Q)
{
Queue q;
return (q->front == q->rear);

}

 

由于时间问题和能力问题,函数的名字等等设计得不是很好,及代码也不是很完美,而且我一句注释都没有打,太懒了,还是不能拖到一天,该完成的就得当时完成。最后,你会发现,单链表,栈,队列他们都是运用的一种结构,只是插入的思路和输出的思路不同罢了,至于对代码的刨析,就不写了(时间问题),如果有读者看不懂的话,可以留言问我。今天就写到这里了,对于链式存储的队列,明天再写吧,哎,前面欠的债,躲得过初一,躲不过十五。加油,期待下一次!

上一篇:Java 队列模拟


下一篇:第三章_下(队列)