用C语言建立一个顺序表,输入n个数并输出。程序编译没错,但运行不了,求指教。程序如下: c语言题,输入HeLLo,输出结果为hEllO,下面是我写的...

c++\u987a\u5e8f\u8868\u7a0b\u5e8f\uff0c\u7f16\u8bd1\u6ca1\u6709\u9519\u8bef\uff0c\u4f46\u662f\u8fd0\u884c\u65f6\u5c31\u662f\u4e0d\u884c\uff0c\u6c42\u6307\u6559

\u4f60\u7684\u9519\u8bef\u5728\u4e8e\u7ed3\u6784\u4f53\u4e2d\u4f7f\u7528\u4e86string\u7c7b\u578b\uff0c\u7ed3\u6784\u4f53\u5728\u4f60\u5b9a\u4e49\u7684\u65f6\u5019\u5927\u5c0f\u5c31\u56fa\u5b9a\u4e86\uff08\u5982\u679cstruct\u5927\u5c0f\u4e0d\u56fa\u5b9a\uff0c\u7f16\u8bd1\u5668\u5c31\u4e0d\u53ef\u80fd\u8ba9\u6211\u4eec\u4f7f\u7528sizeof\u6765\u8ba1\u7b97\u5927\u5c0f\uff09\uff0c\u800cstring\u662f\u4e0d\u5b9a\u957f\u5f97\uff0c\u6240\u4ee5\u8fd0\u884c\u7684\u65f6\u5019\u7a0b\u5e8f\u8bd5\u56fe\u8bbf\u95ee\u8d8a\u754c\u5185\u5b58\u65f6\u5c31\u4f1a\u51fa\u73b0\u201c\u67d0\u67d0\u67d0\u5185\u5b58\u4e0d\u80fd\u4e3aread!\u201d,\u4f60\u8981\u4e48\u4f7f\u7528\u5b9a\u957f\u5b57\u7b26\u6570\u7ec4\uff0c\u8981\u4e48\u5b9a\u4e49\u4e00\u4e2a\u6307\u9488\uff0c\u5148\u52a8\u6001\u5206\u914d\u5185\u5b58\uff0c\u518d\u5c06\u6307\u9488\u6307\u5411\u8be5\u5185\u5b58\uff0c\u5f53\u7136\uff0c\u5206\u914d\u5185\u5b58\u540e\u8bb0\u5f97\u91ca\u653e\u6389\uff0c\u4f60\u5f97\u7a0b\u5e8f\u5c31\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c\u5149\u77e5\u9053\u4f7f\u7528malloc\uff0c\u4e0d\u8bb0\u5f97free\u3002
\u53e6\u5916\uff0c\u4f60\u7684\u8fd9\u79cd\u601d\u8def\u4e0d\u662f\u4e0d\u884c\uff0c\u4f46\u5efa\u7acb\u94fe\u8868\u53ef\u80fd\u662f\u66f4\u597d\u5f97\u9009\u62e9\u3002
\u4f60\u7684\u7a0b\u5e8f\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\uff1aL.data[L.size]=item;\u7ed3\u6784\u4f53\u7684\u5185\u5bb9\u62f7\u8d1d\u53ef\u4e0d\u80fd\u8fd9\u4e48\u5e72\uff0c\u4f60\u5fc5\u987b\u5c06\u6bcf\u4e00\u9879\u62f7\u8d1d\u8fdb\u53bb\u3002
\u4ee5\u4e0b\u662f\u4ee3\u7801\u548c\u8fd0\u884c\u7ed3\u679c\uff1a
#include "iostream.h"#include "string.h"#include "stdlib.h"#define MAXSIZE 100
typedef struct{// string num;// string name;// string phone; char num[100];}Type;
typedef struct{ Type *data; int size;}SeqList;
int IniList(SeqList &L){ L.data=(Type*)malloc(MAXSIZE*sizeof(Type)); if(L.data==NULL) { cout<<"overflow"<<endl; return 0; } L.size=0; return 1;}
void DelRam(SeqList &L)//\u4e0d\u8981\u5fd8\u8bb0\u91ca\u653e\u5185\u5b58{ free(L.data);}
void InsertRear(SeqList &L,Type item)//*{ if (L.size ==MAXSIZE)cout<<"Insert:SeqList is full!"<<endl; strcpy(L.data[L.size].num,item.num); L.size++; //////}
int main(){ SeqList L; Type temp; int i; L.size=1;////
if( !IniList(L) ) cout>temp.num>>temp.name>>temp.phone ) while( cin>>temp.num )//\u8c03\u8bd5\u65b9\u4fbf { // if(temp.num == "0" && temp.name == "0" && temp.phone == "0") break; if(strcmp(temp.num,"0")==0)break;//\u8c03\u8bd5\u65b9\u4fbf else { InsertRear(L,temp); } }
for(i=0; i<L.size; i++) { cout<<L.data[i].num<<endl; } }
DelRam(L); return 0;}


\u53e6\u5916\uff0c\u4f5c\u4e3a\u7a0b\u5e8f\u5458\u5e94\u8be5\u4e25\u8c28\u4e00\u70b9\uff0c\u4e0d\u8981\u60f3\u5f53\u7136\uff0c\u6ca1\u6709\u8c03\u8bd5\u8fc7\u7684\u8bdd\u5efa\u8bae\u4e0d\u8981\u4e71\u7ed9\u7b54\u6848\u3002

get(str)\u4e0d\u662fget(str[i])
strlwr\u7684\u7528\u6cd5\u4e0d\u5bf9\uff0c\u4f60\u662f\u6bcf\u6b21\u90fd\u628a\u6574\u4e2a\u5b57\u7b26\u4e32\u8f93\u51fa\u4e86
\u4f60\u7684\u5faa\u73af\u53d8\u91cf\u4ece0\u523010\uff0c\u4f46\u662f\u4f60\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\u670910\u4e2a\u5417
assii\u7801\u4e0d\u5bf9\uff0c'a'\u662f97\u4e0d\u662f90\uff0c\u4f60\u4e0d\u8bb0\u5f97\u5c31\u4e0d\u8981\u5199assii\u7801\uff0c\u76f4\u63a5\u5199str[i]>'a'&&str[i]<'z'
\u6539\u6b63\u540e\u4ee3\u7801\u5982\u4e0b\uff0c\u8bf7\u91c7\u7eb3

#include#includevoid main(){ char str[10]; int i; gets(str); for(i=0;i=65&&str[i]=97&&str[i]<=122) printf("%c",str[i]-32); } printf("\n");}

楼主你好
修改如下:
#include<stdio.h>
#include<malloc.h>
#define MaxSize 40 //顺序表存储空间的初始分配量 --> (1)可以将MaxSize调大点儿
typedef struct
{
int *p;
int data[MaxSize];
int length;
int listsize;
}SeqList;
void InitList(SeqList &x) //定义顺序表的初始化函数
//--> (2)这里应该是SeqList &x 需要址传递 值传递是不会改变实参值的
{
x.p=(int*)malloc(MaxSize*sizeof(int));
if(!x.p) printf("存储分配失败!");
x.length=0;
x.listsize=MaxSize;
}
int main()
{
SeqList L;
InitList(L);
int i,n,k;
printf("请输入顺序表的长度n:");
scanf("%d",&n);
if(n<=0)
{
printf("数据错误!\n");
return 0;
}
if(n>MaxSize)
{
L.p=(int*)realloc(L.p,n*sizeof(int));
if(!L.p)printf("存储空间扩展失败!\n");
L.listsize=n;
}
printf("请输入数据:");
for(i=0;i<=n-1;i++)
{
scanf("%d",&k);
L.data[i]=k;
L.length++;
}
printf("线性表为:");
for(i=0;i<=n-1;i++)
printf("%d ",L.data[i]);
printf("\n");
return 1;
}

请看注释(1)(2)

希望能帮助你哈^_^

楼主定义的结构体中int*p;请问是干什么的?你给p申请空间干嘛呢??你最后存数据存子data[]里面,但是data长度只有5,所以只能存5个。。。。而且单步调试就会发现,在初始化的时候就会提醒你L没有初始化就使用啊。。。

void InitList(SeqList x) //定义顺序表的初始化函数

这里改成
void InitList(SeqList &x) //定义顺序表的初始化函数 加一个&

ps:若有不明白的地方,可以追问

  • 鐢–璇█寤虹珛涓涓『搴忚〃,杈撳叆n涓暟骞惰緭鍑恒傜▼搴忕紪璇戞病閿,浣嗚繍琛屼笉浜,姹...
    绛旓細int i,n,k;printf("璇杈撳叆椤哄簭琛鐨勯暱搴锛");scanf("%d",&n);if(n<=0){ printf("鏁版嵁閿欒锛乗n");return 0;} if(n>MaxSize){ L.p=(int*)realloc(L.p,n*sizeof(int));if(!L.p)printf("瀛樺偍绌洪棿鎵╁睍澶辫触锛乗n");L.listsize=n;} printf("璇疯緭鍏ユ暟鎹細");for(i=0;i<=n...
  • C璇█:1.寤虹珛鍚n涓暟鎹厓绱犵殑椤哄簭琛骞惰緭鍑鸿琛ㄤ腑鍚勫厓绱犵殑鍊煎強椤哄簭琛...
    绛旓細1銆乮nclude<iostream>#include<cstdio>#include<cstdlib>using namespace std;typedef struct Lnode{int *data;int length;int maxsize;struct Lnode *next;}List;void creatList(List &L,int n){int count = 0;L.data = (int*)malloc(sizeof(int)*n);if (!L.data)cout << "鐢宠绌洪棿澶...
  • 鐢–璇█鍒涘缓涓涓『搴忚〃骞跺畬鎴愭彃鍏ョ瓑鎿嶄綔
    绛旓細char a[],int n) { //寤虹珛椤哄簭琛int i;for(i = 0;i < n;i++) L->data[i] = a[i];L->length = n;}bool listinsert(sqlist *&L,int i,char e) { //鎻掑叆鏁版嵁鍏冪礌int j;if(i < 1 || i > L->length + 1) return false;i--;for(j = L->length;j > i;j--)...
  • 姹傞珮浜哄府缂涓涓鏈夊叧椤哄簭琛鐨C璇█绋嬪簭,鏈涢熷洖,闈炲父鎰熻阿
    绛旓細int charu(int a[]){ int n,i=0;while(a[i]!=0)i++;printf("\n璇ユ暟鍒楅暱搴︿负%d銆俓n",i);printf("璇杈撳叆瑕佹彃鍏ュ厓绱犵殑浣嶇疆n锛坣<=%d锛夛紙鍦ㄧ%d浣嶆彃鍏ュ嵆鍦ㄦ湯灏惧鍔涓涓鍏冪礌锛夛細\n",i+1,i+1);while(1){ scanf("%d",&n);if(n>=1&&n<=i+1)break;printf("璇疯緭鍏0-%d浠ュ唴...
  • 鐢╟璇█鍒涘缓涓涓『搴忚〃,骞跺畬鎴愬垱寤,鏄剧ず,鏌ユ壘,鎻掑叆,鍒犻櫎,閫鍑虹瓑浠诲姟
    绛旓細ss[k+1]=ss[k]; //ss[i]鍚庨潰鐨勫厓绱犲悜鍚庣Щ鍔 }while((k--)!=(i-1));printf("璇杈撳叆瑕佹彃鍏ョ殑鍊: ");scanf("%d",&e);ss[i-1]=e;n++;} /***鏄剧ず鍑芥暟***/ void show(int *ss){ int i=0;for(i;i<n;i++){ printf(" %d ->",ss[i]);} //printf("\b")...
  • C璇█缂栫▼:灏杈撳叆鐨N涓暣鏁伴噰鐢ㄥ啋娉℃帓搴忔寜鐓т粠灏忓埌澶х殑椤哄簭鎺掑垪骞朵笖杈撳嚭...
    绛旓細void BubbleSort(int L[]) { // 瀵椤哄簭琛L浣滃啋娉℃帓搴 int i,j,fini = 0;for (i = 1; i < n && !fini; i++) { fini = 1;for (j = 1; j <= n-i; j++)if (L[j] > L[j+1]) { L[0] = L[j];L[j] = L[j+1];L[j+1] = L[0];fini = 0;} } } ...
  • 鐢–璇█缂栧啓涓涓鏈夊叧椤哄簭琛鐨勭▼搴忎唬鐮
    绛旓細void CreateListR(SqList *L,ElemType a[],int n) /*鍒涘缓椤哄簭琛 */ { int i;for(i=0;i<n;i++)L=(SqList *)malloc(sizeof(SqList));L->elem[i]=a[i];L->length++;} void DispList(SqList *L) /* 杈撳嚭椤哄簭琛 */ { int i;if(ListEmpty(L)) return;for(i=0;...
  • 鏁版嵁缁撴瀯 鐢–璇█瀹炵幇椤哄簭琛鐨寤虹珛鍙婇亶鍘
    绛旓細include <stdio.h>#include <malloc.h>typedef struct nlist{ int *np; int len;//宸蹭娇鐢ㄥ湴鍧涓暟 int maxlen;//鏈澶у湴鍧涓暟}NLT;NLT *createlist();//鍒涘缓椤哄簭琛int addtolist(NLT *nlist);//鍚戦『搴忚〃鎻掑叆鍏冪礌void pList(NLT *nlist);//閬嶅巻椤哄簭琛╥nt main(){ int i...
  • 姹涓涓敤C璇█鍐欑殑椤哄簭琛绋嬪簭!!
    绛旓細int i,n,chg;printf("ÊäÈëµ卤Ç掳ÅÅÐ貌Á´卤铆³陇¶È:");scanf("%d",&n);h=head;for(i=0;i<n-1;i )for(q=h->next,p=q->next;p!=NULL;q=p,p=p->next)if(q->data>p->data...
  • 鎬庢牱鐢╟璇█瀹炵幇鐢椤哄簭琛瀹屾垚杈撳叆涓涓插瓧绗,鍦ㄦ彃鍏,璁$畻闀垮害,鍒犻櫎璁$畻...
    绛旓細char str[maxsize]; printf("杈撳叆瀛楃涓:"); gets(str); for(int i=0;str[i]!='\0';i++){ //杩欎釜鍦版柟鐢'\0'瀛楃灏卞ソ浜嗭紝鍥犱负瀛楃鏁扮粍鎴栬呭瓧绗︿覆鐨勭粨鏉熺閮芥槸\0 L->data[i]=str[i]; L->num++; }} int InsertList( SeqList *L,int i,...
  • 扩展阅读:学编程一年大概多少钱 ... 编程必背100个代码 ... 少儿编程一般学几年 ... 十大编程语言排名 ... 高中生想学编程怎么学 ... 学编程的十大忠告 ... 自学编程10秒学会 ... 编程的四个基本步骤 ... 三类人不适合学编程 ...

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