数据结构程序

数据结构程序,第1张

#include<如巧stdio.h>

void main()

{ int a[10],i=0,temp

int *head,*tail,min=0,max=0

head=a

tail=a+9

for(i=0i<10i++)

{

scanf("%d",&a[i])

if(a[i]>a[max])

max=i

if(a[i]<誉橡扰a[min])

min=i

}

temp=*head

*head=a[max]

a[max]=temp

temp=*tail

*tail=a[min]

a[min]=temp

for(i=0i<10i++)

{

printf("%d "庆旦,&a[i])

}

}

#include<stdio.h>

#include <malloc.h>

typedef struct node

{

char ch

struct node *pNext

}Node

int main()

{

char ch

Node *p=NULL//pointer to a node

Node *head=NULL//pointer to the head of the list

Node *tail=NULL

int count=1

printf("请输入节点值,以“ #” 号结束\n:")

scanf("%c",&ch)

while(ch!='#')

{

p=(Node*)malloc(sizeof(Node))

p->ch=ch

p->pNext=NULL

if(head==NULL)

{

head=tail=p

}

else

{

tail->pNext=p

tail=p

}

scanf("%c",&ch)

}

//带弯display

p=head

printf("你输入的链表是:\n ")

while(p!=NULL)

{

printf("%c ",p->ch)

p=p->pNext

}

fflush(stdin)

printf("\n请输入你蠢迟闷要查找的节旦颂点\n:")

scanf("%c",&ch)

p=head

while(p!=NULL)

{

if(p->ch!=ch)

{

p=p->pNext

count++

}

else break

}

if(p==NULL)printf("该值的序号是:0\n")

else printf("该值的序号是%d\n",count)

p=head

while(p!=NULL)

{

head=p->pNext

free(p)

p=head

}

return 1

}

#include<StdAfx.h>

#include<iostream.h>

#include<string.h>

#include<stdlib.h>

/*栈*/

template <class T>

class CStack

{

public:

int m_count

T * m_arr

int m_curposition

CStack(int count)

{

m_arr = new T [count]

m_count = count

m_curposition = -1

}

bool push(T val)

{

if(m_curposition == m_count-1)

return false

m_arr[++m_curposition] = val

}

T pop()

{

if(IsEmpty())

return 0

return m_arr[m_curposition--]

}

T GetElement()

{

if(IsEmpty())

return 0

return m_arr[m_curposition]

}

bool IsEmpty()

{

if(m_curposition <0)

return true

return false

}

~CStack()

{

delete [] m_arr

}

}

/* *** 作函数定义 */

bool ExpressionIsRight(char * pExpression) // 表达式是否正确

bool ProcessExpression(char * pExpression) /档蠢/ 处理表达式

int GetIndex(char ch) // 获取 *** 作符在数组中的下标

void Calculate() // 计算

double GetResult() // 得到结果

bool IsEnd(char ch)// 表达式是否结束

double GetNum(char *pStr, int &i) /毕答/ 获取 *** 作数

CStack<char>stack_sign(100) // 运算符栈CStack<double>stack_num(100) // *** 作数栈

//运算符号优先级表

int PriTbl[7][7]={

{1,1,-1,-1,-1,1,1},

{1,1,-1,-1,-1,1,1},

{1,1,1,1,-1,1,1},

{1,1,1,1,-1,1,1},

{-1,-1,-1,-1,-1,0,2},

{1,1,1,1,2,1,1},

{-1,-1,-1,-1,-1,2,0}

}

bool ExpressionIsRight(char * pExpression)

{

int len = strlen(pExpression)

char arr[7] = {'+','-','*','/','(',')','行数陪.'}

for(int i=0i<leni++)

{

if(!(pExpression[i]>='0' &&pExpression[i]<='9'))

{

if(pExpression[0] == '.' || pExpression[len-1] == '.')

return false

int flag = 0

for(int j=0j<sizeof(arr)j++)

{

if(pExpression[i] == arr[j])

{

flag = 1

break

}

}

if(flag == 0)

return false

}

}

return true

}

bool ProcessExpression(char * pExpression)

{

if(!ExpressionIsRight(pExpression))

return false

int len = strlen(pExpression)

pExpression[len++] = '#'

stack_sign.push('#')

if(len == 1)

return false

for(int i=0i<leni++)

{

if(pExpression[i] >= '0' &&pExpression[i] <= '9') // *** 作数

{

double val = GetNum(pExpression, i)

stack_num.push(val)

}

else // *** 作符

{

int pre = GetIndex(stack_sign.GetElement())

int next = GetIndex(pExpression[i])

switch(PriTbl[pre][next])

{

case -1:// next >pre

stack_sign.push(pExpression[i])

break

case 0: // next = pre

if(IsEnd(pExpression[i]))

return true

stack_sign.pop()

break

case 1: // next <pre

Calculate()

i--// back

break

}

}

}

return true

}

double GetNum(char *pStr, int &i)

{

char Nums[100]

int j = 0

while((pStr[i] >= '0' &&pStr[i] <= '9') || pStr[i]=='.')

Nums[j++] = pStr[i++]

i--

Nums[j] = '\0'

return atof(Nums)

}

int GetIndex(char ch)

{

switch(ch)

{

case '+': return 0

case '-': return 1

case '*': return 2

case '/': return 3

case '(': return 4

case ')': return 5

case '#': return 6

}

}

void Calculate()

{

double num1, num2

num2 = stack_num.pop()

if(stack_num.IsEmpty())

{

cout <<"表达式错误!" <<endl

exit(0)

}

num1 = stack_num.pop()

switch(stack_sign.pop())

{

case '+': stack_num.push(num1+num2)return

case '-': stack_num.push(num1-num2)return

case '*': stack_num.push(num1*num2)return

case '/':

{

if(num2 == 0)

{

cout <<"除数不能为0!程序终止!" <<endl

exit(0)

}

stack_num.push(num1/num2)

return

}

}

}

bool IsEnd(char ch)

{

if(ch == '#')

if(stack_sign.GetElement() == ch)

return true

return false

}

double GetResult()

{

if(stack_sign.GetElement() != '#')

{

cout <<"表达式错误!" <<endl

exit(0)

}

return stack_num.GetElement()

}

//穷举

void FindBro1(int * pArray,int nCount)

{

int * pTemp = new int[nCount]

for(int i=0i<nCounti++)

{

pTemp[i]=-1

for(int j=i+1j<nCountj++)

{

if(pArray[j]>=pArray[i])//找到

{

pTemp[i]=j

break

}

}

}

cout<<"结果:"

for(int m=0m<nCountm++)

{

cout<<pTemp[m]<<" "

}

cout<<endl<<endl

delete []pTemp

}

//栈方法

void FindBro2(int * pArray,int nCount)

{

CStack<int>stack_bro(nCount)

for(int i=nCount-1i>=0i--)

{

stack_bro.push(-1)

for(int j=i+1j<nCountj++)

{

if(pArray[j]>=pArray[i])//找到

{

stack_bro.pop()

stack_bro.push(j)

break

}

}

}

cout<<"结果:"

while(!stack_bro.IsEmpty())

{

cout<<stack_bro.pop()<<" "

}

cout<<endl<<endl

}

void FindBro(int nmode)//nmode为1时,是用普通穷举法,为2时,加入栈编程

{

int nCount

cout <<"请输入元素个数:\n"

cin>>nCount

int *pArray = new int[nCount]

for(int i=0i<nCounti++)

{

printf("请输入第%d个元素的值:\n",i+1)

cin>>pArray[i]

}

if(nmode==1)

{

FindBro1(pArray,nCount)

}

else

{

FindBro2(pArray,nCount)

}

delete []pArray

return

}

//表达式

void express()

{

char strExpression[100]

cout <<"请输入算术表达式:"

cin >>strExpression // 输入表达式

if(!ProcessExpression(strExpression)) // 处理表达式

{

cout <<"表达式错误!" <<endl<<endl

}

else

cout <<"计算结果:" <<GetResult() <<endl<<endl // 输出结果

}

void main()

{

cout <<"1. 表达式求值:\n"

express() //表达式求值

cout <<"2. 数组穷举法,找亲兄弟:\n"

FindBro(1)//数组穷举法,找亲兄弟

cout <<"3. 借助栈编程,找亲兄弟:\n"

FindBro(2)//借助栈进行

}

表达式部分参考了网上的一个方法


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12416588.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存