❶ 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"); }