求两道数据结构算法 我有两道数据结构的问题 希望能给出详细解答和做题步骤?

\u6570\u636e\u7ed3\u6784\uff0c\u5b9e\u73b0\u4e0b\u5217\u4e24\u9053\u7b97\u6cd5(\u6025)

\u4f60\u597d\u54e6\u697c\u4e3b~ \u5f88\u9ad8\u5174\u770b\u5230\u4f60\u7684\u95ee\u9898\u3002 \u4f46\u662f\u53c8\u5f88\u9057\u61be\u5230\u73b0\u5728\u8fd8\u6ca1\u6709\u4eba\u56de\u7b54\u4f60\u7684\u95ee\u9898\u3002\u4e5f\u53ef\u80fd\u4f60\u73b0\u5728\u5df2\u7ecf\u5728\u522b\u7684\u5730\u65b9\u627e\u5230\u4e86\u7b54\u6848\uff0c\u90a3\u5c31\u5f97\u606d\u559c\u4f60\u5566\u3002 \u53ef\u80fd\u662f\u4f60\u95ee\u7684\u95ee\u9898\u6709\u4e9b\u4e13\u4e1a\u4e86\uff0c\u6ca1\u4eba\u4f1a\u3002\u6216\u8005\u522b\u4eba\u6ca1\u6709\u9047\u5230\u6216\u8005\u63a5\u89e6\u8fc7\u4f60\u7684\u95ee\u9898\uff0c\u6240\u4ee5\u5e2e\u4e0d\u4e86\u4f60\u3002\u5efa\u8bae\u4f60\u53bb\u95ee\u9898\u7684\u76f8\u5173\u8bba\u575b\u53bb\u6c42\u52a9\uff0c\u90a3\u91cc\u7684\u4eba\u901a\u5e38\u6bd4\u8f83\u591a\uff0c\u4e5f\u4f1a\u6bd4\u8f83\u70ed\u5fc3\uff0c\u80fd\u5feb\u70b9\u5e2e\u4f60\u89e3\u51b3\u95ee\u9898\u3002 \u5e0c\u671b\u6211\u7684\u56de\u7b54\u80fd\u591f\u5e2e\u5230\u4f60\uff01 \u795d\u4f60\u597d\u8fd0\u3002\u3002\u3002\u3002\u3002

\u9ebb\u70e6\u91c7\u7eb3\uff0c\u8c22\u8c22!

\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5730\u4f4d\u5bf9\u4e8e\u4e00\u4e2a\u7a0b\u5e8f\u5458\u6765\u8bf4\u4e0d\u8a00\u800c\u55bb\u3002\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u4e0d\u662f\u6765\u529d\u4f60\u4eec\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\uff0c\u4e5f\u4e0d\u662f\u6765\u548c\u4f60\u4eec\u8bf4\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6709\u591a\u91cd\u8981\u3002
\u4e3b\u8981\u662f\u6700\u8fd1\u51e0\u5929\u540e\u53f0\u6709\u8bfb\u8005\u95ee\u6211\u662f\u5982\u4f55\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\uff0c\u6709\u6ca1\u6709\u4ec0\u4e48\u6377\u5f84\uff0c\u662f\u8981\u770b\u89c6\u9891\u8fd8\u662f\u770b\u4e66\uff0c\u53bb\u54ea\u5237\u9898\u7b49.....\u800c\u4e14\u6709\u4e9b\u8fd8\u662f\u5927\u4e09\u5927\u56db\u7684\uff0c\u641e\u7684\u6211\u90fd\u66ff\u4f60\u4eec\u7740\u6025\u3001\u62c5\u5fc3.....
\u6240\u4ee5\u6211\u4eca\u5929\u5c31\u5206\u4eab\u4e0b\u81ea\u5df1\u5e73\u65f6\u90fd\u662f\u600e\u4e48\u5b66\u4e60\u7684\u3002
\u5b66\u4e60\u7b97\u6cd5\u7684\u6377\u5f84\u5c31\u662f\u591a\u5237\u9898
\u8bf4\u5b9e\u8bdd\uff0c\u8981\u8bf4\u6377\u5f84\uff0c\u6211\u89c9\u5f97\u5c31\u662f\u811a\u8e0f\u5b9e\u5730\u7740\u591a\u52a8\u624b\u53bb\u5237\u9898\uff0c\u591a\u5237\u9898\u3002
\u4f46\u662f\uff0c\u5982\u679c\u4f60\u662f\u5c0f\u767d\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f60\u8fde\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u94fe\u8868\u3001\u6811\u4ee5\u53ca\u5e38\u89c1\u7684\u7b97\u6cd5\u601d\u60f3\uff0c\u5982\u9012\u5f52\u3001\u679a\u4e3e\u3001\u52a8\u6001\u89c4\u5212\u8fd9\u4e9b\u90fd\u6ca1\u5b66\u8fc7\uff0c\u90a3\u4e48\uff0c\u6211\u4e0d\u5efa\u8bae\u4f60\u53bb\u5237\u9898\u7684\u3002\u800c\u662f\u5148\u53bb\u627e\u672c\u4e66\u5148\u53bb\u5b66\u4e60\u8fd9\u4e9b\uff0c\u7136\u540e\u518d\u53bb\u5237\u9898\u3002
\u4e5f\u5c31\u662f\u8bf4\uff0c\u5047\u5982\u4f60\u8981\u53bb\u8bf8\u5982leetcode\u8fd9\u4e9b\u7f51\u7ad9\u5237\u9898\uff0c\u90a3\u4e48\uff0c\u4f60\u8981\u5148\u5177\u5907\u4e00\u5b9a\u7684\u57fa\u7840\uff0c\u8fd9\u4e9b\u57fa\u7840\u5305\u62ec\uff1a
1\u3001\u5e38\u89c1\u6570\u636e\u7ed3\u6784\uff1a\u94fe\u8868\u3001\u6811(\u5982\u4e8c\u53c9\u6811)\u3002
2\u3001\u5e38\u89c1\u7b97\u6cd5\u601d\u60f3\uff1a\u8d2a\u5a6a\u6cd5\u3001\u5206\u6cbb\u6cd5\u3001\u7a77\u4e3e\u6cd5\u3001\u52a8\u6001\u89c4\u5212\uff0c\u56de\u6eaf\u6cd5\u3002
\u4ee5\u4e0a\u5217\u51fa\u6765\u7684\u7b97\u662f\u6700\u57fa\u672c\u7684\u5427\u3002\u5c31\u662f\u8bf4\u4f60\u5237\u9898\u4e4b\u524d\uff0c\u8981\u628a\u8fd9\u4e9b\u8fc7\u4e00\u904d\u518d\u53bb\u5237\u9898\u3002\u5982\u679c\u4f60\u8fde\u8fd9\u4e9b\u6700\u57fa\u672c\u7684\u90fd\u4e0d\u77e5\u9053\u7684\u8bdd\uff0c\u90a3\u4e48\u4f60\u518d\u5237\u9898\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u5f88\u96be\u53d7\u7684\uff0c\u601d\u8def\u4e5f\u4f1a\u76f8\u5bf9\u6bd4\u8f83\u5c11\u3002
\u603b\u4e4b\uff0c\u5343\u4e07\u4e0d\u8981\u6025\uff0c\u5148\u628a\u8fd9\u4e9b\u57fa\u672c\u7684\u8fc7\u4e00\u904d\uff0c\u529b\u6c42\u7406\u89e3\uff0c\u518d\u53bb\u5237\u9898\u3002\u8fd9\u4e9b\u57fa\u7840\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\uff0c\u6211\u662f\u5728\u5927\u4e00\u7b2c\u4e8c\u5b66\u671f\u5b66\u7684\uff0c\u6211\u6ca1\u770b\u89c6\u9891\uff0c\u6211\u662f\u901a\u8fc7\u770b\u4e66\u5b66\u7684\uff0c\u90a3\u65f6\u5019\u770b\u7684\u4e66\u662f\uff1a
1\u3001\u7b97\u6cd5\u5206\u6790\u4e0e\u5206\u6790\u57fa\u7840\uff1a\u8fd9\u672c\u6bd4\u8f83\u7b80\u5355\uff0c\u63a8\u8350\u65b0\u624b\u770b\u3002
2\u3001\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5206\u6790---C\u8bed\u8a00\u63cf\u8ff0\uff1a\u4ee3\u7801\u7528C\u5199\u7684\uff0c\u63a8\u8350\u770b\u3002
3\u3001\u6311\u6218\u7a0b\u5e8f\u8bbe\u8ba1\u7ade\u8d5b(\u7b2c\u4e8c\u7248)\uff1a\u4e5f\u662f\u5f88\u4e0d\u9519\u7684\u4e00\u672c\u4e66\uff0c\u63a8\u8350\u770b\u3002

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

//-----------线性表的单链表存储结构-------------
typedef int ElemType;
typedef struct Node{
ElemType data;
struct Node *next;
} *LNode, *LinkList;
//----------线性表的单链表基本操作------------
LinkList InitList(void); //构造一个空的线性表
LNode NewLNode(ElemType X);//构造一个数据域为X的新结点
LNode FindPrefious(ElemType X, LinkList L);
//初始条件:线性表L已存在。 操作结果:在线性表L中寻找值为X的结点,若找到则返回该结点的前驱,否则返回NULL。
void ListDelete(LNode Pre);//初始条件:线性表L中结点P已找到。 操作结果:删除该结点。
//初始条件:线性表L中结点P已找到,新结点S已构造。 操作结果:在该结点之前插入新结点X。
void ListInsert(LNode Pre, LNode S);
LinkList CreateList(ElemType a[], ElemType len); //用来构造一个链表
void Union(LinkList A, LinkList B, LinkList * L); //合并两个串
int Strcompare(LinkList S, LinkList T); //比较两个串的大小
void PrintLink(LinkList L); //输出链表

int main(void)
{
LNode LA, LB, L;
ElemType A[5]={1,2,3,4,5}, B[8]={3,4,5,6,7,8,9,11};
//把集合A和B分别存入两个单向链表LA和LB中
LA = CreateList(A, 5);
LB = CreateList(B, 8);
PrintLink(LA); //输出链表
PrintLink(LB); //输出链表

Union(LA, LB, &L);//合并两个串
PrintLink(L); //输出链表

if (Strcompare(LA, LB) > 0) //比较两个串的大小
printf("LA > LB\n");
else if (Strcompare(LA, LB) < 0)
printf("LA < LB\n");
else
printf("LA = LB\n");

system("pause");
return 0;
}

void Union(LinkList A, LinkList B, LinkList * L)//合并两个串
{
ElemType X;
LNode S, P;

*L = InitList(); //构造一个空的线性表

P = A->next;
while(P) //遍历链表A
{
X = P->data;
S = NewLNode(X); //构造一个数据域为X的新结点
ListInsert(*L, S); //把新结点S插到头结点后面。
P = P->next;
}

P = B->next;
while(P) //遍历链表B
{
X = P->data;
if(!FindPrefious(X, A)) //看看A中是否有数据值相同的结点,只保留其中一个
{
S = NewLNode(X); //构造一个数据域为X的新结点
ListInsert(*L, S); //把新结点S插到头结点后面。
}
P = P->next;
}
}

void PrintLink(LinkList L) //输出链表
{
LNode P = L->next;
while(P) //遍历链表L
{
printf("%d ", P->data);
P = P->next;
}
printf("\n");
}

int Strcompare(LinkList S, LinkList T)//比较两个串的大小
{
LNode PS, PT;

PS = S->next;
PT = T->next;

while(PS && PT) //遍历链表A
{
if (PS->data > PT->data)
return 1;
else if (PS->data < PT->data)
return -1;
PS = PS->next;
PT = PT->next;
}

if (PS)
return 1;
else if (PT)
return -1;
else
return 0;
}

LinkList CreateList(ElemType a[], ElemType len)//用来构造一个链表
{
LNode L, S;
ElemType i;

L = InitList(); //构造一个空的线性表
for(i=0; i<len; i++)
{
S = NewLNode(a[i]); //构造一个数据域为a[i]的新结点
ListInsert(L, S); //把新结点S插到头结点后面。
}
return L;
}

LinkList InitList(void) //构造一个空的线性表
{
LNode Head;

Head = (LNode)malloc(sizeof(struct Node)); //为链表的头结点分配空间
if(!Head)
{
printf("Out of space!");
return NULL;
}
Head->next = NULL;
return Head;//返回头结点
}

LNode NewLNode(ElemType X)//构造一个数据域为X的新结点
{
LNode S;

S = (LNode)malloc(sizeof(struct Node));//为新结点分配空间
if(!S)
{
printf("Out of space!");
return NULL;
}
S->data = X;
S->next = NULL;
return S;//返回新结点
}

//线性表L已存在。 操作结果:在线性表L中寻找值为X的结点,若找到则返回该结点的前驱,否则返回NULL。
LNode FindPrefious(ElemType X, LinkList L)
{
LNode P = L;

while(P->next && P->next->data != X)//遍历链表寻找值为X的结点
P = P->next;
if(!P->next) //如果找不到值为X的结点,返回NULL
return NULL;
else //若找到则返回该结点的前驱P
return P;
}

//初始条件:线性表L中结点P已找到,新结点S已构造。。 操作结果:在该结点之前插入新结点X。
void ListInsert(LNode Pre, LNode S)
{
S->next = Pre->next;
Pre->next = S;
}

呵呵,一写这样的程序就找到了上学时的感觉。
本程序在VS2008下运行通过,如需ANSI C++编译器下运行,请将Console替换成标准输入输出Cout和Cin,如需ANSI C编译器下运行,请将Console替换成printf和Scanf
程序如下:
#include "stdlib.h"
#include "string.h"
using namespace System;

#define NULL 0
#define TRUE 1
#define FALSE 0

typedef int BOOL;

typedef struct NODE
{
int data;
struct NODE*next;
}Node;

/***********测试数据*************************/
void CreateList(Node *pHeadA,Node *pHeadB) //构造两个链表
{
Node *p,*q;

int i=0;

p=pHeadA;

for(i=0;i<100;i++)
{
q=new Node;
q->data=i;
q->next=NULL;
p->next=q;
p=q;

}

p=pHeadB;
for(i=50;i<150;i++)
{
q=new Node;
q->data=i;
q->next=NULL;
p->next=q;
p=q;

}

}
void DisplayList(Node *pList) //遍历链表
{
Node *p;

if(pList->next!=NULL) p=pList->next;

while (p!=NULL)
{
Console::Write(p->data);
Console::Write(" ");
p=p->next;
}
}
/******************************************/

BOOL IsExistInList(Node *pList,int nValue) //判断值nValue是否在链表中存在
{
Node *p;

if(pList->next!=NULL) p=pList->next;
else
{
return FALSE;
}

while (p!=NULL)
{
if(p->data==nValue) return TRUE;
p=p->next;
}

return FALSE;

}

void Union(Node *pListA,Node *pListB,Node *pListC) //将链表A,B,连接成表C
{

Node *p,*q;

p=pListC;
while(pListA!=NULL || pListB!=NULL) //遍历A,B链表的所有有节点,逐一判断在pListC中是否存在,不存在则插入
{
pListA=pListA->next;
pListB=pListB->next;

if(pListA!=NULL) //对于链表A
{
if(!IsExistInList(pListC,pListA->data)) //如果不存在则插入
{
q=new Node;
q->data=pListA->data;
q->next=NULL;
p->next=q;
p=q;
}

}
if(pListB!=NULL) //对于链表B
{
if(!IsExistInList(pListC,pListB->data)) //如果不存在则插入
{
q=new Node;
q->data=pListB->data;
q->next=NULL;
p->next=q;
p=q;
}

}
}

}

int Strcompare(char *pStrA,char *pStrB) //字符串比较
{
char *p1=pStrA,*p2=pStrB;
while(*p1!='\0' && *p2!='\0')
{
if(*p1<*p2) return -1;
else if(*p1>*p2) return 1;
p1++;
p2++;
}
if (strlen(p1)>strlen(p2))
{
return 1;
}
else if(strlen(p1)<strlen(p2))
{
return -1;
}
else return 0;
}
int main(void)
{

Node *pHeadA,*pHeadB; //初始化链表头
pHeadA=new Node;
pHeadA->data=-999;
pHeadA->next=NULL;
pHeadB=new Node;
pHeadB->data=-999;
pHeadB->next=NULL;

CreateList(pHeadA,pHeadB); //建立两个测试链表
DisplayList(pHeadA); //显示链表
Console::WriteLine();
DisplayList(pHeadB);
Console::WriteLine();

Node *pHeadC; //建立目的链表
pHeadC=new Node;
pHeadC->data=-999;
pHeadC->next=NULL;
Union(pHeadA,pHeadB,pHeadC); //合并链表
DisplayList(pHeadC);

int n=Strcompare("wuchunlei","wuchunlei"); //比较两个字符串
n=Strcompare("wuchunlei","wuchunleil");
n=Strcompare("wuchunleil","wuchunlei");
n=Strcompare("wuchunlai","wuchunlei");
n=Strcompare("wuchunlei","wuchunlai");
n=Strcompare("wuchunlei","auchunlei");

Console::Read();

return 0;
}

  • 姹備袱閬撴暟鎹粨鏋勭畻娉
    绛旓細void Union(LinkList A, LinkList B, LinkList * L); //鍚堝苟涓や釜涓 int Strcompare(LinkList S, LinkList T); //姣旇緝涓や釜涓茬殑澶у皬 void PrintLink(LinkList L); //杈撳嚭閾捐〃 int main(void){ LNode LA, LB, L;ElemType A[5]={1,2,3,4,5}, B[8]={3,4,5,6,7,8,9,11}...
  • 鏁版嵁缁撴瀯绠楁硶璁捐棰樺拰2涓绠楅(閲嶅垎)
    绛旓細涓暟锛1锛2锛1锛2锛1锛2锛1锛2锛3锛2锛1锛0 2锛2^0+2+2^2+2^3+鈥︹+2^(h-1)=2^h-1 銆嬨媅2^h-1]<n<[2^(h+1)-1]鎵浠=[log2 (n+1)]鍚戜笅鍙栨暣 绠楁硶璁捐棰樺ソ涔呮病鐪嬩簡锛屽緢浼よ剳绛 甯屾湜瀵逛綘鏈夌敤
  • 姹傚姪澶х鏁版嵁缁撴瀯绠楁硶棰!璋㈣阿璋㈣阿!
    绛旓細鍙互鍏堣a[0]=b[0]锛岀劧鍚庝粠b鏁扮粍鐨勭浜屼釜鍏冪礌渚濇鍚慳涓彃鍏ワ紝鍏蜂綋浠g爜鍙互鍙傝冧笅闈㈡潵鏀广傛渶濂借嚜宸卞鐫鍐欎竴涓嬨
  • 楂樺垎鎮祻:c++瀹屾垚鏁版嵁缁撴瀯绠楁硶(鍏5閬撻 瑕佹墍鏈夌▼搴 鏈濂芥湁鐐规枃瀛楄鏄...
    绛旓細1.閾捐〃閫嗙疆2.鍒犻櫎閾捐〃涓墍鏈夌殑鍋舵暟鑺傜偣3.灏嗛摼琛ㄤ腑鎵鏈夊鏁版帓鍦ㄥ伓鏁板墠4.浜屽弶鏍戠殑闈為掑綊閬嶅巻绠楁硶骞跺湪姝ゅ熀纭涓婃眰鍙跺瓙鐨勪釜鏁5.鎶樺崐鏌ユ壘绠楁硶... 1. 閾捐〃閫嗙疆2. 鍒犻櫎閾捐〃涓墍鏈夌殑鍋舵暟鑺傜偣3. 灏嗛摼琛ㄤ腑鎵鏈夊鏁版帓鍦ㄥ伓鏁板墠4. 浜屽弶鏍戠殑闈為掑綊閬嶅巻绠楁硶 骞跺湪姝ゅ熀纭涓婃眰鍙跺瓙鐨勪釜鏁5. 鎶樺崐鏌ユ壘绠楁硶 灞曞紑  鎴戞潵绛...
  • 鏁版嵁缁撴瀯鐨绠楁硶 璇峰府蹇欒В绛旇繖涓ら亾棰榽
    绛旓細mid=(low+high)/2;if(x==a[mid])return mid;else if(x<a[mid])return bin_search(a,low,mid-1,x);else return bin_search(a,mid+1,high,x);} } //浠ヤ笂鏄掑綊绠楁硶锛屾槗鐞嗚В int main(){ int a[5]={1,2,3,4,5};bin_search(a,1,5,4);system("pause");return 0;} incl...
  • 璺眰鏁版嵁缁撴瀯绠楁硶璁捐棰!!!浜屽弶鏍戠殑!!
    绛旓細void FillParent(Node* t){ if (t->lchild != NULL){ t->lchild->parent = t;FillParent(t->lchild);} if (t->rchild != NULL){ t->rchild->parent = t;FillParent(t->rchild);} }
  • 璇锋暀4閬撴暟鎹粨鏋鐨绠楁硶棰
    绛旓細int argc, char const *argv[]){Node *L1;L1=Statistics("3Ada3#helloworld");printf("\n%f",Probability(L1,'l'));return 0;}3.include <stdio.h>#include <stdlib.h>typedef struct _Element{int element;struct _Element *next;} Element;typedef struct _Elements{int element1,element2...
  • 姹傝В涓閬鏁版嵁缁撴瀯绠楁硶棰
    绛旓細涓鍏辨湁n涓暟瀛楋紝鍒欏彲浠ヤ粠涓棿浣嶇疆寮濮嬫墿灞曘傦紙褰搉=2k鏃讹紝l=r=n/2锛涘綋n=2k+1鏃讹紝l=n/2,r=n/2+1锛夌劧鍚庡垎鍒線涓よ竟鎵╁睍銆傞愪綅姣旇緝銆備唬鐮佽繕鏄瘮杈冩竻鏅扮殑鍚с傘傚鏋滄湁浠涔堜笉鐞嗚В璇风户缁拷闂俻ython:int lef,r,prevlef = n/2r = n/2prev = -99999999 #鍘熻皡鎴戜笉鐭ラ亾python鐨-INF鎬庝箞鍐...
  • 鏁版嵁缁撴瀯绠楀彂棰 绠楁硶璁捐棰 1銆佸亣璁炬湁涓や釜渚濆厓绱犲奸掑鏈夊簭鎺掑垪鐨勭嚎鎬...
    绛旓細head2=q2;else q2->next=p2;q2=p2;p2=(struct list*)malloc(sizeof(struct list));scanf("%d",&p2->data);} q2->next=NULL;unionlist();} void unionlist(){ struct list *head,*p;int i=0;p1=head1;p2=head2;head=p=(struct list*)malloc(sizeof(struct list));p->data=0;...
  • 姹傝В,鏁版嵁缁撴瀯涓绠楁硶
    绛旓細瀛樺叆鍝堝笇琛: 涓嬫爣 0 1 2 3 4 5 6 7 8 9 10 鍏抽敭瀛 33 13 2 24 16 7 19杩欏氨鏄渶鍚庡緱鍒扮殑鍝堝笇琛.//C璇█娴嬭瘯绋嬪簭#include<stdio.h>#include<stdlib.h>#define SUCCESS 1#define UNSUCCESS 0#define HASHSIZE 11#define NULLKEY -1typ...
  • 扩展阅读:数据分析公式一览表 ... 数据的排序方式主要有 ... 一键生成数据分析图 ... 怎么做图表数据分析图 ... 公式规律大全 ... 一列数据怎么自动分列 ... 数据分析柱状图 ... 常见数据分析图表 ... 十大基本算法 ...

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