程式碼(前三章)

lookfd發表於2020-10-10

今天花了幾個小時才弄出來了,還是有點不熟練,不過萬幸的是終於可以自己手寫了,哈哈,功夫不負有心人,直接進入正題。

 

順序儲存結構的單連結串列:

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);

}

 

由於時間問題和能力問題,函式的名字等等設計得不是很好,及程式碼也不是很完美,而且我一句註釋都沒有打,太懶了,還是不能拖到一天,該完成的就得當時完成。最後,你會發現,單連結串列,棧,佇列他們都是運用的一種結構,只是插入的思路和輸出的思路不同罷了,至於對程式碼的刨析,就不寫了(時間問題),如果有讀者看不懂的話,可以留言問我。今天就寫到這裡了,對於鏈式儲存的佇列,明天再寫吧,哎,前面欠的債,躲得過初一,躲不過十五。加油,期待下一次!

相關文章