數據結構課程設計——排序與棧

冒泡排序與堆排序的比較

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h> 
#define max 100//隨機函數生成的元素個數 

typedef int KeyType;//關鍵字類型 
typedef struct
{
	KeyType Key;//關鍵字域	
}LineList;//線性表元素類型

int count1 = 0,count2 = 0,count3 = 0,count4 = 0;//記錄比較次數與交換次數 
void BubbleSort(LineList r[],int n)
{
	/*冒泡排序算法函數*/ 
	int i,j,exchange;
	LineList temp;
	for (i = 1; i < n; i++)//每趟進行比較 
	{
		exchange = 0;//本趟比較是否有交換變量
		for(j = 1; j <= n-i; j++)//從第一個到第n-i個記錄進行一趟比較 
		{
			count1++;
			if(r[j].Key > r[j+1].Key)//若前面記錄大於後面記錄則交換 
			{
				temp = r[j];
				r[j] = r[j+1];
				r[j+1] = temp;
				exchange = 1;
				count2++; 
				 
			} 
			if(exchange = 0)
			{
				return;//若本躺比較沒有交換,則提前結束循環 
			}
		} 	
	}
	printf("此次冒泡排序比較次數爲:%d\n",count1);
	printf("此次冒泡排序移動次數爲:%d\n",count2);
}

void swap(int *num1,int *num2)
{
	/*移動函數*/
	int tmp = *num1;
	*num1 = *num2;
	*num2 = tmp;
	count4++; 
}

void down(LineList r[],int i,int n)
{
	/*元素向下調整函數*/
	int child,tmp;//子節點索引號與儲存當前父節點元素的臨時變量 
	for(tmp = r[i].Key; 2*i+1<n; i = child)
	{
		child = 2*i+1;
		if(child != n-1 && r[child+1].Key > r[child].Key)
		{	//挑出左右子節點中的較大者 
			child++;
			count3++;
		}
		if(r[i].Key < r[child].Key)
		{
			//交換當前父節點處的元素值與較大子節點的元素值
			count3++;
			swap(&r[i].Key,&r[child].Key); 
		} 
		else break;
	} 
}

void HeapSort(LineList r[],int n)
{
	//堆排序函數 
	int i;
	//(1)根據數組的元素,創建大根堆
	for(i = (n/2)-1; i>=0; i--)
	{
		down(r,i,n);		
	} 
	//(2)調整大根堆
	for(i = n-1; i > 0; i--)
	{
		swap(&r[0].Key,&r[i].Key);
		down(r,0,i);
	} 
	printf("此次堆排序比較次數爲:%d\n",count3);
	printf("此次堆排序移動次數爲:%d\n",count4);	
}

int main()
{
	int i,n;
	LineList r[max];
	srand((unsigned int)time(NULL));
	for( i = 1; i <= max; i++)
	{
		r[i].Key = rand(); 
	}
	printf("本次隨機生成的%d個數字爲:\n",max);
	
	for(i = 1; i <= max; i++)
	{
		printf("%d ",r[i]);
	}
	printf("\n\n");
	BubbleSort(r,max);
	printf("\n");
	HeapSort(r,max);
}

基於棧的計算器

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<windows.h> 

/*結構體定義*/
typedef struct ListNode
{
	char name[10];
	float num;
	struct ListNode *next;
} LN;

typedef LN* LL;

typedef struct StackNode
{
	int flag;//判斷數字與符號 
	float data;
	char sign;//括號判斷符 
	struct StackNode * next; 
}SN, *LSP;

typedef struct LinkStack
{
	LSP top;
	int count;
}LS;

LL N;
char z[100];//儲存表達式 

/*判斷棧空函數*/
int ISEmpty(LS *S)
{
	if(S->count==0&&S->top->next==NULL) 
		return 1;
	return 0;
}

/*數字入棧函數*/ 
int PushNum(LS *S,float a)
{
	LSP s=(LSP)malloc(sizeof(SN));
	s->data=a;
	s->flag=1;
	s->next=S->top;
	S->top=s;
	S->count++;
	return 1;
} 

/*符號入棧函數*/
int PushCh(LS *S,char a)
{
	LSP s=(LSP)malloc(sizeof(SN));
	s->sign=a;
	s->flag=2;
	s->next=S->top;
	S->top=s;
	S->count++;
	return 1;
} 

/*數字出棧函數*/
int PopNum (LS *S,float *a)
{
	LSP p;
	if(ISEmpty(S)) 
	return 0;
	*a=S->top->data;
	p=S->top;
	S->top=S->top->next;
	free(p);
	S->count--;
	return 1;
}

/*符號出棧函數*/
int PopCh (LS *S,char *a)
{
	LSP p;
	if(ISEmpty(S)) 
	return 0;
	*a=S->top->sign;
	p=S->top;
	S->top=S->top->next;
	free(p);
	S->count--;
	return 1;
}

/*錄入變量函數*/
void CreateList(LL *L,int n)
{
	LL p,l;
	int i;
	*L=(LL) malloc (sizeof(LN));
	l=*L;
	printf("請輸入%d個變量及其值:\n",n);
	for(i=0;i<n;i++)
	{
		p=(LN *) malloc (sizeof(LN));
		printf("%d. ",i+1);
		scanf("%s%f",p->name,&p->num);
		l->next=p;
		l=p; 
	}
	l->next=NULL;
}

/*打印變量函數*/
void PrintList(LL l)
{
	int flag = 0;
	while(l->next)
	{
		l=l->next;
		if(flag) 
		printf(",");
		printf("%s=%.2f",l->name,l->num);
		flag=1;
	}
	printf("\n");
}

/*查找變量對應的值函數*/
int Find(LL l,char ch[10],float *a) 
{
	int flag=1;
	while(l->next)
	{
		l=l->next;
		if(strcmp(ch,l->name)==0)
		{
			flag=0;
			break;
		} 
	}
	if(flag) 
	return 0;
	*a = l->num;
	return 1;
}

/*中綴轉後綴函數*/
void MidtoLast(LS *last)
{
	LS l,p;
	p.count=0;
	l.count=0;
	char a[1000],b,d,ch[10];
	int flag=1;
	int i=0,j=0;
	int k;
	float c;
	printf("請輸入表達式:\n"); 
	scanf("%s",a);
	d=a[0];
	strcpy(z,a);
	for(i=0;flag==1;i++,d=a[i])
	{
		if(d=='('||d=='*'||d=='/')
		{
			PushCh(&p,d);
		}
		else if(d=='+'||d=='-')
		{
			while(p.count>0&&p.top->sign!='(')
			{
				PopCh(&p,&b);
				PushCh(&l,b);
			}
			PushCh(&p,d);
		}
		else if(d==')')
		{
			while(p.top->sign!='(')
			{
				PopCh(&p,&b);
				PushCh(&l,b);
			}
			PopCh(&p,&b);
		}
		else if(d=='=')
		{ 
			while(p.count>0)
			{
				PopCh(&p,&b);
				PushCh(&l,b);
			}
			flag=0;
		}
		else if(d>='0'&&d<='9')
		{
			for(j=0;a[i+j]!='='&&a[i+j]!='+'&&a[i+j]!='-'&&a[i+j]!='*'&&a[i+j]!=')'&&a[i+j]!='/'&&a[i+j]!='(';j++)
			{
				ch[j]=a[j+i];
				ch[j+1]='\0';
			} 
			k=j; 
			c=0;
			i+=k-1;
			for(j=0;j<k;j++)
			{
				c+=(float)(ch[j]-'0')*pow(10,(k-j-1));
			}
			PushNum(&l,c);
		}
		else
		{
			for(j=0;a[i+j]!='='&&a[i+j]!='+'&&a[i+j]!='-'&&a[i+j]!='*'&&a[i+j]!=')'&&a[i+j]!='/'&&a[i+j]!='(';j++)
			{
				ch[j]=a[j+i];
				ch[j+1]='\0';
			}
			i+=j-1;
			Find(N,ch,&c);
			PushNum(&l,c);
		}
	}
	while(l.count!=0){
		if(l.top->flag==1){
			PopNum(&l,&c);
			PushNum(last,c);
		}
		else if(l.top->flag==2)
		{
			PopCh(&l,&d);
			PushCh(last,d);
		}
	}
} 

/*後綴轉中綴並進行計算函數*/
float LasttoMid(LS last)
{
	float a,b,c;
	char d;
	LS l;
	while(last.count!=0)
	{
		if(last.top->flag==1)
		{
			PopNum(&last,&c);
			PushNum(&l,c);
		}
		if(last.top->flag==2)
		{
			PopNum(&l,&b);
			PopNum(&l,&a);
			PopCh(&last,&d);
			switch(d){
				case '+': 
					c=a+b;
					break;
				case '-': 
					c=a-b;
					break;
				case '*': 
					c=a*b;
					break;
				case '/': 
					c=a/b;
					break;
			}
			PushNum(&l,c);
		}
	}
	return l.top->data;
}	
int main() 
{
	int n;
	LS last;
	last.count=0;
	printf("請輸入變量個數:");
	scanf("%d",&n);
	CreateList(&N,n);
	
	system("cls");
	PrintList(N);
	MidtoLast(&last);
	
	system("cls");
	PrintList(N);
	printf("%s%.2f",z,LasttoMid(last));
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章