数据结构、算法、程序:已知一个带头结点的单链表La,其中存放着一组非零整数。 C++数据结构题:一个带头节点的单链表乘以一组整形数,分裂链...
\u6570\u636e\u7ed3\u6784\uff1a\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\u5c06\u4e00\u4e2a\u5e26\u5934\u7ed3\u70b9\u7684\u5355\u94fe\u8868A\u5206\u89e3\u6210\u4e24\u4e2a\u5e26\u5934\u7ed3\u70b9\u7684\u5355\u94fe\u8868A\u548cB\u7a0b\u5e8f\u5982\u4e0b\uff1a
#include
#include
typedef struct node
{
char data;
struct node *nextPtr;
}*LinkList, Lnode;
static void CreateList(LinkList *headPtr, LinkList *tailPtr, char ch);
static void Decompose(LinkList *headPtrA, LinkList *headPtrB, LinkList *tailPtrB);
static void VisitList(LinkList headPtr);
static void DestroyList(LinkList *headPtr, LinkList *tailPtr);
int main(void)
{
LinkList headPtrA = NULL, tailPtrA = NULL, headPtrB = NULL, tailPtrB = NULL;
char ch;
while (1)
{
printf("Enter ch('@'-quit): ");
scanf(" %c", &ch);
if (ch == '@')
{
break;
}
else
{
CreateList(&headPtrA, &tailPtrA, ch);
}
}
VisitList(headPtrA); /* \u6253\u5370\u5206\u89e3\u524d\u7684\u94fe\u8868 */
if (headPtrA != NULL) /* \u94fe\u8868\u4e0d\u7a7a\u7684\u60c5\u51b5\u5bf9\u5176\u8fdb\u884c\u5206\u89e3 */
{
Decompose(&headPtrA, &headPtrB, &tailPtrB); /* \u5bf9\u94fe\u8868\u8fdb\u884c\u5206\u89e3*/
}
else
{
printf("headPtrA is empty.\n");
}
VisitList(headPtrA); /* \u6253\u5370\u5206\u89e3\u540e\u7684\u94fe\u8868 */
VisitList(headPtrB);
DestroyList(&headPtrA, &tailPtrA); /* \u9500\u6bc1\u94fe\u8868 */
DestroyList(&headPtrB, &tailPtrB);
return 0;
}
static void CreateList(LinkList *headPtr, LinkList *tailPtr, char ch)
{
LinkList newPtr;
if ((newPtr = (LinkList)malloc(sizeof(Lnode))) == NULL)
{
exit(1);
}
newPtr -> data = ch;
newPtr -> nextPtr = NULL;
if (*headPtr == NULL)
{
newPtr -> nextPtr = *headPtr;
*headPtr = newPtr;
}
else
{
(*tailPtr) -> nextPtr = newPtr;
}
*tailPtr = newPtr;
}
static void Decompose(LinkList *headPtrA, LinkList *headPtrB, LinkList *tailPtrB)
{
int count = 0;
LinkList cA, pA;
char ch;
cA = NULL;
for (pA = *headPtrA; pA != NULL; cA = pA,pA = pA -> nextPtr)
{
ch = pA -> data;
count++;
if (count % 2 == 0)
{
CreateList(headPtrB, tailPtrB, ch);
cA -> nextPtr = pA -> nextPtr;
}
}
}
static void VisitList(LinkList headPtr)
{
while (headPtr != NULL)
{
printf("%c -> ", headPtr -> data);
headPtr = headPtr -> nextPtr;
}
printf("NULL\n");
}
static void DestroyList(LinkList *headPtr, LinkList *tailPtr)
{
LinkList tempPtr;
while (*headPtr != NULL)
{
tempPtr = *headPtr;
*headPtr = (*headPtr) -> nextPtr;
free(tempPtr);
}
*headPtr = NULL;
*tailPtr = NULL;
}
\u7ed3\u6784\u5316\u67e5\u8be2\u8bed\u8a00(Structured Query Language)\u7b80\u79f0SQL(\u53d1\u97f3\uff1a/ˈes kjuː ˈel/ "S-Q-L")\uff0c\u662f\u4e00\u79cd\u7279\u6b8a\u76ee\u7684\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u662f\u4e00\u79cd\u6570\u636e\u5e93\u67e5\u8be2\u548c\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\uff0c\u7528\u4e8e\u5b58\u53d6\u6570\u636e\u4ee5\u53ca\u67e5\u8be2\u3001\u66f4\u65b0\u548c\u7ba1\u7406\u5173\u7cfb\u6570\u636e\u5e93\u7cfb\u7edf\uff1b\u540c\u65f6\u4e5f\u662f\u6570\u636e\u5e93\u811a\u672c\u6587\u4ef6\u7684\u6269\u5c55\u540d\u3002
\u7ed3\u6784\u5316\u67e5\u8be2\u8bed\u8a00\u662f\u9ad8\u7ea7\u7684\u975e\u8fc7\u7a0b\u5316\u7f16\u7a0b\u8bed\u8a00\uff0c\u5141\u8bb8\u7528\u6237\u5728\u9ad8\u5c42\u6570\u636e\u7ed3\u6784\u4e0a\u5de5\u4f5c\u3002\u5b83\u4e0d\u8981\u6c42\u7528\u6237\u6307\u5b9a\u5bf9\u6570\u636e\u7684\u5b58\u653e\u65b9\u6cd5\uff0c\u4e5f\u4e0d\u9700\u8981\u7528\u6237\u4e86\u89e3\u5177\u4f53\u7684\u6570\u636e\u5b58\u653e\u65b9\u5f0f\uff0c\u6240\u4ee5\u5177\u6709\u5b8c\u5168\u4e0d\u540c\u5e95\u5c42\u7ed3\u6784\u7684\u4e0d\u540c\u6570\u636e\u5e93\u7cfb\u7edf, \u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u7ed3\u6784\u5316\u67e5\u8be2\u8bed\u8a00\u4f5c\u4e3a\u6570\u636e\u8f93\u5165\u4e0e\u7ba1\u7406\u7684\u63a5\u53e3\u3002\u7ed3\u6784\u5316\u67e5\u8be2\u8bed\u8a00\u8bed\u53e5\u53ef\u4ee5\u5d4c\u5957\uff0c\u8fd9\u4f7f\u5b83\u5177\u6709\u6781\u5927\u7684\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u7684\u529f\u80fd\u3002
1986\u5e7410\u6708\uff0c\u7f8e\u56fd\u56fd\u5bb6\u6807\u51c6\u534f\u4f1a\u5bf9SQL\u8fdb\u884c\u89c4\u8303\u540e\uff0c\u4ee5\u6b64\u4f5c\u4e3a\u5173\u7cfb\u5f0f\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u7684\u6807\u51c6\u8bed\u8a00\uff08ANSI X3. 135-1986\uff09\uff0c1987\u5e74\u5f97\u5230\u56fd\u9645\u6807\u51c6\u7ec4\u7ec7\u7684\u652f\u6301\u4e0b\u6210\u4e3a\u56fd\u9645\u6807\u51c6\u3002\u4e0d\u8fc7\u5404\u79cd\u901a\u884c\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u5728\u5176\u5b9e\u8df5\u8fc7\u7a0b\u4e2d\u90fd\u5bf9SQL\u89c4\u8303\u4f5c\u4e86\u67d0\u4e9b\u7f16\u6539\u548c\u6269\u5145\u3002\u6240\u4ee5\uff0c\u5b9e\u9645\u4e0a\u4e0d\u540c\u6570\u636e\u5e93\u7cfb\u7edf\u4e4b\u95f4\u7684SQL\u4e0d\u80fd\u5b8c\u5168\u76f8\u4e92\u901a\u7528\u3002
\u5728\u7ebf\u626b\u63cf\uff0c\u5bf9\u6bcf\u4e2a\u8282\u70b9\uff0c\u4e58\u4e00\u7ec4\u6570\uff0c\u5bf9\u5176\u4e2d\u6bcf\u4e2a\u7ed3\u679c\u505a\u4e2a\u8282\u70b9\uff1a\u5982\u679c\u662f\u5947\u6570\u5c31\u8fde\u63a5\u5230lb\uff0c\u5947\u6570\u63a5\u5230la\uff1b\u4e4b\u540e\u518d\u5bf9\u4e0b\u4e00\u4e2a\u8282\u70b9\u8fd9\u4e48\u64cd\u4f5c\u3002
楼上的说了原理,我还是给一个运行通过的程序你吧。遍历La,大于等于0放在Lb,其他放在Lc。
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
typedef struct NODE
{
struct NODE *next;
int data;
} T_NODE;
// 输入链表存储
void InputData(struct NODE * pHead)
{
int count = 10;
T_NODE *p = pHead;
while(count-- > 0)
{
p->next = (struct NODE *)malloc(sizeof(struct NODE));
scanf("%d", &(p->next->data));
p->next->next = NULL;
p = p->next;
}
p = pHead;
while(p->next)
{
printf("%d ", p->next->data);
p = p->next;
}
printf("\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
// 生成单链表头结点
struct NODE * pHead_La = (struct NODE *)malloc(sizeof(struct NODE));
pHead_La->data = 0;
pHead_La->next = NULL;
// 输入正负数
InputData(pHead_La);
struct NODE * pHead_Lb = (struct NODE *)malloc(sizeof(struct NODE));
pHead_Lb->data = 0;
pHead_Lb->next = NULL;
struct NODE * pHead_Lc = (struct NODE *)malloc(sizeof(struct NODE));
pHead_Lc->data = 0;
pHead_Lc->next = NULL;
T_NODE *p = pHead_La;
T_NODE *pPostive = pHead_Lb;
T_NODE *pNegative = pHead_Lc;
while(p->next)
{
// 正数和0
if (p->next->data >= 0)
{
pPostive->next = (struct NODE *)malloc(sizeof(struct NODE));
pPostive->next->data = p->next->data;
pPostive->next->next = NULL;
pPostive = pPostive->next;
}
else
{
pNegative->next = (struct NODE *)malloc(sizeof(struct NODE));
pNegative->next->data = p->next->data;
pNegative->next->next = NULL;
pNegative = pNegative->next;
}
p = p->next;
}
pPostive = pHead_Lb;
while(pPostive->next)
{
printf("%d ", pPostive->next->data);
pPostive = pPostive->next;
}
printf("\n");
pNegative = pHead_Lc;
while(pNegative->next)
{
printf("%d ", pNegative->next->data);
pNegative = pNegative->next;
}
printf("\n");
return 0;
}
遍历La,如果是负整数就接在Lb后边,要不然就接在Lc后边呗
绛旓細銆愮瓟妗堛戯細(1)鏁版嵁缁撴瀯 閲囩敤椤哄簭琛ㄥ畾涔夈(2)鎬濊矾 璁剧疆鍙橀噺min锛岄亶鍘嗘暣涓〃锛屼笉鏂洿鏂板綋鍓嶅凡缁忛亶鍘嗚繃鐨勫厓绱犵殑鏈灏忓煎嵆鍙備负鏂逛究璧疯锛屼簨鍏堝亣璁捐〃涓嶄负绌恒(3)绠楁硶 DataType min_seq(PSeqList palist){ /*姹傞潪绌洪『搴忚〃涓殑鏈灏忔暟鎹厓绱*/ DataType min;inti;min=palist-锛瀍lement[0]; /*...
绛旓細绠楁硶鍜绋嬪簭娌℃湁鍖哄埆锛屽湪鏁版嵁缁撴瀯涓簩鑰呮槸閫氱敤鐨勮繖鍙ヨ瘽鏄敊璇殑銆傜▼搴忓寘鍚绠楁硶锛岀畻娉灏辨槸绋嬪簭鐨勭伒榄傦紝涓涓闇瑕佸疄鐜扮壒瀹氬姛鑳界殑绋嬪簭锛屽疄鐜板畠鐨勭畻娉曞彲浠ユ湁寰堝绉嶏紝鎵浠ョ畻娉曠殑浼樺姡鍐冲畾鐫绋嬪簭鐨勫ソ鍧忋傜▼搴忓憳寰堢啛缁冪殑鎺屾彙浜嗙▼搴忚璁¤瑷鐨勮娉曪紝杩涜绋嬪簭璁捐锛岃蒋浠跺紑鍙戠殑鏃跺欏氨鏄璁″ソ鐨勭畻娉曪紝鍔犱笂杞欢宸ョ▼鐨 鐞嗚...
绛旓細鈥绠楁硶+鏁版嵁缁撴瀯=绋嬪簭鈥濇槸涓涓钁楀悕鐨勫叕寮忋傜▼搴忚繍琛岀殑杩囩▼灏辨槸鏁版嵁娴佺殑澶勭悊杩囩▼锛屾庝箞澶勭悊锛岄偅灏辨槸绠楁硶闂锛屾暟鎹庝箞缁勭粐锛岄偅灏辨槸鏁版嵁缁撴瀯浜嗐傜▼搴忚璁℃槸缁欏嚭瑙e喅鐗瑰畾闂绋嬪簭鐨勮繃绋嬶紝鏄蒋浠舵瀯閫犳椿鍔ㄤ腑鐨勯噸瑕佺粍鎴愰儴鍒嗐傜▼搴忚璁″線寰浠ユ煇绉嶇▼搴忚璁¤瑷涓哄伐鍏凤紝缁欏嚭杩欑璇█涓嬬殑绋嬪簭銆
绛旓細include<stdio.h> include<stdlib.h> struct node{ int data;struct node* pre;};void print(struct node *p)//鎵撳嵃閾炬爤 {while(p){printf("%d ",p->data);p=p->pre;} } int main(){int i;struct node *p,*q;for(i=1;i<11;i++)//1~10渚濇鍏ユ爤 {p=(struct node*)malloc(...
绛旓細鍦ㄩ珮绾ц瑷绋嬪簭涓張鍒嗕负锛氶潪缁撴瀯鐨勫師瀛愮被鍨嬪拰缁撴瀯绫诲瀷 鈼嗐鎶借薄鏁版嵁绫诲瀷(ADT)锛氭槸鎸涓涓鏁板妯″瀷浠ュ強瀹氫箟鍦ㄨ妯″瀷涓婄殑涓缁勬搷浣溿備竴涓娊璞$殑鏁版嵁绫诲瀷鐨勮蒋浠舵ā鍧楅氬父鍖呭惈銆瀹氫箟鍜岃〃绀哄拰瀹炵幇 鐢ㄤ笁鍏冪粍(D锛孲锛孭)锛氭暟鎹璞°佹暟鎹叧绯汇佸熀鏈搷浣 鈼 鏁版嵁缁撴瀯锛鎸囩殑鏄暟鎹箣闂寸殑鐩镐簰鍏崇郴锛屽嵆鏁版嵁鐨勭粍缁囧舰寮...
绛旓細1. 鏈夌┓鎬锛氱畻娉蹇呴』鍦ㄦ湁闄愭楠ゅ唴瀹屾垚銆2. 纭垏鎬э細绠楁硶鐨勬瘡涓姝ュ繀椤绘槑纭畾涔夈3. 杈撳叆锛氱畻娉曞彲鏈夐浂涓垨澶氫釜杈撳叆浠ュ畾涔夊垵濮嬬姸鎬併4. 杈撳嚭锛氱畻娉曡嚦灏鏈変竴涓杈撳嚭锛屽弽鏄犲杈撳叆鐨勫鐞嗙粨鏋溿5. 鍙鎬э細绠楁硶鑳藉疄闄呰繍琛岋紝涓斿彲閫氳繃鏈夐檺娆$瑪绠楀畬鎴愩傝绠楁満绉戝瀹跺凹鍏嬪姵鏂锋矁鎬濈殑钁椾綔銆鏁版嵁缁撴瀯鍗佺畻娉=绋嬪簭...
绛旓細楂樺垎鎮祻:c++瀹屾垚鏁版嵁缁撴瀯绠楁硶(鍏5閬撻 瑕佹墍鏈绋嬪簭 鏈濂芥湁鐐规枃瀛楄鏄 ) 1.閾捐〃閫嗙疆2.鍒犻櫎閾捐〃涓墍鏈夌殑鍋舵暟鑺傜偣3.灏嗛摼琛ㄤ腑鎵鏈夊鏁版帓鍦ㄥ伓鏁板墠4.浜屽弶鏍戠殑闈為掑綊閬嶅巻绠楁硶骞跺湪姝ゅ熀纭涓婃眰鍙跺瓙鐨勪釜鏁5.鎶樺崐鏌ユ壘绠楁硶... 1. 閾捐〃閫嗙疆2. 鍒犻櫎閾捐〃涓墍鏈夌殑鍋舵暟鑺傜偣3. 灏嗛摼琛ㄤ腑鎵鏈夊鏁版帓鍦ㄥ伓鏁板墠4. 浜屽弶鏍戠殑闈...
绛旓細1. 淇敼褰掑苟鎺掑簭绠楁硶涓殑`merge`鍑芥暟锛岀‘淇濇暟缁勭储寮曚笉浼氳秺鐣屻俙``cpp void merge(int data[], int p, int q, int r) { int i, j, k, n1, n2;n1 = q - p + 1;n2 = r - q;int L[n1], R[n2];for (i = 0; i < n1; i++) { L[i] = data[p + i];} for (i...
绛旓細1锛塱=0,j=n-1 2锛夊湪涓嶈秺鐣岀殑鍓嶆彁涓嬶紝i浠庡綋鍓嶅悜鍚庢壘鍒扮涓涓鍋舵暟a[i]3锛夎嫢i宸茶秺鐣岋紝鍒欒浆姝ラ8锛4锛夊湪涓嶈秺鐣岀殑鍓嶆彁涓嬶紝j浠庡綋鍓嶄綅缃悜鍓嶆壘鍒扮涓涓鏁癮[j]5锛夎嫢j宸茶秺鐣岋紝鍒欒浆姝ラ8锛6锛夎嫢i<j锛屽垯浜ゆ崲a[i]涓巃[j]锛岀劧鍚巌++,j-- 7锛夎嫢i<j锛屽垯杞楠2锛8锛绠楁硶缁撴潫 姝ょ畻娉曠殑...
绛旓細include <stdio.h> void delRe(int a[],int &length) //length涓鸿繑鍥炴暟缁勯暱搴 {int i,j,t;t=length;i=0; //i鎸囧悜鏃犻噸澶嶅尯鍩熺殑鏈熬 j=1; //j涓鸿妫鏌ョ殑鍏冪礌 while(j<t)if(a[i]==a[j]){j++;length--;} else {i++;a[i]=a[j];j++;} } main(){int i,n,ar...