C语言数据结构求解 C语言数据结构求解

c\u8bed\u8a00\u6570\u636e\u7ed3\u6784

#include#include#defineINFINITY0#defineMAX_VERTEX_NUM10//\u6700\u5927\u9876\u70b9\u6570#defineMAX_EDGE_NUM40//\u6700\u5927\u8fb9\u6570typedefenum{DG,DN,UDG,UDN}Graphkind;typedefcharVertexType;//\u9876\u70b9\u6570\u636e\u7c7b\u578btypedefstructArcCell{intadj;//\u65e0\u6743\u56fe\uff0c1\u62160\u8868\u793a\u76f8\u90bb\u5426\uff1b\u5e26\u6743\u56fe\u5219\u662f\u6743\u503c\u3002//int*info;}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];typedefstruct{VertexTypevexs[MAX_VERTEX_NUM];//\u9876\u70b9\u5411\u91cfAdjMatrixarcs;//\u90bb\u63a5\u77e9\u9635intvexnum,arcnum;//\u56fe\u7684\u5f53\u524d\u9876\u70b9\u6570\u548c\u5f27\u6570\u3002Graphkindkind;}MGraph;intLocateVex(MGraphG,VertexTypev1){inti;for(i=0;i>G.vexnum;cout>G.arcnum;for(inti=0;i>G.vexs[i];}for(i=0;i>v1>>v2;cout>w;i=LocateVex(G,v1);j=LocateVex(G,v2);G.arcs[i][j].adj=w;G.arcs[j][i].adj=G.arcs[i][j].adj;}return1;}voiddispMGraph(MGraphG){cout#include#defineMAX_VERTEX_NUM20//\u6700\u5927\u9876\u70b9\u6570#defineMAX_EDGE_NUM40//\u6700\u5927\u8fb9\u6570intvisited[MAX_VERTEX_NUM];typedefintVertexType;//\u9876\u70b9\u6570\u636e\u7c7b\u578btypedefstructArcNode{intadjvex;intweight;structArcNode*nextarc;}ArcNode;typedefstructVNode{VertexTypedata;ArcNode*firstarc;}VNode,AdjList[MAX_VERTEX_NUM];typedefstruct{AdjListvertices;intvexnum,arcnum;intkind;}ALGraph;voidCreateDG(ALGraph&G){inti,j,k;ArcNode*p;cout>G.vexnum;cout>G.arcnum;cout>i>>j;p=(ArcNode*)malloc(sizeof(ArcNode));p->adjvex=j;p->nextarc=G.vertices[i].firstarc;G.vertices[i].firstarc=p;}}voidDisp(ALGraphG){inti,j;ArcNode*p;coutnextarc;j=1;}if(j==1)coutadjvex])dfs(G,p->adjvex);p=p->nextarc;}return;}voiddfs1(ALGraphG){inti;for(i=0;i>v;cout<<"\u6df1\u5ea6\u4f18\u5148\u5e8f\u5217:";dfs1(G);cout<

1\u3001head=malloc(sizeof(Node));//\u5934\u7ed3\u70b9

\u8fd9\u4e2a\u8fd4\u56de\u7684\u662f void *, \u4f60\u8981\u7684\u662f linklist, \u6240\u4ee5\u8981\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u4e00\u4e0b
head = (linklist)malloc(sizeof(Node));

2\u3001\u6c42\u94fe\u8868\u7684\u957f\u5ea6\u51fd\u6570\uff0c\u90a3\u4e2a\u5faa\u73af\u91cc\u9762 p \u5e94\u8be5\u5c0f\u5199\u5440\u3002

方法很多,可以在插入数据后再对线性表进行删改,也可以在插入前进行处理。

我这里代码是在插入前处理。

(注释掉的函数int getPNUM(struct Sqlist *st,int n);是我预留的,题2如果你想改成插入后,再对线性表素数进行查找,可以用这个函数。否则可以删除)。

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define LIST_INIT_SIZE 800

struct Sqlist{

    int *elem;

    int length;

    int listsize;

};

int insert2List(struct Sqlist *st,int num,int inx);//向线性表第inx个元素的位置插入一个元素。成功返回1,失败返回0

int findNum(struct Sqlist *st,int num);//在线性表中查找指定数字,存在返回1,不存在返回0

//int getPNUM(struct Sqlist *st,int n);//查找素数,返回第几n个素数的下标。未找到返回-1

void showList(struct Sqlist *st);//打印线性表

void clearList(struct Sqlist *st);//清空线性表

int main()

{

    int i,k,nums[LIST_INIT_SIZE],n,num,cnt,flag;

    struct Sqlist st={nums,0,LIST_INIT_SIZE};

    srand(time(NULL));


//--------------题1-----------------------------------------------------------------------

    n=100;

    k=1;

    printf("1、随机生成100个【100,200】之间的随机数,去除重复并保存到线性表
");

    while(n--)

    {

        num=rand()%101+100;

        printf("--%3d产生随机数%d
",k++,num);

        if(findNum(&st,num))

            printf("该数字已在线性表中存在,重复去除
");

        else

        {

            if(insert2List(&st,num,st.length+1))

                printf("该随机值已保存到线性表尾部
");

            else{

                printf("异常!插入失败!
");

                return 1;

            }


        }

    }

    showList(&st);


    clearList(&st);

//-------------题2----------------------------------------------------------------

    n=20;

    cnt=0;

    k=1;

    printf("1、随机生成20个【1,200】之间的随机数,在第一个素数后插入1个0,第二个素数后插入2个0,以此类推,最后输出所有元素
");

    while(n--)

    {

        num=rand()%200+1;

        printf("--%3d产生随机数%d
",k++,num);

        flag=1;

        for(i=2;i<num;i++)

            if(num%i==0)

            {

                flag=0;

                break;

            }

        if(flag)

        {

            cnt++;

            printf("该随机值是一个素数,在其尾部插入%d个0
",cnt);

            for(i=0;i<cnt;i++)

                num*=10;

            printf("该随机值变更为%d
",num);

        }

        if(insert2List(&st,num,st.length+1))

            printf("该随机值已保存到线性表尾部
");

        else{

            printf("异常!插入失败!
");

            return 1;

        }


    }

    showList(&st);

    return 0;

}

void clearList(struct Sqlist *st)//清空线性表

{

    st->length=0;

    printf("线性表数据已清除
");

}

void showList(struct Sqlist *st)//打印线性表

{

    int i;

    printf("当前线性表的数据为:
");

    for(i=0;i<st->length;i++)

        printf("%d ",st->elem[i]);

    printf("
");

}

int findNum(struct Sqlist *st,int num)//在线性表中查找指定数字,存在返回1,不存在返回0

{

    int *p=st->elem;

    while(p<=&st->elem[st->length-1])

        if(*p++==num)

            return 1;

    return 0;

}

/*

int getPNUM(struct Sqlist *st,int n)//查找素数,返回第几n个素数的下标。未找到返回-1

{

    int i,j,flag,cnt=0;

    for(i=0;i<st->length;i++)

    {

        flag=1;

        for(j=2;j<st->elem[i];j++)

            if(st->elem[i]%j==0)

            {

                flag=0;

                break;

            }

        if(flag)

            cnt++;

        if(cnt==n)

            return i;

    }

    return -1;

}

*/

int insert2List(struct Sqlist *st,int num,int inx)//向线性表第inx个元素的位置插入一个元素。成功返回1,失败返回0

{

    int i;

    if(st->length==st->listsize)

    {

        printf("线性表已满,插入失败!
");

        return 0;

    }

    if(inx<1 && inx>st->length+1)

    {

        printf("插入位置无效!线性表当前数据长度为%d,插入位置必须大于1且不能大于%d!
",st->length,st->length+1);

        return 0;

    }

    for(i=st->length;i>=inx;i--)

        st->elem[i]=st->elem[i-1];

    st->elem[inx-1]=num;

    st->length++;

    return 1;

}



如上图,把k位置的数据删除后,需要把k后面的元素逐个向前移动一次。

一共是n个元素,k前面(包括k)一共是k个元素,剩下需要移动的就是n-k个元素。答案选A



需要移动k+1、k+2。。。一直到n的元素,所以次数是n-(k+1)+1

  • C璇█涓閬鏁版嵁缁撴瀯棰樼洰姹傝В
    绛旓細寰堥珮鍏翠负妤间富瑙g瓟锛岄鍏堜綘鏈変笁涓妭鐐筁锛宲, q,鏍规嵁浣犵殑鎰忔滾涓哄ご缁撶偣锛宲鏄湁鏁版嵁x,e鐨勭粨鐐癸紝骞朵笖p鏄疞鐨勪笅涓涓粨鐐癸紝鐒惰宷=L->next锛燂紝浣哃涓嬩竴涓粨鐐规槸浠涔堬紵涓嶇煡閬撳惂锛佽繖閲屽簲璇ユ槸L->next=q锛岃鏄巕涔熸槸鎸囧悜p锛岃鐧戒簡灏辨槸p,q鎸囧悜鍚屼竴涓┖闂达紝鎺ョ潃妤间富蹇界暐浜唒=p->next!!!,p->next鏄粈...
  • 鏁版嵁缁撴瀯c璇█鐗堥棶棰
    绛旓細鎸夊垪涓轰富搴忓瓨鏀句簬涓涓繛缁殑瀛樺偍绌洪棿涓 a[10,20]涓鍒10涓厓绱狅紝閭d箞a[6,2]锛2琛ㄧず绗笁鍒楋紙鍓嶉潰鏈0,1锛夛紝鍓嶉潰涓ゅ垪灏辨槸20 绗笁鍒6涓紝寰楀埌20+6=26锛屽紑濮嬪湴鍧涓200,鍒200+26=226
  • 鎬ヨВ涓涓鏁版嵁缁撴瀯鐨勯(C璇█)
    绛旓細妤间富鐪熸槸澶湁缂樹簡,鎴戜篃姝e鏁版嵁缁撴瀯,杩欐槸鑰佸笀缁欐垜浠彁渚涚殑婧愪唬鐮,甯屾湜鏈夌敤Status MakeNode(Link *p,ElemType e) { /* 鍒嗛厤鐢眕鎸囧悜鐨勫间负e鐨勭粨鐐,骞惰繑鍥濷K;鑻ュ垎閰嶅け璐ャ傚垯杩斿洖ERROR */ *p=(Link)malloc(sizeof(LNode)); if(!*p) return ERROR; (*p)->data=e; return OK; } void FreeNode(Link ...
  • C璇█鏁版嵁缁撴瀯闂姹傝В!!!
    绛旓細scanf("%c",&ch);Init(); /*鍒濆鍖*/ MapRand(map);/*鐢熸垚杩峰*/ PrMap(map);/*鏄剧ず杩峰鍥*/ if(ch=='1')PeopleFind(map);/*浜哄伐鎺㈢储*/ else FindWay(map,1,1);/*绯荤粺鑷姩浠庝笅鏍1,1鐨勫湴鏂瑰紑濮嬫帰绱*/ Result();/*杈撳嚭缁撴灉*/ Close();} void Init(void)/*鍥惧舰鍒濆鍖*/ { ...
  • 鏁版嵁缁撴瀯c璇█鐗堣〃杈惧紡姹傚兼爣鍑嗙▼搴
    绛旓細鎬濊矾锛氫腑缂琛ㄨ揪寮忥紞鍚庣紑琛ㄨ揪寮忥紞姹傚 鍙傝冧唬鐮侊細include <iostream> include <cstdio> include <vector> include <cstdlib> include <cstring> include <iterator> include <algorithm> // 鍫嗘爤鐨勬暟缁勫疄鐜帮紝鏁扮粍鐨勫ぇ灏忓浐瀹氥倀emplate<class T> class stack { private:T *s; // 鏁扮粍鐨勯鍦板潃锛堟爤搴...
  • c璇█缂栫▼ 鏁版嵁缁撴瀯棰
    绛旓細*/#include <stdio.h>#include <malloc.h>#include <stdlib.h>typedef struct node{ int a; struct node *next;} NODE;// 鍒涘缓椤哄簭閾捐〃锛岄暱搴istSize锛屽苟鍒濆鍖朜ODE *createlist(NODE *head, int listSize, int arr[]);// 鎻掑叆涓涓妭鐐筰nt insertnode(NODE *head, int index, int...
  • c璇█鏁版嵁缁撴瀯闂姹傝В
    绛旓細A[0][0]鍜孉[2][2]鐩稿樊浜(2*n+2)涓厓绱狅紝涓嶢[3][3]鐩稿樊浜(3*n+3)涓厓绱狅紝鎵浠ワ紝676-644=32锛32/2*3=48锛644+48=692锛岄夋嫨c椤广傛弧鎰忚閲囩撼鍛
  • 鍏充簬鏁版嵁缁撴瀯(C璇█)鐨勫嚑涓
    绛旓細鏈澶氭瘮杈冩鏁板弬鑰冧弗钄氭晱銆鏁版嵁缁撴瀯銆嬬涔濈珷 鏌ユ壘 220椤点5.渚嬪鍥句腑杩欐5鏍戯紝鍋囪i=2锛2i=4涓嶅ぇ浜巒锛2i+1=5澶т簬n锛屾墍浠2杩欎釜缁撶偣娌℃湁鍙冲瓙鏍戙6.椤哄簭鏍堢殑绫诲瀷瀹氫箟锛歵ypedef struct{ char *base; //涔熷彲鐢‥lemType锛屽彧瑕佸畾涔変簡灏辫 char *top; int stacksize;}SqStackTp; //...
  • 鏁版嵁缁撴瀯c璇█鐗,姹傚ぇ绁炶В绛,鍐欎竴涓嬭缁嗙殑杩囩▼,璋㈣阿鍟
    绛旓細姝eソ鍦ㄥ涔鏁版嵁缁撴瀯锛屽垰濂界鍒拌繖涓浜 include <stdio.h> include <stdlib.h> include <stdbool.h> typedef struct LNode { int data;struct LNode *next;} LNode, *LinkList;LinkList Create(LinkList la); //鍒涘缓涓涓崟閾捐〃 void TravelList(LinkList la); //閬嶅巻鍗曢摼琛 void Delete...
  • 扩展阅读:数据结构c语言版严蔚 ... 数据结构用c还是c++ ... 数据结构是最难的课吗 ... c难还是数据结构难 ... 先学c还是先学数据结构 ... 数据结构必须学c吗 ... 数据结构知识点总结 ... c语言数据结构怎么学 ... 数据结构c语言版课后答案 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网