大连海事大学网络工程专业数据结构实验代码

大连海事大学网络工程专业数据结构实验代码

写代码是一个非常重要的锻炼自己的过程,希望大家一定要自己写,不要抄袭,并且注重数据结构与算法的学习,希望我们海大的计算机系同学们都有一个光明的未来。

上代码了。

第一个实验:多项式

/*
 @lizzy_0323
 翻版必究
 TODO:
 */
#include 
#include 
#include 
const double eps = 1e-8;
#define N 50
#define value(a,b) ((fabs((a) - (b)))<(eps)) 
typedef struct H{
     
    double coef;
    double expn;
    struct H *next;
}HLink;
static int Is0 = 0;
//用于创建链表
void Build(HLink *H)
{
     
    int i = 0;
    char a;
    //用于读取错误输入
    char wrong[100];
    HLink *p,*pend = NULL; 
    while(a != '#')
    {
     
        if((p = (HLink *)malloc(sizeof(HLink))) == NULL) 
        {
     
            printf("内存分配失败!");
            exit(0);
        }
        if(H->next == NULL)  
        {
     
            H->next = p;   
            pend = p;    
        }
        else     
        {
     
            pend->next = p;    
            pend = p;           
        }
        printf("请输入第%d项的次数:", i + 1);
        while(!(scanf("%lf", &p->expn)))
        {
     
            fgets(wrong,N,stdin);    
            printf("请以正确的格式录入");
        }
        fgets(wrong,N,stdin);
        printf("请输入第%d项的系数为:",i + 1);
        while(!(scanf("%lf", &p->coef)))
        {
     
            fgets(wrong,N,stdin);    
            printf("请以正确的格式录入");
        }
        fgets(wrong,N,stdin);
        printf("是否还要继续输入?(#号结束)");
        a = getchar();  //读取用户输入
        fgets(wrong,N,stdin);
        i++;
    }
    p->next = NULL;
}
//节点形式输出
void OutOnenode(HLink *H)
{
     
    HLink *p = NULL;    
    if(H->next == NULL)
    {
     
        if(Is0 == 0)
        {
     
            printf("当前多项式为空");
        }
        else
        {
     
            printf("次数为:0 系数为:0\n\n");
        }
    }
    p = H->next;      
    while(p != NULL)
    {
     
        printf("次数为:%.2f,系数为:%.2f\n\n",p->expn, p->coef);
        p = p->next;  
    }
}
//以多项式格式输出
void Output(HLink *H)
{
     
    HLink *p = NULL;    
    if(H->next == NULL)
    {
     
        if(Is0 == 0)
        {
     
            printf("当前多项式为空!");
        }
        else
        {
     
            printf("0");
        }
        return;
    }
    p = H->next;      
    if(value(p->expn,1))  
    {
     
        if(value(p->coef,1))  
        {
     
            printf("x");    
        }
        else if(value(p->coef,-1)) 
        {
     
           printf("-x");
        }
        else
        {
     
            printf("%.2fx",p->coef);
        }
    }
    else if(value(p->expn,0)) 
    {
     
        printf("%.2f",p->coef);
    }
    else
    {
     
        if(value(p->coef,1)) 
        {
     
            printf("x^%.2f",p->expn);  
        }
        else if(value(p->coef,-1)) 
        {
     
           printf("-x^%.2f",p->expn);
        }
        else
        {
     
            printf("%.2fx^%.2f",p->coef, p->expn);
        }
    }
    p = p->next;    
    while(p != NULL) 
    {
     
        if(p->coef > 0)  
        {
     
            printf("+"); 
            if(value(p->expn,1))
            {
     
                if(value(p->coef,1))
                {
     
                    printf("x");
                }
                else
                {
     
                    printf("%.2fx",p->coef);
                }
            }
            else if(value(p->expn,0))
            {
     
                printf("%.2f",p->coef);
            }
            else
            {
     
                if(value(p->coef,1))
                {
     
                    printf("x^%.2f",p->expn);
                }
                else
                {
     
                    printf("%.2fx^%.2f",p->coef,p->expn);
                }
            }
        }
        else    //如果系数为小于0
        {
     
            printf("-"); 
            if(value(p->expn,1))
            {
     
                if(value(p->coef,-1))
                {
     
                    printf("x");
                }
                else
                {
     
                    printf("%.2fx",(-(p->coef)));
                }
            }
            else if(value(p->expn,0))
            {
     
                printf("%.2f",(-(p->coef)));
            }
            else
            {
     
                if(value(p->coef,-1))
                {
     
                    printf("x^%.2f",p->expn);
                }
                else
                {
     
                    printf("%.2fx^%.2f",(-(p->coef)), p->expn); 
                }
            }
        }
        p = p->next;
    }
}
//相加函数
void AddNode(HLink *H,HLink H1,HLink H2)
{
     
    HLink *p,*q,*temp,*pend = NULL;
    if(H1.next == NULL && H2.next == NULL)  
    {
     
        return;
    }
    p = H1.next;
    q = H2.next;
    while(p && q)
    {
     
        //为临时节点分配内存
        if((temp = (HLink *)malloc(sizeof(HLink))) == NULL) 
        {
     
            printf("内存分配失败!");
            exit(0);
        }
        //如果当前节点为空
        if(H->next == NULL)
        {
     
            H->next = temp;
            pend = temp;
        }
        if(p->expn > q->expn)
        {
     
            *temp = *p;
            p = p->next;
        }
        else if(p->expn == q->expn)
        {
     
            *temp = *p;
            temp->coef = p->coef + q->coef;
            p = p->next;
            q = q->next;
        }
        else
        {
     
            *temp = *q;
            q = q->next;
        }
        pend->next = temp;
        pend = temp;
    }
    while(p) 
    {
     
        if((temp = (HLink *)malloc(sizeof(HLink))) == NULL) 
        {
     
            printf("内存分配失败!");
            exit(0);
        }
        *temp= *p;
        if(H->next == NULL) 
        {
     
            H->next =temp;
        }
        else
        {
     
            pend->next = temp;
        }
        pend = temp;
        p = p->next;
    }
    while(q)    
    {
     
        if((temp = (HLink *)malloc(sizeof(HLink))) == NULL) 
        {
     
            printf("内存分配失败!");
            exit(0);
        }
        *temp = *q;
        if(H->next == NULL)
        {
     
            H->next = temp;
        }
        else
        {
     
            pend->next = temp;
        }
        pend = temp;
        q = q->next;
    }
    temp->next = NULL; 
}
//相乘函数
void MultH(HLink *H,HLink H1,HLink H2)
{
     
    HLink *p,*q,*r,*pend = NULL;
    q = H1.next;
    //如果有空节点,直接返回
    if(H1.next == NULL || H2.next == NULL)
    {
     
        return;
    }
    while(q)
    {
     
        r = H2.next;
        while(r)
        {
     
            if((p = (HLink *)malloc(sizeof(HLink))) == NULL) 
            {
     
                printf("内存分配失败!");
                exit(0);
            }
            if(H->next == NULL)
            {
     
                H->next = p; 
                pend = p; 
            }
            else 
            {
     
                pend->next = p;    
                pend = p;          
            }
            p->expn = r->expn + q->expn;     
            p->coef = (r->coef) * (q->coef);  
            r = r->next;    
        }
        q = q->next; 
    }
    p->next = NULL; 
}
//合并函数
void Union(HLink *H)
{
     
    HLink *now,*pend,*p = NULL;
    if(H->next == NULL) 
    {
     
        return;
    }
    now = H->next;
    pend = now->next;
    while(pend)
    {
     
        if(now->expn == pend->expn) 
        {
     
            now->coef += pend->coef;   
            p = pend;                    
            pend = pend->next;        
            now->next = pend;           
            free(p);                       
        }
        else    
        {
     
            now = now->next;     
            pend = pend->next;
        }
    }
}
//多项式排序函数
void Swap(HLink *H)
{
     
    HLink *pre, *p, *pend,*phead=H->next;
    //如果当前多项式为空
    if(H->next == NULL)
    {
     
        return;
    }
    p=phead->next; 
    phead->next = NULL;   
    //当前节点不为空
    while(p != NULL)
    {
     
        pend = p->next;      
        pre = H;     
        while(pre->next != NULL && ((pre->next->expn) > (p->expn)))   
        {
     
            pre = pre->next;
        }
        p->next = pre->next;  
        pre->next = p;       
        p = pend;   
    }
}
//检查是否有清零的函数
void Check0(HLink *H)
{
     
    HLink *pre,*p,*pend = NULL; 
    pre = H;
    p = H->next;
    while(p != NULL)
    {
     
        if(p->coef == 0)  
        {
     
            pre->next = p->next;
            pend = p;
            p = p->next;
            free(pend);
            Is0 = 1;
        }
        else   
        {
     
            p = p->next;
            pre = pre->next;
        }
    }
}
//清除多项式函数
void Check(HLink *H)
{
     
    HLink *p,*pend = NULL;
    p = H->next;
    while(p)
    {
     
        pend = p;
        p = p->next;
        free(pend);
    }
    H->next = NULL;
}
//用于显示菜单
void Menu()
{
     
    printf("请输入序号选择功能:\n");
    printf("1、录入新的多项式\n");
    printf("2、显示多项式各项次数为和对应系数\n");
    printf("3、输出多项式表达式\n");
    printf("4、多项式加法\n");
    printf("5、多项式乘法\n");
    printf("0、退出\n");
}
//主函数
int main()
{
     
    int  button;
    int j = 0;   
    double expn;    
    char pend[100];    
    int index[100] = {
     0};   
    //指针数组
    HLink *pt[100] = {
     NULL};  
    HLink *H1,*H2,*q,*r,*e = NULL; 
    //对指针进行初始化
    H1 = (HLink *)malloc(sizeof(HLink));
    H1->next = NULL;
    H2 = (HLink *)malloc(sizeof(HLink));
    H2->next = NULL;
    q = (HLink *)malloc(sizeof(HLink));
    q->next = NULL;
    e = (HLink *)malloc(sizeof(HLink));
    e->next = NULL;
    while(1)
    {
     
        //打印菜单
        Menu();
        scanf("%d", &button);
        switch(button)
        {
     
        case 1:
            {
     
                char a;
                for(int index = 0;index < j;index++)
                {
     
                    Check(pt[index]); 
                }
                j = 0; 
                //读取用户输入
                do 
                {
     
                    if((pt[j] = (HLink *)malloc(sizeof(HLink))) == NULL) 
                    {
     
                        printf("内存分配失败!");  //如果分配失败
                        exit(0);
                    }
                    pt[j]->next = NULL;  
                    printf("当前为第%d个多项式\n",j + 1); 
                    Build(pt[j]);  
                    Check0(pt[j]);    
                    Swap(pt[j]);         
                    Union(pt[j]);  
                    j++; 
                    printf("是否还要继续录入?(#号结束)");
                    a = getchar();
                    fgets(pend,N,stdin);
                } while(a != '#');   //输入#时结束
                printf("\n");
                break;
            }
        case 2:
            {
     
                if(j == 0 && H1->next == NULL && H2->next ==NULL)
                {
     
                    printf("没有录入任何数据!\n");
                }
                else
                {
     
                    for(int k = 0;k < j;k++)
                    {
     
                        printf("----当前为第%d个多项式--------\n",k+1);
                        OutOnenode(pt[k]);
                    }
                    if(H1->next != NULL)
                    {
     
                        printf("--------多项式1为--------\n");
                        OutOnenode(H1);
                    }
                    if(H2->next != NULL)
                    {
     
                        printf("--------多项式2为--------\n");
                        OutOnenode(H2);
                    }
                }
                break;
            }
        case 3:
            {
     
                if(j == 0 && H1->next == NULL && H2->next ==NULL)
                {
     
                    printf("没有录入任何数据!\n");
                }
                else
                {
     
                    for(int k = 0;k < j;k++)
                    {
     
                        printf("-----第%d个多项式--------\n",k+1);
                        Output(pt[k]);
                    }
                    if(H1->next != NULL)
                    {
     
                        printf("--------多项式1为--------\n");
                        Output(H1);
                    }
                    if(H2->next != NULL)
                    {
     
                        printf("--------多项式2为--------\n");
                        Output(H2);
                    }
                }
                break;
            }
        case 4:
            {
     
                if(j == 0)
                {
     
                    printf("当前无多项式录入!\n");
                    break;
                }
                printf("当前共有%d个多项式\n",j);
                int k;
                for(k = 0;k < j;k++)
                {
     
                    printf("第%d个多项式:",k + 1);
                    Output(pt[k]);
                    printf("\n");
                }
                printf("请输入需要相加的多项式序号,输入0后结束:");
                k = 0;
                Check(q);
                while(index[k] != 0)
                {
     
                    if(k % 2 == 0)
                    {
     
                        Check(H1);
                        AddNode(H1,*q,*pt[index[k] - 1]);
                        Check0(H1);    
                    }
                    else
                    {
     
                        Check(q);
                        AddNode(q,*H1,*pt[index[k] - 1]);
                        Check0(q);    
                    }
                    k++;
                }
                if(k % 2 == 0)
                {
     
                    Check(H1);
                    H1->next = q->next;
                }
                else
                {
     
                    Check(q);
                }
                printf("--------相加后的多项式为--------\n");
                Output(H1);
                break;
            }
        case 5:
            {
     
                int k = 0;
                if(j == 0)
                {
     
                    printf("当前无多项式录入!\n");
                    break;
                }
                printf("当前共有%d个多项式",j);
                for(k = 0;k < j;k++)
                {
     
                    printf("第%d个多项式:",k + 1);
                    Output(pt[k]);
                    printf("\n");
                }
                printf("请输入需要相乘的多项式序号,(输入0后结束输入):");
                k = 0;
                if(index[0] == 0) 
                {
     
                    printf("当前没有选择多项式\n");
                    break;
                }
                Check(e);
                if((r = (HLink *)malloc(sizeof(HLink))) == NULL) 
                {
     
                    printf("内存分配失败!");
                    exit(0);
                }
                r->expn = 0;
                r->coef = 1;
                e->next = r;
                r->next = NULL;
                k = 0;
                while(index[k] != 0)
                {
     
                    if(k % 2 == 0)
                    {
     
                        Check(H2);
                        MultH(H2,*e,*pt[index[k] - 1]);
                        Swap(H2);         
                        Union(H2);        
                    }
                    else
                    {
     
                        Check(e);
                        MultH(e,*H2,*pt[index[k] - 1]);
                        Swap(e);         
                        Union(e);        
                    }
                    k++;
                }
                if(k % 2 == 0)
                {
     
                    Check(H2);
                    H2->next = e->next;
                }
                else
                {
     
                    Check(e);
                }
                printf("-------相乘后的多项式为--------\n");
                Output(H2);
                break;
            }
        case 0:
        {
     
            printf("程序已安全退出\n");
            exit(0);
            break;
        }
        default:
        {
     
        printf("输入错误,请重新输入!\n\n");
        break;
        }
        }
    }
    return 0;
}









第二个实验:字符串匹配算法:

/*
 @lizzy_0323
 FOR
 Real
 翻版必究
 TODO:
 */
#include 
#include
#include
#define N 50
char wrong[N];//用于储存错误输入
char c1[N][N], c2[N][N];//用于储存两个字符串
void Out()
{
     
	printf("请问您是否要现在退出程序?\n");
	printf("1-是 2-不是\n");
	int t;//用于判断用户当前是否要进行退出
	scanf("%d",&t);
	if(t==1)
	{
     
		printf("程序已安全退出\n");
		exit(0);
	}
	else
	{
     
		return;
	}
	
}
void Menu()
{
     
	printf("*****************************\n");
	printf("1.输入主串,子串和匹配起始位置\n");
	printf("2.朴素的模式匹配算法\n");
	printf("3.KMP算法(Next[])\n");
	printf("4.KMP算法(Nextval[])\n");
	printf("5.修改主串或者子串\n");
	printf("6-删除字符串\n");
	printf("0.退出管理系统\n");
	printf("*****************************\n");
}
void getString2(int c)
{
     
	fgets(c2[c],20,stdin);
	int i=strlen(c2[c]);
	if(c2[c][i-1]=='\n') //因为最后一行没有回车符
    {
     
        c2[c][i-1]='\0';
	}
}
void getString1(int c)
{
     
	fgets(c1[c],20,stdin);
	int i=strlen(c2[c]);
	if(c1[c][i-1]=='\n') //因为最后一行没有回车符
    {
     
        c1[c][i-1]='\0';
	}
}
//朴素的模式匹配算法
int BF(char* a, char* b, int pos)
{
     
	int length1, length2;
	length1 = strlen(a)-1;
	length2 = strlen(b)-1;
	int i = pos, j = 0, k = 0;
	int count = 1;//记录比较次数
	bool is1 = true;
	bool isIn=true;
	int num[N];
	for (i = 0; i <= length2; i++)//先为num数组赋0
	{
     
		num[i] = 0;
	}
	i = 0;
	printf("当前主串是%s\n", a);
	printf("当前子串是%s\n", b);
	while (i <= length1  && j <= length2 )//如果匹配没到末尾
	{
     
		if (count == 1 && is1 == true)
		{
     
			printf("当前为第%d趟\n", count);
		}
		num[j] += 1;
		if (a[i] == b[j])//如果当前字符匹配相同,继续匹配后续字符
		{
     
			printf("%c,%c\n", a[i], b[j]);
			i++;
			j++;
			is1 = false;
			isIn=true;
		}
		else
		{
     
			printf("%c,%c\n", a[i], b[j]);
			i = i - j + 1;
			j = 0;//回到初始位置
			isIn=false;
			if(a[i]!='\0'&&a[i]!='\n')
			{
     
			count++;
			printf("当前为第%d趟\n", count);
			}
			//printf("当前为第%d趟\n", count);
		}
	}
	printf("匹配结束\n");
	for (k = 0; k <= length2; k++)//先为num数组赋0
	{
     
		printf("模式串第%d个字符匹配的次数是%d\n", k+1, num[k]);
	}
	if (j >= length2&&isIn==true)//匹配成功
	{
     
		printf("匹配总趟数是%d\n", count);
		return i - length2;//返回匹配成功的位置
	}
	else
	{
     
		printf("匹配总趟数是%d\n", count);
		return 0;
	}
}
//获取next数组
void Get_next(char* b, int* next)
{
     
	int j = 0, k = -1;
	next[j] = k;
	int length = strlen(b)-1;
	while (j <= length)
	{
     
		if (k == -1 || b[j] == b[k])//如果回到了第一个字符或匹配成功
		{
     
			j++;
			k++;
			next[j] = k;
		}
		else//如果匹配失败
		{
     
			k = next[k];
		}
	}
	return;
}
//获取nextval数组
void Get_nextval(char* b, int* nextval)
{
     
	int j = 0, k = -1;
	nextval[j] = k;
	int length = strlen(b)-1;
	while (j<=length)
	{
     
		if (k == -1 || b[j] == b[k])//如果回到了第一个字符或匹配成功
		{
     
			j++;
			k++;
			if (b[j] != b[k])
			{
     
				nextval[j] = k;
			}
			else
			{
     
				nextval[j] = nextval[k];
			}
		}
		else//如果匹配失败
		{
     
			k = nextval[k];
		}
	}
	return;
}
//KMP算法
int KMP(char* a, char* b, int pos)
{
     
	int length1, length2;
	length1 = strlen(a)-1;
	length2 = strlen(b)-1;
	int i = pos, j = 0, k = 0;
	int next[N];//定义一个next数组
	int num[N];
	bool is1 = true;
	bool isIn=true;
	printf("当前主串是%s\n", a);
	printf("当前子串是%s\n", b);
	Get_next(b, next);
	int count = 1;//记录比较次数
	for (i = 0; i <=length2; i++)//先为num数组赋0
	{
     
		num[i] = 0;
	}
	i = 0;
	while (i <= length1 && j <= length2 )//如果匹配没到末尾
	{
     
		if (count == 1 && is1 == true)
		{
     
			printf("当前为第%d趟\n", count);
		}
		if (j == -1 || a[i] == b[j])//如果当前字符匹配相同或第一位就不同,继续匹配后续字符
		{
     
			if (j != -1)
			{
     
				num[j] += 1;
				printf("%c,%c\n", a[i], b[j]);
			}
			else
			{
     
				num[0] += 1;
			}
			i++;
			j++;
			is1 = false;
			isIn=true;
		}
		else
		{
     
			printf("%c,%c\n", a[i], b[j]);
			j = next[j];//回到数组指向的位置
			isIn=false;
			if(a[i]!='\0'&&a[i]!='\n')
			{
     
			count++;
			printf("当前为第%d趟\n", count);
			}
		}
	}
	printf("匹配结束\n");
	for (k = 0; k <=length2; k++)
	{
     
		printf("模式串第%d个字符匹配的次数是%d\n", k+1, num[k]);
	}
	if (j >= length2&&isIn==true)//匹配成功
	{
     
		printf("匹配总趟数是%d\n", count);
		return i - length2;//返回匹配成功的位置
	}
	else
	{
     
		printf("匹配总趟数是%d\n", count);
		return 0;
	}
}
//改进的KMP算法
int KMP_Nextval(char* a, char* b, int pos)
{
     
	int length1, length2;
	length1 = strlen(a) - 1;
	length2 = strlen(b) - 1;
	int i = pos, j = 0, k = 0;
	int nextval[N];//定义一个next数组
	int num[N];
	bool isIn=true;
	bool is1 = true;
	printf("当前主串是%s\n", a);
	printf("当前子串是%s\n", b);
	Get_nextval(b, nextval);
	int count = 1;//记录比较次数
	for (i = 0; i <= length2; i++)//先为num数组赋0
	{
     
		num[i] = 0;
	}
	i = 0;
	while (i <= length1 && j <= length2)//如果匹配没到末尾
	{
     
		if (count == 1 && is1 == true)
		{
     
			printf("当前为第%d趟\n", count);
		}
		if (j == -1 || a[i] == b[j])//如果当前字符匹配相同或第一位就不同,继续匹配后续字符
		{
     
			if (j != -1)
			{
     
				num[j] += 1;
				printf("%c,%c\n", a[i], b[j]);
			}
			else
			{
     
				num[0] += 1;
			}
			i++;
			j++;
			is1 = false;
			isIn=true;
		}
		else
		{
     
			printf("%c,%c\n", a[i], b[j]);
			j = nextval[j];//回到数组指向的位置
			isIn=false;
			if(a[i]!='\0'&&a[i]!='\n')
			{
     
			count++;
			printf("当前为第%d趟\n", count);
			}
		}
	}
	printf("匹配结束\n");
	for (k = 0; k <= length2; k++)//先为num数组赋0
	{
     
		printf("模式串第%d个字符匹配的次数是%d\n", k + 1, num[k]);
	}
	if (j >= length2&&isIn==true)//匹配成功
	{
     
		printf("匹配总趟数是%d\n", count);
		return i - length2;//返回匹配成功的位置
	}
	else
	{
     
		printf("匹配总趟数是%d\n", count);
		return 0;
	}
}
//用于删除字符串
void Delete()
{
     
	int button;
	printf("请输入要删除的字符串类型\n");
	printf("1-主串 2-子串\n");
	while (scanf("%d", &button) != 1)
	{
     
		fgets(wrong,20,stdin);
		printf("输入不合法,请重新输入\n");
	}
	//如果删除主串
	if(button==1)
	{
     
	printf("请输入要删除的主串位置\n");
	while (scanf("%d", &button) != 1)
	{
     
		fgets(wrong,20,stdin);
		printf("输入不合法,请重新输入\n");
	}
	c1[button][0]='\0';
	}
	//如果删除子串
	if(button==2)
	{
     
	printf("请输入要删除的子串位置\n");
	while (scanf("%d", &button) != 1)
	{
     
		fgets(wrong,20,stdin);
		printf("输入不合法,请重新输入\n");
	}
	c2[button][0]='\0';
	}
}
int main()
{
     
	//显示用户菜单
	int button;

	int index = 1;//用于储存匹配起始位置
	int ans1=0, ans2=0, ans3=0;
	int a = 0;
	int x = 0;
	while (a < 100)//继续循环的条件
	{
     
		Menu();
		while (scanf("%d", &button) != 1)
		{
     
			fgets(wrong,20,stdin);
			printf("输入不合法,请重新输入\n");
		}
		switch (button)
		{
     
		case(0):
			printf("管理系统已退出!\n");
			a = 100;
			break;
		case(1):
			printf("请输入要储存的位置(1-100)\n");
			while (scanf("%d", &x) != 1)
			{
     
				fgets(wrong,20,stdin);
				printf("输入不合法,请重新输入\n");
			}
			getchar();
			printf("请输入主串\n");
			getString1(x);
			printf("请输入子串\n");
			getString2(x);
			while (strlen(c1[x]) < strlen(c2[x]))//如果子串长度大于主串
			{
     
				printf("子串长度大于主串,请重新输入正确的子串和主串\n");
				printf("请输入主串\n");
				scanf("%s", c1[x], 100);
				printf("请输入子串\n");
				getString2(x);
			}
			printf("请输入匹配起始位置\n");
			while ((scanf("%d", &index) != 1))//如果输入错误或者匹配位置大于主串位置
			{
     
				printf("请输入正确的起始位置\n");
				fgets(wrong,20,stdin);
			}
			if(index>strlen(c1[x]))
			{
     
			printf("匹配位置大于主串长度请输入正确的起始位置\n");
			while ((scanf("%d", &index) != 1))//如果输入错误或者匹配位置大于主串位置
			{
     
				printf("请输入正确的起始位置\n");
				fgets(wrong,20,stdin);
			}
			}
			Out();
			break;
		case(2):
			printf("请输入要进行匹配字符串的位置(1-100)\n");
			while (scanf("%d", &x) != 1)
			{
     
				fgets(wrong,20,stdin);
				printf("输入不合法,请重新输入\n");
			}
			while (strlen(c1[x])>N)
			{
     
				printf("您输入的位置当前无字符串储存\n");
				printf("请重新输入要进行匹配的位置(1-100)\n");
				while (scanf("%d", &x) != 1)
				{
     
					fgets(wrong,20,stdin);
					printf("输入不合法,请重新输入\n");
				}
			}
			ans1 = BF(c1[x], c2[x], index - 1);
			if (ans1 == 0)
			{
     
				printf("匹配失败\n");
			}
			else
			{
     
				printf("匹配成功,成功的位置序号是%d\n", ans1);
			}
			Out();
			break;
		case(3):
			printf("请输入要进行匹配的位置(1-100)\n");
			while (scanf("%d", &x) != 1)
			{
     
				fgets(wrong,20,stdin);
				printf("输入不合法,请重新输入\n");
			}
			if (strlen(c1[x]) >= 1)//如果有字符
			{
     
				ans2 = KMP(c1[x], c2[x], index - 1);
			}
			else
			{
     
				printf("您输入的位置当前无字符串储存\n");
				break;
			}
			if (ans2 == 0)
			{
     
				printf("匹配失败\n");
			}
			else
			{
     
				printf("匹配成功,成功的位置序号是%d\n", ans2);
			}
			Out();
			break;
		case(4):
			printf("请输入要进行匹配的位置(1-100)\n");
			while (scanf("%d", &x) != 1)
			{
     
				fgets(wrong,20,stdin);
				printf("输入不合法,请重新输入\n");
			}
			if (strlen(c1[x]) >= 1)//如果有字符,进行匹配
			{
     
				ans3 = KMP_Nextval(c1[x], c2[x], index - 1);
			}
			else
			{
     
				printf("您输入的位置当前无字符串储存\n");
				break;
			}
			if (ans3 == 0)
			{
     
				printf("匹配失败\n");
			}
			else
			{
     
				printf("匹配成功,成功的位置序号是%d\n", ans3);
			}
			Out();
			break;
		case(5):
		{
     
			printf("请输入要修改的串类型\n");
			printf("1-主串,2-子串\n");
			int b,c;
			while (scanf("%d", &b) != 1)
			{
     
				fgets(wrong,20,stdin);
				printf("输入不合法,请重新输入\n");
			}
			if(b==1)
			{
     
				printf("请输入要修改的主串位置\n");
				while (scanf("%d", &c) != 1)
			    {
     
				fgets(wrong,20,stdin);
				printf("输入不合法,请重新输入\n");
			    }
				printf("请输入修改后的字符串\n");
				getchar();
				getString1(c);
				break;
			}
			if(b==2)
			{
     
				printf("请输入要修改的子串位置\n");
				while (scanf("%d", &c) != 1)
			    {
     
				fgets(wrong,20,stdin);
				printf("输入不合法,请重新输入\n");
			    }
				printf("请输入修改后的字符串\n");
				getchar();
				getString2(c);
				break;
			}
			else
			{
     
				printf("当前无修改操作\n");
			}
			Out();
			break;
		}
		case(6):
		{
     
			Delete();
			break;
		}
		default:
			printf("无对应选项,请重新输入\n");
		    break;
		}
	}
	return 0;
}


第三个实验:二叉树

/*
 @lizzy_0323
 翻版必究
 TODO:
 */
#include 
#include 
#include
#include
#define N 50
using namespace std;
int a[N];
//用于记录是否有重复节点
int bp=1;
static int num1=0,num2=0,num3=0;//用于计算
//定义树结构
struct TreeNode {
	struct TreeNode * LeftNode;
	string data;
	struct TreeNode * RightNode;
};
//用于判断输入是否正确
int  Judgeint(int x)
{
	int i;
	char wrong[N];
	while (1)
	{
		if (scanf("%d", &i) == 0)
		{
			printf("输入错误请 重新输入 Error类型为 : %d\n", x);
			fgets(wrong,N,stdin);
		}
		else
		{
			return i;
		}
	}
}
//用于显示菜单
void Menu()
{
	printf("____________________________________________\n");
	printf("| 1.创建二叉树                              |\n");
	printf("| 2.添加节点                                |\n");
	printf("| 3.打印前序遍历结果                        |\n");
	printf("| 4.打印中序遍历结果                        |\n");
	printf("| 5.打印后序遍历结果                        |\n");
	printf("| 6.打印层序遍历结果                        |\n");
	printf("| 7.求根到给定节点的路径                    |\n");
    printf("| 8.退出                                    |\n");
	printf("|___________________________________________|\n");
	return;
}
//添加节点
void AddNode(TreeNode ** tree, string value)
{
	if (*tree == NULL)
	{
		//为指针节点分配一段地址
		*tree = (TreeNode*)malloc(sizeof(TreeNode));
		if (*tree != NULL)
		{
			(*tree)->LeftNode = NULL;
			(*tree)->data=value;
			(*tree)->RightNode = NULL;
		}
		else
		{
			printf("内存分配失败\n");
		}
	}
	else
	{
		if (value <= (*tree)->data)
			AddNode(&((*tree)->LeftNode), value);
		if (value > (*tree)->data)
			AddNode(&((*tree)->RightNode), value);
	}
}
//创建二叉树
void CreatTree(TreeNode **Tree)
{
	string x;
    int num=0;
    printf("请输入该二叉树的节点个数(1-20)\n");
    num=Judgeint(1);
    while(num>20)
    {
        printf("请重新输入该二叉树的节点个数(1-20)\n");
        num=Judgeint(1);   
    }
    *Tree=NULL;
    for(int i=0;idata).c_str());
	FirPut(tree->LeftNode);
	FirPut(tree->RightNode);
}
//中序遍历
void SecPut(TreeNode * tree)
{
	if (tree == NULL)
	{
		if(num2==0)
		{
			printf("该二叉树没有节点储存\n");
		}
		return;
	}
	num2++;
	SecPut(tree->LeftNode);
	printf("%s ", (tree->data).c_str());
	SecPut(tree->RightNode);
}
//后序遍历
void ThrPut(TreeNode * tree)
{
	if (tree == NULL)
	{
		if(num3==0)
		{
			printf("该二叉树没有节点储存\n");
		}
		return;
	}
	num3++;
	ThrPut(tree->LeftNode);
	ThrPut(tree->RightNode);
	printf("%s ", (tree->data).c_str());
} 
//层序遍历
void LevPut(TreeNode * tree)
{
	//定义队列结构
    struct Queue
   {
	TreeNode * data;
	struct Queue * Next;
   };
	Queue *phead, *pend, *pnew;
	phead = (Queue*)malloc(sizeof(Queue));
	phead->data = tree;
	phead->Next = NULL;
	pend = phead;
	do
	{
		TreeNode * Temp = NULL;
		Temp = phead->data;
		//head = head->Next;
		if (Temp->LeftNode != NULL)
		{
			//申请一个临时节点
			pnew = (Queue*)malloc(sizeof(Queue));
			pnew->data = NULL;
			pnew->Next = NULL;
			pnew->data = Temp->LeftNode;
			pend->Next = pnew;
			pend = pend->Next;
		}
		if (Temp->RightNode != NULL)
		{
			pnew = (Queue*)malloc(sizeof(Queue));
			pnew->data = NULL;
			pnew->Next = NULL;
			pnew->data = Temp->RightNode;
			pend->Next = pnew;
			pend = pend->Next;
		}
		printf("%s ", Temp->data.c_str());
		//头节点出队
		phead = phead->Next;
	} while (phead != NULL);
}
//用于判断是哪棵树
int  WhichTree()
{
    printf("请输入要进行操作的二叉树位置(1-20)\n");
    int index=Judgeint(1);
    while(index>20)
    {
        printf("请重新输入要进行操作的二叉树位置(1-20)\n");
        index=Judgeint(1);
    }
    return index;
}
//求根节点到该节点的路径
bool ShowPath(TreeNode*tree,string value)
{
	TreeNode * Node = tree;
	int i=0;
	while (Node != NULL && Node->data != value)
	{
		if (value < Node->data)
		{
			Node = Node->LeftNode;
			a[i]=1;
		}
		else
		{
			Node = Node->RightNode;
			a[i]=2;
		}
		i++;
	}
	if (Node == NULL)
		return false;
	//如果找到了节点
	else
	{
	//用于统计重复节点的个数
	while(Node!=NULL&&Node->LeftNode!=NULL&&Node->LeftNode->data==Node->data)
	{
		bp++;
		Node=Node->LeftNode;
	}
	return true;
	}
}
//主函数部分
int main()
{
	TreeNode * tree[N]={NULL};//定义一个树的数组
	int x;
    int index;
	while (1)
	{
        Menu();
		switch (Judgeint(1))
		{
		case 1: 
		{
			CreatTree(&tree[index=WhichTree()]);
			break;
		}
		case 2:	
        {
            string i;
			index=WhichTree();
			printf("请输入要插入节点的值\n");
			i=Judgeint(2);
			AddNode(&tree[index], i);
			break;
        }
		case 3:
        {
			printf("前序遍历 :");
			FirPut(tree[index=WhichTree()]);
			printf("\n");
			break;
        }
		case 4:
        {
            printf("中序遍历 : ");
			SecPut(tree[index=WhichTree()]);
			printf("\n");
			break;
        }
		case 5:
        {
            printf("后序遍历 : ");
			ThrPut(tree[index=WhichTree()]);
			printf("\n");
			break;
        }
		case 6:
        {
            printf("层序遍历 : ");
			LevPut(tree[index=WhichTree()]);
			printf("\n");
			break;
        }
		case 7:
        {
			int j=0;
			string newData;
			index=WhichTree();
			printf("请输入要查找的节点所存储的数据\n");
			newData.resize(N);
			scanf("%s",&newData[0]);
			if(ShowPath(tree[index],newData))//如果查找成功
			{
				int i1=1;
				do
				{
				printf("以下为路径%d\n",i1);
				TreeNode *temptree=tree[index];
				for(int i=0;a[i]!=0;i++)
				{
					cout<<" "<data ;
					if(a[i]==1)//如果指向左端
					{
						temptree=temptree->LeftNode;
					}
					if(a[i]==2)//如果指向右端
					{
						temptree=temptree->RightNode;
					}
				}
				for(int k=i1;k<=bp;k++)
				{
				cout<data<

你可能感兴趣的:(数据结构,算法,数据结构,字符串,c++,c语言)