コード例 #1
0
void LineEdit()
{
	SqStack s;
	char ch;

	InitStack(s);
	printf("请输入一个文本文件,^Z结束输入:\n");
	ch = getchar();
	while (ch != EOF) {
		while (ch != EOF && ch != '\n') {
			switch (ch) {
			case '#': if (!StackEmpty(s))
					Pop(s, ch);
				break;
			case '@': ClearStack(s);
				break;
			default: Push(s, ch);
			}
			ch = getchar();
		}
		StackTraverse(s, copy);
		fputc('\n', fp);
		ClearStack(s);
		if (ch != EOF)
			ch = getchar();
	}
	DestroyStack(s);
}
コード例 #2
0
ファイル: Algorithm.cpp プロジェクト: awyd234/DS_yan
void LineEdit(){
	// P50 算法3.2 行编辑程序
	// #:退格 @:退行
	// 利用字符栈S,从终端接收一行并传送至调用过程的数据区
	SqStack S;
	InitStack(S);
	SElemType e;
	char ch = getchar();
	while (ch != EOF){
		while (ch != EOF && ch != '\n'){
			switch (ch){
				case'#': 
					Pop(S, e);
					break;
				case'@':
					ClearStack(S);
					break;
				default:
					Push(S, ch);
					break;
			}
			ch = getchar();	//从终端接收下一个字符
		}
		StackTraverse(S, Visit_Display_Char);
		ClearStack(S);
		if (ch != EOF) ch = getchar();
	}
	DestroyStack(S);
}// LineEdit
コード例 #3
0
ファイル: proj3_1.cpp プロジェクト: LewiesCheng/Code
int main()
{
    SqStack S;
    printf("(1)��ʼ��ջ��\n");
    InitStack (S);
    printf("(2)�ж�ջs�Ƿ�ǿգ�");
    if(StackEmpty(S))
        printf("�ǿ�\n");
    else printf("��\n");
    printf("(3)���ν�ջԪ��a,b,c,d,e:\n");
    push(S,'a');
    push(S,'b');
    push(S,'c');
    push(S,'d');
    push(S,'e');
    printf("(4)�ж�ջs�Ƿ�ǿգ�");
    if(StackEmpty(S))
        printf("�ǿ�\n");
    else printf("��\n");

    printf("(5)ջ�ij����ǣ�%d\n",StackLength(S));
    printf("(6)�����ջ����ջ��Ԫ��:");
    StackTraverse(S);
    printf("\n(7)�����ջ����:");
    StackGet(S);

    printf("\n(8)�ж�ջs�Ƿ�ǿգ�");
    if(StackEmpty(S))
        printf("�ǿ�\n");
    else printf("��\n");

    printf("(9)�ͷ�ջ");
    DestroyStack(S);

}
コード例 #4
0
 void LineEdit()
 { /* 利用字符栈s,从终端接收一行并送至调用过程的数据区。算法3.2 */
   SqStack s;
   char ch,c;
   InitStack(&s);
   printf("请输入一个文本文件,^Z结束输入:\n");
   ch=getchar();
   while(ch!=EOF)
   { /* EOF为^Z键,全文结束符 */
     while(ch!=EOF&&ch!='\n')
     {
       switch(ch)
       {
         case '#':Pop(&s,&c);
                  break; /* 仅当栈非空时退栈 */
         case '@':ClearStack(&s);
                  break; /* 重置s为空栈 */
         default :Push(&s,ch); /* 有效字符进栈 */
       }
       ch=getchar(); /* 从终端接收下一个字符 */
     }
     StackTraverse(s,copy); /* 将从栈底到栈顶的栈内字符传送至文件 */
     ClearStack(&s); /* 重置s为空栈 */
     fputc('\n',fp);
     if(ch!=EOF)
       ch=getchar();
   }
   DestroyStack(&s);
 }
コード例 #5
0
ファイル: ALGO3-4.CPP プロジェクト: cjpthree/datastructure_vs
 static void LineEdit()
 { // 利用字符栈s,从终端接收一行并送至调用过程的数据区。算法3.2
   SqStack s;
   char ch,c;
   InitStack(s);
   printf("请输入一个文本文件,^Z结束输入:\n");
   ch=getchar();
   while(ch!=EOF)
   { // EOF为^Z键,全文结束符
     while(ch!=EOF&&ch!='\n')
     {
       switch(ch)
       {
         case '#':Pop(s,c);
                  break; // 仅当栈非空时退栈
         case '@':ClearStack(s);
                  break; // 重置s为空栈
         default :Push(s,ch); // 有效字符进栈
       }
       ch=getchar(); // 从终端接收下一个字符
     }
     StackTraverse(s,copy); // 将从栈底到栈顶的栈内字符传送至文件
     ClearStack(s); // 重置s为空栈
     fputc('\n',fp);
     if(ch!=EOF)
       ch=getchar();
   }
   DestroyStack(s);
 }
コード例 #6
0
ファイル: main.c プロジェクト: sunjiawe/MyC
int main()
{
    int j;
    SqStack s;
    int e;
    if(InitStack(&s)==OK)
    {
    	for(j=1; j<=10; j++)
        	Push(&s,j);		
	}
                
    printf("Õ»ÖÐÔªËØÒÀ´ÎΪ£º");
    StackTraverse(s);
    
    Pop(&s,&e);
    printf("µ¯³öµÄÕ»¶¥ÔªËØe=%d\n",e);
    
    printf("Õ»¿Õ·ñ£º%d(1:¿Õ0:·ñ)\n",StackEmpty(s));
    
    GetTop(s,&e);
    printf("Õ»¶¥ÔªËØe=%dÕ»µÄ³¤¶ÈΪ%d\n",e,StackLength(s));
    
    ClearStack(&s);
    printf("Çå¿ÕÕ»ºó£¬Õ»¿Õ·ñ£º%d(1:¿Õ0:·ñ)\n",StackEmpty(s));
    
    return 0;
}
コード例 #7
0
 void LineEdit()
 { // 利用字符栈s,从终端接收一行并送至调用过程的数据区。算法3.2
   SqStack s;
   char ch,c;
   int n,i;
   InitStack(s);
   scanf("%d",&n);  //多case
   ch=getchar();
   for(i=1;i<=n;i++)
   { ch=getchar();
     while(ch!='\n')
    {
       switch(ch)
       {
         case '#':Pop(s,c);
                  break; // 仅当栈非空时退栈
         case '@':ClearStack(s);
                  break; // 重置s为空栈
         default :Push(s,ch); // 有效字符进栈
       }
       ch=getchar(); // 从终端接收下一个字符
     }
     StackTraverse(s,visit); // 将从栈底到栈顶的栈内字符输出
    ClearStack(s); // 重置s为空栈
    }
   DestroyStack(s);
 }
コード例 #8
0
ファイル: main3-1.c プロジェクト: Xu-Wensheng/stack
int main()
{
	int j;
	SqStack s;
	SElemType e;

	if( InitStack(&s) == OK )
		for(j = 1; j <= 9; j++)
			Push(&s, j);

	printf("The items in the stack is: \n");
	StackTraverse(s, visit);

	Pop(&s, &e);
	printf("The item popped is: %d\n", e);
	printf("\n");

	printf("The stack is empty now? empty:1, not empty:0\t%d\n", StackEmpty(s));
	printf("\n");

	GetTop(s, &e);
	printf("The item at the top of the stack is: %d, the length of the stack is: %d\n",e, StackLength(s));
	printf("\n");

	ClearStack(&s);
	printf("The stack cleared, is the stack empty? empty:1, not empty:0, %d\n", StackEmpty(s));
	printf("\n");

	DestroyStack(&s);
	
	return 0;
}
コード例 #9
0
ファイル: editor.c プロジェクト: schwxd/linux-kernel
void LineEdit()
{
	char ch, c;
	SqStack s;

	InitStack(&s);
	ch = getchar();

	while (ch != EOF) {
		while ((ch != EOF) && (ch != '\n')) {
			switch (ch) {
			case '#':
				Pop(&s, &c);
				break;
			case '@':
				ClearStack(&s);
				break;
			default:
				Push(&s, ch);
				if (ch == EOF)
					printf("\n push eof\n");
				break;
			}
			ch = getchar();
		}
		// '\n' detected, send out and clear whole stack 
		if (ch == '\n') {
			StackTraverse(s, sendToFile);
			fputc('\n', fp);
			ClearStack(&s);
			ch = getchar();
		} else if (ch == EOF) {
			StackTraverse(s, sendToFile);
			ClearStack(&s);
			printf(" now exit\n");
		}
		/* if (ch == '\n') */
		/* 	fputc('\n', fp); */
		/* if (ch != EOF) */
		/* 	ch = getchar(); */
		/* else */
		/* 	printf("ch == eof, should exit\n"); */
	}
	DestroyStack(&s);
}
コード例 #10
0
ファイル: main.cpp プロジェクト: zz0807/learngit
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100 // 存储空间初始分配量
#define STACKINCREMENT 10 // 存储空间分配增量
#define MAXTIME 9999//MAXTIME用来表示该两个站点之间不互相连通

typedef struct stop      /*定义结构体*/
{
    char stopname[100];//站名
    int stopnumber;//站号
    struct stop *next;
} stop,*stop1;
typedef struct lineway
{
    char linenumber[10];;//线路号
    int stopcount;//该线路上站的个数
    stop station[30];
} way;
struct total
{
    int totalline;//线路总数
    way line[20];//线路
};
typedef int SElemType; // 定义栈元素类型
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
typedef int PathMatrix;
typedef int DistancMatrix ;
typedef struct vex
{
    int num;//存储站点的序号,用于arc[][]
    char name[30];//存储站名
} mvex;
typedef struct
{
    int  vexnum;//vexnum表示顶点个数,
    int arcnum;//arcnum表示弧的个数
    int **arc;//arc存放该结构体的二维数组,定义成指针是为了后续的动态分配,存放弧的信息。该二维数组存放通过两个点之间所需要的时间
   mvex *vex;//存放顶点信息
} Mgraph;
struct SqStack
{
    SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
    SElemType *top; // 栈顶指针
    int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈

Status InitStack(SqStack &S)
{
// 构造一个空栈S,该栈预定义大小为STACK_INIT_SIZE
    S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!S.base) return ERROR;
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return OK;
}

Status Push(SqStack &S,SElemType e)
{
// 在栈S中插入元素e为新的栈顶元素
    if(S.top-S.base>=S.stacksize)
    {
        S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
        if(!S.base) return ERROR;
        S.top=S.base+S.stacksize;
        S.stacksize+=STACKINCREMENT;
    }
    *S.top++=e;
    return OK;
}

Status Pop(SqStack &S,SElemType &e)
{
// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
    if(S.top==S.base) return ERROR;
    e=*--S.top;
    return OK;
}

Status GetTop(SqStack S,SElemType &e)
{
// 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
    if(S.top==S.base) return ERROR;
    e=*(S.top-1);
    return OK;
}

int StackLength(SqStack S)
{
// 返回栈S的元素个数
    int i;
    i=S.top-S.base;
    return i;
}

Status StackTraverse(SqStack S)
{
// 从栈顶到栈底依次输出栈中的每个元素
    SElemType *p = (SElemType *)malloc(sizeof(SElemType));
    p = S.top;
    if(S.top==S.base)printf("The Stack is Empty!");
    else
    {
        printf("The Stack is: ");
        p--;
        while(p>=S.base)
        {
            printf("%d ", *p);
            p--;
        }
    }
    printf("\n");
    return OK;
}
void search(struct total&ex)//查询线路
{
    char x[10];
    int i,j,index;
    printf("请输入您要查询的线路\n");
    scanf("%s",x);
    for(i=0; i<ex.totalline; i++)
    {
        if(strcmp(ex.line[i].linenumber,x)==0)
        {
            index=i;
            break;

        }
    }
    for(j=0; j<ex.line[index].stopcount; j++)
    {
        printf("%s ",ex.line[index].station[j].stopname);
    }
    printf("\n");
    printf("按任意键返回主菜单\n");
    system("pause");
}
int checkname(char *s,Mgraph C)//判断是否存在该站点
{
    int i;
    for(i=0;i<C.vexnum;i++)
    if(strcmp(s,C.vex[i].name)==0)
    return 1;
    return 0;
}
void searchname(struct total&ex,Mgraph C)//按站点名称查询线路
{
    char x[20];
    int i,j;
    int count=0;//存储找到的线路的个数
    while(1)
    {
    printf("请输入您要查询的站点名称\n");
    scanf("%s",x);
    if(checkname(x,C)==0)
    printf("该站点不存在,请重新输入");
    else break;
    }
    for(i=0; i<ex.totalline; i++)
    {
        for(j=0; j<ex.line[i].stopcount; j++)
        {
            if(strcmp(ex.line[i].station[j].stopname,x)==0)
            {
                printf("%s\n",ex.line[i].linenumber);
                count++;
            }
        }
        if(j<ex.line[i].stopcount)
        break;

    }
    if(count==0)
        printf("没有找到您输入的站点\n");

}
int getnumByname(char name[30],Mgraph C)//通过站名来查找站的编号
{

    int i;
    for(i=0;i<C.vexnum;i++)
    if(strcmp(name,C.vex[i].name)==0)
    return C.vex[i].num;
    return 0;
}
char* getnameBynum(int num,Mgraph C)//通过站号来查站名
{
    int i;
    for(i=0;i<C.vexnum;i++)
    if(num==C.vex[i].num)
    return C.vex[i].name;
    return NULL;
}
void ShortestPath_FLOYD(Mgraph G, int**P, int**D)//D是dist,P用来存储路径,P[i][j]存储j的前驱
{
    // 算法7.16
    // 用Floyd算法求有向网G中各对顶点v和w之间的最短路径
    int v,w,u;
    for (v=1; v<=G.vexnum; ++v)        // 各对结点之间初始已知路径及距离
        for (w=1; w<=G.vexnum; ++w)//初始化P数组
        {
            D[v][w] = G.arc[v][w];
            if(v!=w&&D[v][w]!=MAXTIME)
            P[v][w]=v;
            else P[v][w]=-1;
        }//for

    for (u=1; u<=G.vexnum; ++u)
        for (v=1; v<=G.vexnum; ++v)
            for (w=1; w<=G.vexnum; ++w)
                if (D[v][u]+D[u][w] < D[v][w])    // 从v经u到w的一条路径更短
                {
                    D[v][w] = D[v][u]+D[u][w];
                    P[v][w]=P[u][w];
                }//if
} // ShortestPath_FLOYD*/
void exchange(int**P,Mgraph C)
{
   char s1[30],s2[30];//用来存储读取arc文件中的两个有关系的站名
    char *s;
    int m=0,n=0,e=-1,i,j;
    while(1)
    {
    printf("请输入起始站\n");
    scanf("%s",s1);
    if(checkname(s1,C)==0)
    printf("该站点不存在,请重新输入");
    else break;
    }

    while(1)
    {
        printf("请输入终点站\n");
    scanf("%s",s2);
    if(checkname(s2,C)==0)
    printf("该站点不存在,请重新输入");
    else break;
    }

    m=getnumByname(s1,C);
    n=getnumByname(s2,C);
    SqStack S;
    InitStack(S);
    if(P[m][n]!=-1)
    Push(S,n);
    i=m;
    j=n;
    while(1)
    {
        e=P[i][j];
        Push(S,e);
        if(e==i)
        break;
        j=e;}
        StackTraverse(S);
    SElemType *p = (SElemType *)malloc(sizeof(SElemType));
    p = S.top;
    if(S.top==S.base)printf("The Stack is Empty!");
    else
    {
        printf("The Stack is: ");
        p--;
        while(p>=S.base)
        {
            s=getnameBynum(*p,C);
            printf("%s-",s);
           // printf("%d", *p);
            p--;
        }
    }
    printf("\n");
}
コード例 #11
0
ファイル: stack.c プロジェクト: eyan422/DataStructure
int main()
{
    int j;
    SqStack s;

    SElemType e;
    InitStack(&s);

    for(j=1; j<=12; j++)
    {
        Push(&s,j);
    }

    StackTraverse(s,print);
    Pop(&s,&e);

    StackTraverse(s,print);
    //GetTop(s,&e);

    //conversion(s);

    DestroyStack(&s);
}
コード例 #12
0
ファイル: stack.c プロジェクト: xiahei/TOTAL
int main(void)
{
    /* comp with 'gcc -lm -o stack' */
    Stack *S = (Stack *)malloc(sizeof(Stack));
    S->base = NULL;
    S->top = NULL;
    S->stacksize = 0;
    if (!InitStack(S))
        exit(ERROR);
    conversion(S);

    StackTraverse(S, Visit);
    putchar('\n');
    return 0;
}
コード例 #13
0
ファイル: maze.c プロジェクト: gitzzg/Data_Structure
int main()
{
    int m, n;    // 行数和列数
    int k, flag; // 判断是否走出了迷宫

    ElemType **MAZE = NULL; // 指向迷宫数组
    ElemType **MARK = NULL; // 记忆数组
    SqStack *s, s1;
    QNode *Q, q;

    srand((unsigned)time(0));
    system("color 17"); // 背景色蓝色,前景色白色

    printf("请输入迷宫的行数和列数:");
    while (scanf("%d %d", &m, &n) != EOF && m && n && m <= MAXSIZE && n <= MAXSIZE)
    {
	CreateMAP(&MAZE, m, n, maze);
	CreateMAP(&MARK, m, n, mark);
	Q = &q;
	s = &s1;
	InitQueue(Q); // 初始化队列
	InitStack(s); // 初始化栈
	printf("\n根据您的输入构造迷宫如下:\n");
	PrintMAZE(m, n, MAZE);
	flag = FindPath(MAZE, MARK, Q, m, n, Directrion); // 寻找路径
	if (flag)
	{ // 可以走出迷宫
	    PushData(q, s);
	    k = s->top;
	    k++;
	    for (int i = 0; i < k; i++)
	    {
		system("cls");
		PrintMAZE(m, n, MAZE);      // 打印迷宫
		PrintPuzzle(MAZE, s, m, n); // 打印迷宫
		num++;
		Sleep(600);
	    }
	    StackTraverse(s1); // 输出坐标
	}
	else
	{
	    PrintMAZE(m, n, MAZE);
	    printf("No Path!\n");
	}
    }
    return 0;
}
コード例 #14
0
 void main()
 {
   int j;
   LinkStack s;
   SElemType e;
   if(InitStack(&s))
     for(j=1;j<=5;j++)
       Push(s,2*j);
   printf("栈中的元素从栈底到栈顶依次为: ");
   StackTraverse(s,print);
   Pop(s,&e);
   printf("弹出的栈顶元素为%d\n",e);
   printf("栈空否: %d(1:空 0:否)\n",StackEmpty(s));
   GetTop(s,&e);
   printf("当前栈顶元素为%d,栈的长度为%d\n",e,StackLength(s));
   ClearStack(s);
   printf("清空栈后,栈空否: %d(1:空 0:否),栈的长度为%d\n",StackEmpty(s),StackLength(s));
   printf("是否销毁栈了: %d(1:是 0:否)\n",DestroyStack(&s));
 }
コード例 #15
0
int main()
{
        int j;
        LinkStack s;
        int e;
        if(InitStack(&s)==OK)
                for(j=1;j<=10;j++)
                        Push(&s,j);
        printf("栈中元素依次为:");
        StackTraverse(s);
        Pop(&s,&e);
        printf("弹出的栈顶元素 e=%d\n",e);
        printf("栈空否:%d(1:空 0:否)\n",StackEmpty(s));
        GetTop(s,&e);
        printf("栈顶元素 e=%d 栈的长度为%d\n",e,StackLength(s));
        ClearStack(&s);
        printf("清空栈后,栈空否:%d(1:空 0:否)\n",StackEmpty(s));
        return 0;
}
コード例 #16
0
ファイル: GraphDFS.cpp プロジェクト: JASONDHSU/C
void OutPath(CSTree T)
{
	// 依次从左到右输出森林中每一棵树中从根到叶的路径
// 森林的存储结构为孩子-兄弟链表,T为头指针
	Stack S; //栈S存储路径
	InitStack(S);
	if (T)
	{
		Push(S,T->data); // 根结点加入路径S

		if (!T->firstchild)
			StackTraverse(S, output); // 求得一条路径
		//从栈底到栈顶依次对栈S中每个元素调用函数output()
		else
			OutPath(T->firstchild); // 递归,遍历子树森林

		Pop(S); // 从路径中删除栈顶结点

		OutPath(T->nextsibling); // 递归,遍历其余树的森林
	}
}
コード例 #17
0
ファイル: 2-1.cpp プロジェクト: JakLiao/Mycode
int main()
{
    int a;
    SqStack S;
    SElemType x, e;
    if(InitStack(S))    // 判断顺序表是否创建成功,请填空
    {
        printf("A Stack Has Created.\n");
    }
    while(1)
    {
        printf("1:Push \n2:Pop \n3:Get the Top \n4:Return the Length of the Stack\n5:Load the Stack\n0:Exit\nPlease choose:\n");
        scanf("%d",&a);
        switch(a)
        {
        case 1:
            scanf("%d", &x);
            if(!Push(S,x)) printf("Push Error!\n"); // 判断Push是否合法,请填空
            else printf("The Element %d is Successfully Pushed!\n", x);
            break;
        case 2:
            if(!Pop(S,e)) printf("Pop Error!\n"); // 判断Pop是否合法,请填空
            else printf("The Element %d is Successfully Poped!\n", e);
            break;
        case 3:
            if(!GetTop(S,e))printf("Get Top Error!\n"); // 判断Get Top是否合法,请填空
            else printf("The Top Element is %d!\n", e);
            break;
        case 4:
            printf("The Length of the Stack is %d!\n",StackLength(S)); //请填空
            break;
        case 5:
            StackTraverse(S);  //请填空
            break;
        case 0:
            return 1;
        }
    }
}