❶ C语言版数据结构编程题
//试编写一道在单链表中数据域值为a的结点之后,
//插入一个新结点的算法.若原链表中无数据域值为a的结点,
//则把新结点插入到表尾.设新结点数据域值为x.小弟初学,谢谢大家啦
//定义结点
typedef struct node{
int data;
struct node *next;
}LNode ,*LinkList;
//list为单链表的表头指针,插入元素为x
void insert(LinkList &list,int x)
{
LinkList p,q;
//生成一个结点其结点值为x
p=( LinkList)malloc(sizeof(LNode));
p->data=x;
p->next=NULL;
//查找结点值为a的结点,用r指向q的前一个结点
q=list;
while(q!=NULL&&q->data!=a)
{
r=q;
q=q->next;
}
//插入x
//存在数据域值为a的结点
if(q->data==a)
{
p->next=q->next;
q->next=p;
}
//不存在数据域值为a的结点
else{
q->next=p
}
}
❷ 数据结构(C语言版)编程题 急急急~
改一下这两句
if(estimate(p)==0) printf("此字符串符合要求类型。");
else printf("此字符串不符合要求类型。");
为
if(estimate(p)) printf("此字符串符合要求类型。");
else printf("此字符串不符合要求类型。");
❸ c语言编程 数据结构题
C语言实现链式栈,进行两个栈的进出栈操作,并分别输出栈的内容,参考代码如下:
#include<stdio.h>
#include<stdlib.h>
typedefstructLnode{
intdata;
structLnode*next;
}Lnode,*Linklist;
voidPrint(Linklistlist);
voidPush(Linklistlist,intdata);
intPop(Linklistlist,int*data);
voidDelete(Linklistlist);
intmain(void)
{
Linklists,t;
intdata;
s=(Lnode*)malloc(sizeof(Lnode));
t=(Lnode*)malloc(sizeof(Lnode));
if(s==NULL||t==NULL)
return1;
s->next=NULL;
t->next=NULL;
Push(s,1);
Push(s,2);
Push(s,3);
Push(s,4);
Push(s,5);
printf("链表s=");
Print(s);
printf("链表t=");
Print(t);
Pop(s,&data);
Push(t,data);
Pop(s,&data);
Push(t,data);
Pop(s,&data);
Push(t,data);
Pop(s,&data);
Push(t,data);
Pop(s,&data);
Push(t,data);
printf("链表s=");
Print(s);
printf("链表t=");
Print(t);
Delete(s);
Delete(t);
return0;
}
voidPush(Linklistlist,intdata)
{
Lnode*p=(Lnode*)malloc(sizeof(Lnode));
if(p==NULL)exit(1);
p->data=data;
p->next=list->next;
list->next=p;
}
intPop(Linklistlist,int*data)
{
Lnode*p=list->next;
if(p==NULL)return0;
list->next=p->next;
*data=p->data;
free(p);
return1;
}
voidPrint(Linklistlist)
{
Lnode*p;
for(p=list->next;p!=NULL;p=p->next)
printf("%d",p->data);
printf(" ");
}
voidDelete(Linklistlist)
{
Lnode*p=list->next,*q;
while(p!=NULL){
q=p;
p=p->next;
free(q);
}
list->next=NULL;
}
❹ 数据结构编程题 C语言!
2. 元素类型未定,比较方法未定,不能进行排序。
3. printf("%o",i);// C, 8进制
cout<<std::oct<<i; // C++, 8进制
输入cin>>i
4. 用STL的list,链表不用自己谢啦。(C++ ONLY)
#include <list>
using namespace std;
5. 这个太多了不写。
6. STL, list
list<int> int_lst;
list<int>::iterator i = int_lst.begin()
for(;i != int_lst.end(); ++i)
删除节点:int_lst.erase(i)
PS:这里有陷阱,嘿嘿,注意啦。
交换节点:
int a = (*i);
(*i) = *(i+1);
*(i+1) = a;
完事啦。
❺ 请教一个C语言的数据结构编程的题目,高手进!
#include <stdio.h>
int A[6][6];
int i,j,sides=0,degree[6];
int main() {
for(i=0;i<6;i++) for(j=0;j<6;j++){
scanf("%d",&A[i][j]);
if(A[i][j]){
sides++;
degree[i]++;
degree[j]++;
}
}
sides/=2;
printf("total number of sides is %d \n",sides);
for(i=0;i<6;i++) {
degree[i]/=2;
printf("degree of vertex %d is %d \n",i,degree[i]);
}
return 0;
}
数据:
0 1 1 0 1 0
1 0 1 1 0 1
1 1 0 0 1 1
0 1 0 0 1 0
1 0 1 1 0 1
0 1 1 0 1 0
输出:
total number of sides is 10
degree of vertex 0 is 3
degree of vertex 1 is 4
degree of vertex 2 is 4
degree of vertex 3 is 2
degree of vertex 4 is 4
degree of vertex 5 is 3
说明:
A[i][j]=0 代表 i节点与j节点无连接,因为是无向的, A[j][i]一定=0.
A[i][j]=1时亦然.
因此 A[i][j]图一定是对称的.
所以每个边都计算了2次,度也是
因此最后整除2
❻ 数据结构(用C语言描述)编程题求助!!!
这是个二叉排序树例题,希望对你有帮助! #include "stdio.h" # include "stdlib.h" struct Bnode {int data; struct Bnode *lchild,*rchild; }; void insertbst(Bnode *&t, Bnode *s) {if (t== NULL) t=s; else if (s->data< t->data) insertbst(t-> lchild,s); else insertbst(t-> rchild,s); } void inorder(Bnode *t) { if (t!=NULL) { inorder(t->lchild); printf("%d ", t->data); inorder(t->rchild); } } void main() {int i,x; Bnode *t=NULL,*s; for (i=1;i<=4;i++) {scanf("%d",&x); s=( Bnode *)malloc(sizeof(Bnode)); s->data=x; s-> lchild= NULL; s-> rchild= NULL; printf("\n"); insertbst(t,s); } inorder( t); printf("\n"); }