数据结构的全部抽象定义每种必须包含顺序和链式,要C语言而且必须能运行。答得好加高分!

如:线性表的抽象定义(包含顺序和链式两种)
栈和队列的抽象定义(包含顺序和链式两种).....
要一一分类回答,不要堆在一起,这样很难看的。必须能运行!
最好联系我Q,Q号如名。
或者提供给我数据结构(C语言版)严蔚敏 吴伟民 编著的全部源代码,
注:是C语言源代码不是算法!
不过只要给我数据结构-各种结构C语言代码的抽象定义就可以了!
(存储结构包含顺序和链式两种)

线性表包含: 链表(链式结构,内存单元不连续,指针形式,所以便于插入、删除元素)
队列(内存单元连续,数组形式,可采用动态开辟,节省内存,因为有下标,所以便于查找)
栈(内存单元连续,元素个数因处理而变化,属于“先进先出”)
下面我各给出一个实例:

链表:#include<iostream>
using namespace std;
typedef int ElemType;
struct LNode
{
ElemType data;
LNode *next;
};

void Create(LNode* &HL)
{
LNode *p,*q;
HL=p=new LNode;
for(int i=0;i<7;i++) // 动态生成长度为7的单链表;
{
q=new LNode;
cin>>q->data;
p->next=q;
p=q;
}
p->next=NULL;

p=HL->next;
cout<<"生成的链表为:";
while(p!=NULL)
{
cout<<p->data;
p=p->next;
if(p!=NULL) cout<<" --> ";
}
cout<<endl;
}

void ClearList(LNode* & HL) // 清空链表;
{
LNode *cp;
LNode *np;
cp=HL;
while(cp!=NULL)
{
np=cp->next;
delete cp;
cp=np;
}
HL=NULL;
}

int LenthList(LNode* HL) // 返回链表的长度;
{
int i=0;
HL=HL->next;
while (HL!=NULL)
{
i++;
HL=HL->next;
}
return i;
}

void EmptyList(LNode* HL) // 链表是否为空;
{
bool flag;
flag=(HL==NULL);
if(flag) cout<<"该链表为空!"<<endl;
else cout<<"该链表不为空!"<<endl;
}

void TraverstList(LNode* &HL) // 遍历链表,使每个元素加1并输出;
{
LNode *p;
p=HL->next;
cout<<"生成新链表为:";
while(p!=NULL)
{
p->data+=1;
cout<<p->data;
p=p->next;
if(p!=NULL) cout<<" --> ";
}
cout<<endl;
}
int main()
{
LNode *hl;
Create(hl);
cout<<"输入7个元素:"<<endl;
cout<<"链表长度为:"<<LenthList(hl)<<endl;
TraverstList(hl);
EmptyList(hl);

ClearList(hl);
return 0;
}

队列:
#include<iostream>
using namespace std;
typedef int ElemType;
struct Queue
{
ElemType *queue;
int front,rear,len;
int MaxSize;
};

void InitQueue(Queue &Q) // 初始化队列
{
Q.MaxSize=10;
Q.queue=new ElemType[Q.MaxSize];
Q.front=Q.rear=Q.len=0;
}

void EnQueue(Queue &Q,ElemType item) // 元素进队
{
Q.rear=(Q.rear+1)%Q.MaxSize;
Q.queue[Q.rear]=item;
}

void print(Queue Q) // 元素依次出队
{
cout<<"元素依次出队:";
while(Q.front!=Q.rear)
{
Q.front=(Q.front+1)%Q.MaxSize;
cout<<Q.queue[Q.front]<<' ';
}
cout<<endl;
}

ElemType PeekQueue(Queue &Q) // 队首元素出队
{
if(Q.front==Q.rear)
{
cerr<<"队列为空!"<<endl;
exit(1);
}
Q.front=(Q.front+1)%Q.MaxSize;
return Q.queue[Q.front];
}

void ClearQueue(Queue &Q)
{
if(Q.queue!=NULL) delete []Q.queue;
Q.front=Q.rear=0;
Q.queue=NULL;
Q.MaxSize=0;
}

int main()
{
Queue Q;
InitQueue(Q);
for(int i=1;i<8;i++)
EnQueue(Q,i);
print(Q);
cout<<"队首元素出队:"<<PeekQueue(Q)<<endl;
print(Q);
ClearQueue(Q);
return 0;
}

栈:
#include<iostream>
using namespace std;
typedef int ElemType;
struct Stack
{
ElemType *stack;
int top;
int Maxsize;
};

void InitStack(Stack &S) // 动态分配,初始化栈
{
S.Maxsize=10;
S.stack=new ElemType[S.Maxsize];
S.top=-1;
}

void Push(Stack &S,ElemType item) // 进栈
{
S.top++;
S.stack[S.top]=item;
}

ElemType Pop(Stack &S) // 出栈
{
if(S.top==-1)
{
cerr<<"栈为空!"<<endl;
exit(1);
}
S.top--;
return S.stack[S.top+1];
}

void print(Stack S) // 元素依次出栈
{
if(S.top==-1)
{
cerr<<"栈为空!"<<endl;
exit(1);
}
cout<<"所有元素依次出栈:";
while(S.top>=0)
{
cout<<S.stack[S.top]<<' ';
S.top--;
}
cout<<endl;
}

void ClearStack(Stack &S) // 清空栈
{
if(S.stack)
{
delete []S.stack;
S.stack=0;
}
S.top=-1;
S.Maxsize=0;
}

int main()
{
Stack S;
InitStack(S);
for(int i=1;i<8;i++)
Push(S,i); // 7个元素进栈
print(S);
cout<<"删除栈顶元素"<<Pop(S)<<endl;
print(S);
ClearStack(S);
return 0;
}

等你学到后面之后会有更多的应用,比如用队列的深度广度搜索,单数组双栈,堆,数,图。。。等等 建议你学习C++,比C更强大的对象语言。

欢迎进入程序设计群:54349566 加油。。。。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-17
我先给你个 顺序链表 的 你看行不 因为很简单 没有加注释 自己写的
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
// Status是函数的类型,其值是函数结果状态代码
typedef int Status;
typedef int ElemType;
#define LIST_INIT_SIZE 5
#define LISTINCREMENT 2
typedef struct {
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L){
//构造一个空的线性表L。
L.elem = (ElemType * )malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)exit(OVERFLOW);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}//InitList_Sq
Status ListInsert_Sq(SqList &L,int i,ElemType e){
ElemType *newbase,*p,*q;
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;
}
Status ListDelete_Sq(SqList &L,int i,ElemType &e){
ElemType *q,*p;
if((i<1)||(i>L.length))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;
}
void main()
{SqList L;int i;ElemType e;
InitList_Sq(L);
for (i=0;i<LIST_INIT_SIZE;i++)
{
scanf("%d",&L.elem[i]);
++L.length;
}
for(i=0;i<L.length;i++)
printf("%d",L.elem[i]);
printf("\n");
scanf("%d,%d",&i,&e);
ListInsert_Sq(L,i,e);
for(i=0;i<L.length;i++)
printf("%d",L.elem[i]);
printf("\n");
scanf("%d",&i);
ListDelete_Sq(L,i,e);
for(i=0;i<L.length;i++)
printf("%d",L.elem[i]);
}
第2个回答  2011-04-14
好多哦,我可以发给你《数据结构(C语言版)》严蔚敏 吴伟民 编著电子版
第3个回答  2011-04-14
线性表的链式定义:
#include <stdio.h>
#include <malloc.h>
typedef char ElemType;
typedef struct LNode //定义单链表结点类型
{
ElemType data;
struct LNode *next; //指向后继结点
} LinkList;
void CreateListF(LinkList *&L,ElemType a[],int n)
//头插法建立单链表
{
LinkList *s;int i;
L=(LinkList *)malloc(sizeof(LinkList)); //创建头结点
L->next=NULL;
for (i=0;i<n;i++)
{
s=(LinkList *)malloc(sizeof(LinkList));//创建新结点
s->data=a[i];
s->next=L->next; //将*s插在原开始结点之前,头结点之后
L->next=s;
}
}
void CreateListR(LinkList *&L,ElemType a[],int n)
//尾插法建立单链表
{
LinkList *s,*r;int i;
L=(LinkList *)malloc(sizeof(LinkList)); //创建头结点
L->next=NULL;
r=L; //r始终指向终端结点,开始时指向头结点
for (i=0;i<n;i++)
{
s=(LinkList *)malloc(sizeof(LinkList));//创建新结点
s->data=a[i];
r->next=s; //将*s插入*r之后
r=s;
}
r->next=NULL; //终端结点next域置为NULL
}

线性表的顺序定义
#include <stdio.h>
#include <malloc.h>
#define MaxSize 50
typedef char ElemType;
typedef struct
{ ElemType data[MaxSize]; //存放顺序表元素
int length; //存放顺序表的长度
} SqList; //顺序表的类型定义
void CreateList(SqList *&L,ElemType a[],int n)
//建立顺序表
{
int i;
L=(SqList *)malloc(sizeof(SqList));
for (i=0;i<n;i++)
L->data[i]=a[i];
L->length=n;
}
void InitList(SqList *&L)
{
L=(SqList *)malloc(sizeof(SqList)); //分配存放线性表的空间
L->length=0;
}
相似回答