给我几个数据结构的程序

线性表(顺序,单链)
栈(只要顺序)
队列(顺序,链)
我正在学数据结构! 只会写算法! 不会写程序!
请大家多给我几个程序! 让我多看看!好的话我会多加分的!
还有用栈做十进制转换成8进制的程序! 要程序哦!! 谢谢了!

您可在vc++6.0上运行,我最近也在学习数据结构,这些程序都经过我的调试了.想必您的课本也是《数据结构(C语言版)》严蔚敏编著的吧。
可能存在不足之处,希望大家能够指出,大家相互学习!!!

//线性表的动态分配顺序存储结构
# include "stdlib.h"
# include "stdio.h"
//函数结果状态代码
# define TURE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define OVERFLOW -2
//类型定义
typedef int Status;
typedef int ElemType;

# define List_init_size 100
//线性表存储空间初始分配量
# define Listincrement 10
//线性表存储空间分配增量

typedef struct{
ElemType *elem; //存储空间基址
int length; //当前长度
int listsize;//当前分配的存储容量
// (以sizeof(ElemType)为单位)
}sqlist;

Status InitList(sqlist &L,int elem_number)
{//构造一个空线性表L
if (elem_number> List_init_size ) return ERROR;
L.elem=(ElemType *) malloc(elem_number*sizeof(ElemType));
if (!L.elem) exit(OVERFLOW);
L.length=0;
L.listsize= elem_number;
return OK;
}

//在线性表中插入一个元素
Status Listinsert_sq(sqlist &L,int i,ElemType e) {
ElemType *p,*q,*newbase;
if (i<1 || i>L.length+1) return ERROR;
if (L.length>=L.listsize) {
newbase=(ElemType *) realloc(L.elem, (L.listsize+Listincrement) *sizeof(ElemType));
if (!newbase) exit(OVERFLOW);
L.elem=newbase; L.listsize+=Listincrement ;}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p) *(p+1)=*p;
*q=e; ++L.length;
return OK;
}// listinsert_sq;

//在线性珍中删除第i个元素,其结果保留在e中
Status Listdelete_sq(sqlist &l,int i,ElemType &e)
{
ElemType *p,*q;
if (i<1||i>l.length+1) return ERROR;
p=&(l.elem[i-1]);
e=*p;
q=l.elem+l.length-1;
for(++p;p<=q;++p) *(p-1)=*p;
--l.length;
return OK;
}// listdelete_sq;

void OutPutList(sqlist l)
{
int j;
for(j=0;j<l.length;j++)
printf("%d ",l.elem[j]);
printf("\n");
}

void main()
{
int n,e,i,s,m;
sqlist l;
printf("请输入线性表的元素个数:");
scanf("%d",&n);
InitList(l,n);

printf("请输入线性表的元素:");
for(i=0;i<n;i++)
scanf("%d",&l.elem[i]);
l.length=n;
OutPutList(l);

printf("请输入插入的位置和插入的元素:");
scanf("%d%d",&s,&e);
Listinsert_sq(l,s,e);
OutPutList(l);

printf("请输入删除元素的位置");
scanf("%d",&m);
Listdelete_sq(l,i,e);
OutPutList(l);

printf("程序结束!\n");
}

//栈的顺序存储表示
#include <stdio.h>

#include <ctype.h>
#include <conio.h>
#include <dos.h>
#include "malloc.h"
#include "process.h"

#define list_init_size 100
#define listincrement 10
#define ERROR -1
#define OK 1
#define OVERFLOW -2
#define NULL 0

typedef int elemtype;
typedef int selemtype;
typedef int status ;

typedef stru*basect{
selemtype ;
selemtype *top;
int stacksize;
}sqstack;

status initstack(sqstack &s)
{
s.base=(selemtype *)malloc(list_init_size*sizeof(elemtype));
if(!s.base) exit(OVERFLOW);
s.top=s.base;
s.stacksize=list_init_size;
return OK;
}

status gettop(sqstack s,selemtype &e)
{
if(s.top==s.base) return ERROR;
e=*(s.top-1);
return OK;
}

status push(sqstack &s,selemtype e)
{
if(s.top-s.base>=s.stacksize)
{
s.base=(elemtype *)realloc(s.base,(s.stacksize+listincrement)*sizeof(elemtype));
if(!s.base) exit(OVERFLOW);
s.top=s.base+s.stacksize;
}
*s.top++=e;
return OK;
}

status pop(sqstack &s,selemtype &e)
{
if(s.top==s.base) return ERROR;
e=*--s.top;
return OK;
}

int stackempty(sqstack s)
{
if(s.base==s.top) return 1;
else return 0;
}

void main()
{
sqstack s;
int n,e,i;
initstack(s);

printf("\n请输入元素个数:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&e);
push(s,e);
}
printf("\n");

while(!stackempty(s))
{
pop(s,e);
printf("%d ",e);
}

}

//十进制转化为八进制
#include<stdio.h>
#include<stdlib.h>

#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef int ElemType ;
typedef int SElemType;
typedef int Status ;

typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;

Status StackEmpty(SqStack S)
{
if(S.top==S.base)
return 1;
else return 0;
}

Status InitStack(SqStack &S)
{
S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S.base) exit (OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}

Status GetTop(SqStack S,SElemType &e)
{
if(S.top==S.base)
return ERROR;
e=*(S.top-1);
return OK;
}

Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(ElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!S.base) exit (OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}

Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base)
return ERROR;
e=*--S.top;
return OK;
}

void conversion()
{
int N,e;
SqStack S;
InitStack(S);
printf("请输入十进制数:");
scanf("%d",&N);
while(N)
{
Push(S,N%8);
N/=8;
}
while(!StackEmpty(S))
{
Pop(S,e);
printf("%d",e);
}
}

void main()
{
int e;
SqStack S;
InitStack(S);

conversion();
printf("\n");

while(S.top!=S.base)
{
Pop(S,e);
printf("%d ",e);
}
printf("\n");
}

//--------------ADT Queue 的表示和实现--------------
//--------------队列的顺序存储结构------------------

# include "stdlib.h"
# include "stdio.h"

//函数结果状态代码
# define TURE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define OVERFLOW -2
# define maxqsize 100

typedef int status;
typedef int qelemtype;
typedef struct{
qelemtype *base;
int front;
int rear;
}sqqueue;

//----------循环队列的基本操作的算法描述--------
status initqueue(sqqueue &Q){
//构造一个空队列Q
Q.base=(qelemtype*)malloc(maxqsize*sizeof(qelemtype));
if(!Q.base) return ERROR;
Q.front=Q.rear=0;
return OK;
}

int queuelength(sqqueue Q){
//返回Q的元素个数,即对列的长度
return (Q.rear-Q.front+maxqsize)%maxqsize;
}

status enqueue(sqqueue &Q,qelemtype e){
//插入元素e为Q的新的队尾元素
if((Q.rear+1)%maxqsize==Q.front) return ERROR; //队列满
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%maxqsize;
return OK;
}

status dequeue(sqqueue &Q,qelemtype &e){
//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK
//否则返回ERROR
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%maxqsize;
return OK;
}

void main()
{
//测试基本操作
int i,e;
sqqueue Q;
initqueue(Q);
printf("\n");
for(i=1;i<=10;i++)
{
scanf("%d",&e);
enqueue(Q,e);
}

printf("the length of queue is :%d\n",queuelength(Q));

while(Q.front!=Q.rear)
{
dequeue(Q,e);
printf(" %d",e);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2007-04-07
线性表的应用:
#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;
cout<<"操作成功"<<endl;
}

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;
}

}

//2~9进制的转换

#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;
}
相似回答