1.数据结构编程求救
2.数据结构c语言怎么运行啊
数据结构编程求救
试验一:
#include<iostream>
#include<string>
using namespace std;
struct List
{
int num;
List *next;
};
List *head=NULL;
List* CreateList()
{
List *pL;
List *pEnd;
pL=new List;
head=pL;
pEnd=pL;
cout<<"请输入节点的数目,以 0 结束"<<endl;
cin>>pL->num;
while(pL->num!=0)
{
pEnd->next=pL;
pEnd=pL;
pL=new List;
cin>>pL->num;
}
delete pL;
pEnd->next=NULL;
return head;
}
void ShowList(List *head)
{
cout<<endl;
cout<<"链表节点如下:"<<endl;
while(head)
{
cout<<head->num<<endl;
head=head->next;
}
}
void InsertList(List *head,int num)
{
List *list =new List;
List *l;
while(head)
{
l=head;
head=head->next;
}
list->num=num;
list->next=NULL;
l->next=list;
}
void DeleteList(List *head, int num)
{
List *l;
if(head->num==num)
{
l=head;
head=head->next;
::head=head;
delete l;
return ;
}
List *l1=head;
while(head)
{
if(head->next==NULL){
cout<<"找不到不要删除的数字."<<endl;
return ;
}
if(head->next->num==num)
{
l=head->next;
head->next=l->next;
delete l;
::head=l1;
cout<<"操作成功"<<endl;
return ;
}
head=head->next;
}
cout<<"找不到不要删除的数字."<<endl;
}
int GetListNum(List *head)
{
int num=0;
while(head)
{
num++;
head=head->next;
}
return num;
}
int main()
{
string str;
begin:
cout<<"1->增加链表 2->显示链表 3->插入节点 4->删除节点 5->节点数目"<<endl;
cin>>str;
if(str[0]=='1')
{
CreateList();
}
else if(str[0]=='2')
{
if(head==NULL)
{
cout<<"你的链表现在是空的,请增加链表"<<endl;
getchar();
getchar();
system("cls");
goto begin;
}
ShowList(head);
}
else if(str[0]=='3')
{
if(head==NULL)
{
cout<<"你的链表现在是空的,请增加链表"<<endl;
getchar();
getchar();
system("cls");
goto begin;
}
int num;
cout<<"请输入要插入的数字:"<<endl;
cin>>num;
InsertList(head,num);
}
else if(str[0]=='4')
{
if(head==NULL)
{
cout<<"你的链表现在是空的,请增加链表"<<endl;
getchar();
getchar();
system("cls");
goto begin;
}
int num;
cout<<"请输入要删除的数字:"<<endl;
cin>>num;
DeleteList(head,num);
}
else if(str[0]=='5')
{
cout<<"节点数是:"<<GetListNum(head)<<endl;
}
else
{
cout<<"输入错误,请重新输入.";
}
if(str[0]!='Q' && str[0]!='q'){
cout<<endl<<endl;
getchar();
getchar();
system("cls");
goto begin;
}
}
试验二:
#include<iostream>
#include<string>
using namespace std;
struct Stack {
char c;
Stack *pNext;
};
void InitStack(Stack *&s)
{
s=NULL;
}
char Peek(Stack *s)
{
if(s==NULL) {
cout<<"栈是空的."<<endl;
return -1;
}
return s->c;
}
void Push(Stack *&s,Stack *newS)
{
newS->pNext=s;
s=newS;
}
char Pop(Stack *&s)
{
if(s==NULL)
{
cout<<"栈是空的."<<endl;
return -1;
}
Stack *pNext;
char c;
if(s)
{
pNext=s->pNext;
c=s->c;
delete s;
s=pNext;
return c;
}
}
int main()
{
Stack *s;
Stack *s1;
InitStack(s);
long num;
int m;
int k;
char c;
cout<<"输入一个数:"<<endl;
cin>>num;
cout<<"输入要转换的进制:"<<endl;
cin>>k;
while(num!=0)
{
m=num%k;
c=(int('0')+m);
s1=new Stack;
s1->c=c;
Push(s,s1);
num/=k;
}
while(s)
{
cout<<Pop(s);
}
cout<<endl;
}
数据结构c语言怎么运行啊
首先要确定你想采用线性表的顺序存储结构还是链式存储结构。
以顺序存储结构为例:
#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
typedef int Status;
typedef int ElemType;//顺序表测试用
const int MaxSize=; //只是网站提醒源码示例性的数据,可以根据实际问题具体定义
const int Increasement=;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SeqList;
Status InitList(SeqList &S,股票手机指标源码int n=MaxSize)
{ //初始化线性表
if(n<=0)
n=MaxSize;
S.elem =(ElemType *)malloc(n*sizeof(ElemType));
if(S.elem ==NULL)
exit(ERROR);
S.length =0;
S.listsize =n;
return OK;
}
int ListLength(SeqList &S)
{ //求线性表的长度
return S.length ;
}
void GetElem(SeqList &S,int i,ElemType &e)
{ //按位查找,取线性表的腾冲离瑞丽源码第i个元素
if(i<1 || i>S.length )
exit(ERROR);
e=S.elem [i-1];
}
Status ListInsert(SeqList &S,int i, ElemType e)
{ //在线性表中第i个位置插入值为e的元素
if(i<1 || i>S.length+1 )
return ERROR;
if(S.length >=S.listsize )
{
ElemType *newbase=(ElemType *)realloc(S.elem,Increasement*sizeof(ElemType));
if(newbase==NULL)
exit(ERROR);
S.elem =newbase;
S.listsize =S.listsize+Increasement;
}
for(int j=S.length -1;j>=i-1;j--)
S.elem [j+1]=S.elem [j];
S.elem [i-1]=e;
S.length++;
return OK;
}
Status Output(SeqList S)
{ //遍历线性表,按序号依次输出各元素
if(S.length ==0)
return ERROR;
for(int i=0;i<S.length ;i++)
printf("%d\t",万得sar源码S.elem[i]);
printf("\n");
return OK;
}
void BinInsertSort(int r[ ], int n)
{ //折半插入排序
int low,high,mid;
ElemType e;
for (int i=1; i<n; i++)
{
e=r[i];
low=0; high=i;
while (low<=high)
{
mid=(low+high)/2;
if (e<r[mid])
high=mid-1;
else low=mid+1;
}
for (int j=i-1; j>=high+1; j--)
r[j+1]=r[j];
r[high+1]=e;
}
}
void MergeList(SeqList La, SeqList Lb, SeqList &Lc) {
// 已知顺序表La和Lb中的元素按值非递减排列。
// 归并La和Lb得到新的cs源码做软件顺序表Lc,Lc的元素也按值非递减排列。
int La_len, Lb_len;
ElemType ai, bj;
int i=1, j=1, k=0;
InitList(Lc);
La_len = ListLength(La);
Lb_len = ListLength(Lb);
while ((i <= La_len) && (j <= Lb_len)) { // La和Lb均非空
GetElem(La, i, ai);
GetElem(Lb, j, bj);
if (ai <= bj) {
ListInsert(Lc, ++k, ai);
++i;
} else {
ListInsert(Lc, ++k, bj);
++j;
}
}
while (i <= La_len) {
GetElem(La, i++, ai); ListInsert(Lc, ++k, ai);
}
while (j <= Lb_len) {
GetElem(Lb, j++, bj); ListInsert(Lc, ++k, bj);
}
} // MergeList
void main()
{
SeqList La,Lb,Lc;
InitList(La,3);
InitList(Lb,5);
ElemType data;
printf("输入顺序表La的元素值:\n");
for(int i=1;i<=3;i++)
{
scanf("%d",&data);
ListInsert(La, i, data);
}
fflush(stdin);
printf("输入顺序表Lb的元素值:\n");
for(i=1;i<=5;i++)
{
scanf("%d",&data);
ListInsert(Lb, i, data);
}
//La和Lb非递减排序
BinInsertSort(La.elem , 3);
BinInsertSort(Lb.elem , 5);
MergeList(La,Lb,Lc);
Output(Lc);
}