求一个完整的数据结构程序(C语言)

求一个完整的数据结构程序(C语言),第1张

#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 <iostream>

using namespace std

// 定义二叉差乎锋树结点

struct BTreenode

{

char data

BTreenode *left

BTreenode *right

}

BTreenode *creatTree( )

{

BTreenode *S[50]

int top=-1

BTreenode *BT

BT=NULL

char *str

char ch

int i=0

int k=0

BTreenode *ap

str= new(char)

while((ch=cin.get())!='#')

str[k++]=ch

str[k]='\虚晌0'

while(str[i])

{

switch(str[i])

{

case '(' :S[++top]=apk=1break

case ')'顷铅:top--break

case ',' : k=2 break

default :

{

ap=new BTreenode

ap->data=str[i]

ap->left=NULL

ap->right=NULL

if(BT==NULL)

BT=ap

else

{

if(k==1)

S[top]->left=ap

else

S[top]->right=ap

}

}

}

i++

}

return BT

}

void inorder(BTreenode *root)

{

if (root!=NULL)

{

inorder(root->left )

cout<<root->data

inorder(root->right )

}

}

int main()

{

BTreenode *T

T= creatTree( )

inorder(T)

cout<<endl

return 0

}


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

原文地址:https://54852.com/yw/12439303.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存