C语言数据结构

C语言数据结构,第1张

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

1、具体要求:以下算法是一个迷宫的算法,在走迷宫过程中用到了队列,请理解以下算法,并将算法的实现过程、子函数的作用以及测试数据写在报告纸上。

#include"stdio.h"

#include"stdlib.h"

#define m 4

#define n 4

struct stype

{int x,y,pre

}sq[400]

int mg[m+2][n+2]

int zx[9],zy[9]

void printlj(int rear)

{int i

i=rear

do

{printf("(%d,%d)",sq[i].x,sq[i].y)

i=sq[i].pre

}while(i!=0)

}

void mglj()

{int i,j,x,y,front,rear,find,v

sq[1].x=1sq[1].y=1sq[1].pre=0find=0

front=rear=1mg[1][1]=-1

while(front<=rear&&!find)

{x=sq[front].x

y=sq[front].y

for(v=1v<=8v++)

{i=x+zx[v]

j=y+zy[v]

if(mg[i][j]==0)

{rear++

sq[rear].x=i

sq[rear].y=j

sq[rear].pre=front

mg[i][j]=-1

}

if(i==m&&j==n)

{printlj(rear)

find=1

}

}

front++

}

if(!find) printf("no way\n")

}

void main()

{int i,j

for(i=1i<=mi++)

for(j=1j<=nj++)

scanf("%d",&mg[i][j])

for(i=0i<=m+1i++)

{mg[i][0]=1mg[i][n+1]=1

}

for(j=0i<=n+1i++)

{mg[0][j]=1mg[m+1][j]=1

}

zx[1]=-1zx[2]=-1zx[3]=0zx[4]=1

zx[5]=1zx[6]=1zx[7]=0zx[8]=-1

zy[1]=0zy[2]=1zy[3]=1zy[4]=1

zy[5]=0zy[6]=-1zy[7]=-1zy[8]=-1

mglj()

}

2、具体要求:背包问题是算法中一个经典的问题,请查找相关资料对它进行描述,并且利用C语言实现该算法。附代码。

解析:

第一个问题是个迷宫问题.很多地方都有答案吧

思路就是往前后左右四个方格前进,进到新的方格中的时候再前后左右...这样递归下去.如果遇到不可走的路,就把它从队列里删除.直到找到出口为止.

以前写过具体的算法.但是找不到...只能给你点提示啦

#include

"stdio.h"

#include

"malloc.h"

#define

null

0

struct

node

/*定义结构体*/

{int

data

struct

node

*next

}

struct

node

*head

struct

node

*p

struct

node

*s

void

creat()

/*创建单链表*/

{int

ch

head=(struct

node

*)malloc(sizeof(struct

node))

head->next=null

p=head

printf("请输入数据:

")

scanf("%d",&ch)

while(ch!=-1)

{s=(struct

node

*)malloc(sizeof(struct

node))

s->data=ch

s->next=null

p->next=s

p=s

printf("请输入数据:

")

scanf("%d",&ch)

}

}

void

outline()

/*输出单链表*/

{p=head->next

while(p!=null)

{printf("%d

",p->data)

p=p->next

}

printf("\n")

}

int

locate(int

x)

/*按值查找*/

{p=head->next

while((p!=null)&&(p->data!=x))

p=p->next

if(p==null)

return

0

else

return

(p->data)

}

main()

/*主函数*/

{int

a=0

creat()

outline()

printf("请输入你要找的数:\n")

scanf("%d",&a)

printf("你要找的数的下标是:

%d\n",locate(a))

printf("结点个数是:

%d\n",countnode())

}

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

要求:1.建立一个带头节点的单链表节点的数据类型为int(data)输入非负整数。(用-1为结束标志)长度大于等于10.

2.在单链的第i个节点前插入一个值为X的新节点,i,x用实参调用,插入前后都有一遍输出,每个算法写成函数,用主函数调用,一个主函数,三个用户函数(插入,输入,输出)建立链表.

如果那位高手会希望在22号晚上之前给予答案.谢谢!!!

解析:

首先申明,我这肯定是运行成功的程序,否则我不会贴出来

可能是因为你没有引用相应的头文件

头文件我这里是没写

根据你的程序修改后:

#include <iostream>vs就用这个库

#include <stdlib.h>vc用这两个

#include <stdio.h>

#define NULL 0

typedef struct list

{

int data

struct list* next

}ListNode

ListNode* CreateList(void)

{

int data

ListNode* head=(ListNode*)malloc(sizeof(ListNode))

ListNode *s,*r

r=head

scanf("%d",&data)

while(data!=-1)直到输入-1为止,一直循环

{

s=(ListNode*)malloc(sizeof(ListNode))

s->data=data

r->next=s

r=s

scanf("%d",&data)

}

r->next=NULL

return head

}

ListNode* GetNode(ListNode* head,int i)找到i之前的node,用来插入

{

int count=0

ListNode* p=head

while(p!=NULL)

{

if(count==i)

return p

count++

p=p->next

}

return NULL

}

void InsertList(ListNode* head,int x,int i)

{

ListNode *p,*s

p=GetNode(head,i-1)

if(p==NULL)

{

printf("position error\n")

return

}

s=(ListNode*)malloc(sizeof(ListNode))

s->data=x

s->next=p->next

p->next=s

}

void PrintList(ListNode* head)

{

ListNode* p1=head->next

while(p1!=NULL)

{

printf("%d->",p1->data)

p1=p1->next

}

printf("NULL")

}

int main()

{

ListNode* head = CreateList()

InsertList(head,5,2)

PrintList(head)

return 0

}

原程序:

#define NULL 0

struct link

{

int data

struct link* next

}

void input(struct link* head)

{

int data

struct link* p1=head

struct link* p

scanf("%d",&data)

while(data!=-1)

{

p = (struct link*)(malloc)(sizeof(struct link))

p->data=data

p->next=NULL

p1->next=p

p1=p

scanf("%d",&data)

}

}

void print(struct link* head)

{

struct link* p1=head->next

while(p1!=NULL)

{

printf("%d->",p1->data)

p1=p1->next

}

printf("NULL")

}

void insert(struct link* head,int i,int data)

{

struct link* p1=head

struct link* p

int count=0

while(p1!=NULL&&count<=i-1)

{

if(count==i-1)

{

p = (struct link*)(malloc)(sizeof(struct link))

p->data=data

p->next=p1->next

p1->next=p

return

}

p1=p1->next

count++

}

}

int main()

{

struct link* head = (struct link*)(malloc)(sizeof(struct link))

head->data=0

head->next=NULL

input(head)

insert(head,1,5)

print(head)

return 0


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存