1. 程式人生 > >資料結構試驗四 棧與字串

資料結構試驗四 棧與字串

標頭檔案1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define MAXSIZE 100
typedef int datatype;
typedef struct
{
    datatype  a[MAXSIZE];
    int top;
}seqstack;
/**********************************/
/*函式名稱:init()		 */
/*函式功能:初始化空棧           */
/**********************************/
void init(seqstack *st)
{
    st->top=0;
}
/**********************************/
/*函式名稱:empty()		  */
/*函式功能:判斷棧是否為空        */
/**********************************/
int empty(seqstack *st)
{
    return st->top?0:1;
}
/**********************************/
/*函式名稱:read()	 	 */
/*函式功能:讀棧頂元              */
/**********************************/
datatype read(seqstack *st)
{
    if(empty(st)){
        printf("\n棧的空的!\n");
        exit(1);
    }
    else
        return st->a[st->top--];
}
/**********************************/
/*函式名稱:push()	 	 */
/*函式功能:進棧                  */
/**********************************/
void push(seqstack *st,datatype x)
{
    if(st->top==MAXSIZE){
        printf("棧滿,無法進棧!\n");
        exit(1);
    }
    st->a[st->top++]=x;
}
/**********************************/
/*函式名稱:pop()	 	*/
/*函式功能:出棧                  */
/**********************************/
datatype pop(seqstack *st)
{
    if(st->top==0){
        printf("\n順序棧是空的!\n");
        exit(1);
    }
    return st->a[--st->top];
}


/*
利用棧,實現十進位制整數m到十六進位制數的轉換功能
*/
/**********************************/
/*檔名稱:lab4_01.cpp           */
/**********************************/
#include"seqstack.h"

void Dto16(int m)
{
    seqstack s;			/*定義順序棧*/
    init(&s);
    char ch[20]={'0','1','2','3','4','5','6',
            '7','8','9','A','B','C','D','E','F'};
    printf("十進位制數%u對應的十六進位制數是:",m);
    if(m==0)
        s.a[s.top++]=ch[0];
    while(m)
    {
        s.a[s.top++]=ch[m%16];
        m/=16;
    }
    while(!empty(&s))
        putchar(s.a[--s.top]);
    printf("\n");
}
int main()
{
    int m;
    printf("請輸入待轉換的十進位制數:\n");
    while(scanf("%u",&m)!=EOF)
        Dto16(m);
    return 0;
}

標頭檔案2

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;

typedef int datatype;
typedef struct node
{
    datatype data;
    struct node *next;
}linknode;

typedef linknode *linkstack;
/**********************************/
/*函式名稱:init()		  */
/*函式功能:初始化空棧            */
/**********************************/
linkstack init()
{
    return NULL;
}
/**********************************/
/*函式名稱:empty()		  */
/*函式功能:判斷棧是否為空        */
/**********************************/
int empty(linkstack top)
{
    return top?0:1;
}
/**********************************/
/*函式名稱:read()	 	 */
/*函式功能:讀棧頂元               */
/**********************************/
datatype read(linkstack top)
{
    if(empty(top)){
        printf("\n棧的空的!\n");
        exit(1);
    }
    else
        return top->data;
}
/**********************************/
/*函式名稱:push()	 	 */
/*函式功能:進棧                   */
/**********************************/
linkstack push(linkstack top,datatype x)
{
    linkstack p;
    p=(linkstack)malloc(sizeof(linknode));
    p->data=x;

    p->next=top;
    top=p;
    return top;
}
/**********************************/
/*函式名稱:pop()	 	 */
/*函式功能:出棧                  */
/**********************************/
linkstack pop(linkstack top)
{
    linkstack p;
    if(empty(top)){
        printf("\n順序棧是空的!\n");
        exit(1);
    }
    p=top;
    top=top->next;
    free(p);
    return top;
}


/*
鏈式棧結構,實現十進位制無符號整數m到十六進位制數的轉換功能
*/
/**********************************/
/*檔名稱:lab4_02.cpp           */
/**********************************/
#include"linkstack.h"

void Dto16(int m)
{
    linkstack s;
    s=init();
    char ch[20]={'0','1','2','3','4','5','6',
        '7','8','9','A','B','C','D','E','F'};
    printf("十進位制數%u對應的十六進位制數是:",m);
    if(!m)
        s=push(s,ch[0]);
    while(m)
    {
        s=push(s,ch[m%16]);
        m/=16;
    }
    while(!empty(s))
    {
        putchar(read(s));
        s=pop(s);
    }
    puts("");
}

int main()
{
    unsigned int m;
    printf("請輸入待轉換的十進位制數:\n");
    while(scanf("%u",&m)!=EOF)
        Dto16(m);
    return 0;
}


標頭檔案4

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;

typedef char datatype;
typedef struct node
{
    datatype data;
    struct node *next;
}linknode;
typedef linknode *linkstring;

/**********************************/
/*函式功能:尾插法建立字元單鏈表  */
/**********************************/
linkstring creat()
{
    linkstring head,r,s;
    datatype x;
    head=r=(linkstring)malloc(sizeof(linknode));
    head->next=NULL;
    printf("請輸入一個字串(以回車結束):\n");
    scanf("%c",&x);
    while(x!='\n')
    {
        s=(linkstring)malloc(sizeof(linknode));
        s->data=x;
        r->next=s;
        r=s;
        scanf("%c",&x);
    }
    r->next=NULL;
    return head;
}

/**********************************/
/*函式功能:輸出字串            */
/**********************************/
void print(linkstring head)
{
    linkstring p;
    p=head->next;
    printf("List is:\n");
    while(p)
    {
        printf("%c",p->data);
        p=p->next;
    }
    printf("\n");
}

/*釋放單鏈表的內容*/
void delList(linkstring head)
{
    linkstring p=head;
    while(p)
    {
        head=p->next;
        free(p);
        p=head;
    }
}


/*
鏈式儲存結構linksrting.h檔案
在字串s中從第i個位置起取長度為len的子串,函式返回子串連結串列
*/
#include"linkstring.h"

linkstring substring(linkstring s,int i,int len)
{
    int cnt=0;
    s=s->next;
    while(cnt<i){
        s=s->next;
        cnt++;
    }

    cnt=0;
    linkstring temp,t1,t2;
    temp=t1=(linkstring)malloc(sizeof(linknode));
    temp->next=NULL;
    while(cnt<len&&s)
    {
        t2=(linkstring)malloc(sizeof(linknode));
        t2->data=s->data;   s=s->next;
        t1->next=t2;
        t1=t2;
        cnt++;
    }
    t1->next=NULL;
    return temp;
}

int main()
{
    linkstring str1,str2;
    str1=creat();                /*建字串連結串列*/
    print(str1);
    str2=substring(str1,3,5);    /*從第3個位置取長度為5的子串*/
    print(str2);                 /*輸出子串*/
    delList(str1);
    delList(str2);
    return 0;
}

這裡需要注意

/*
採用帶頭結點的連結串列儲存
在字串s中刪除從第i個位置開始,長度為len的子串
*/
/**********************************/
/*檔名稱:lab4_05.cpp           */
/**********************************/
#include"linkstring.h"

void delstring(linkstring s,int i,int len)
{
    int cnt=0;
    linkstring t1=s;
    while(cnt<i){
        s=s->next;
        cnt++;
    }


    cnt=0;
    //注意下面三行
    linkstring t2=s;
    s=s->next;
    linkstring temp=s;
    while(cnt<len)
    {
        s=temp->next;
        free(temp);
        temp=s;
        cnt++;
    }
    t2->next=s;
    s=t1;
}
int main()
{
    linkstring str;
    str=creat();            /*建字串連結串列*/
    print(str);
    delstring(str,2,3);     /*從第2個位置刪除長度為3的子串*/
    print(str);
    delList(str);
    return 0;
}