线性表的顺序存储结构用C++实现代码:
#include "pch.h"
#include <stdio.h>
#include <malloc.h>
//#define DATA int
typedef int DATA
struct SNode
{
DATA data
SNode* pNext
}
SNode* g_pHead = NULL
void AddHead(DATA data)
{
SNode* p = (SNode*)malloc(sizeof(SNode))
p->data = data
p->pNext = g_pHead
g_pHead = p
}
void Print()
{
SNode* p = g_pHead
printf("List:")
while (p) //当节点的地址不为NULL
{
printf("%d ", p->data)
p = p->pNext
}
printf("\n")
}
int main()
{
AddHead(1)
AddHead(2)
AddHead(3)
Print()
return 0
}
程序运行结果如下:
扩展资料:
双向链表框架:
#pragma once
typedef void* POSITION
typedef int DATA
struct SNode
{
DATA data
SNode *pPrev, *pNext
}
class CList
{
SNode *m_pHead, *m_pTail
int m_nCount
public:
CList()
~CList()
void RemoveAll()
DATA GetNext(POSITION &pos)
DATA GetPrev(POSITION &pos)
void AddHead(DATA data)
void AddTail(DATA data)
void RemoveAt(POSITION pos)
}
(1)编写一个程序,实现顺序环形队列的各种基本运算,并在此基础上设计一个主程序完成如下功能:(1)初始化队列q;
(2)判断队列q是否非空;
(3)依次进队元素100、909、44、8;
(4)出队一个元素,输出该元素;
(5)输出队列q的元素个数;
(6)依次进队元素-67、55、99、70;
(7)输出队列q的元素个数;
#include<stdio.h>
#include<malloc.h>
#define QUEUE_INIT_SIZE 100
#define QUEUEINCREMENT 10
#define OK 1
#define TURE 1
#define FALSE 0
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status
typedef int QElemType
typedef struct
{
QElemType *base
int front
int rear
}SqQueue
Status InitQueue(SqQueue &Q)
{
Q.base=(QElemType *)malloc
(QUEUE_INIT_SIZE*sizeof(QElemType))
if(!Q.base)
exit(OVERFLOW)
Q.front=Q.rear=0
return OK
}
int QueueNumElem(SqQueue Q)
{
return (Q.rear-Q.front)%QUEUE_INIT_SIZE
}
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%QUEUE_INIT_SIZE==Q.front)
return ERROR
Q.base[Q.rear]=e
Q.rear=(Q.rear+1)%QUEUE_INIT_SIZE
return OK
}
SqQueue DeQueue(SqQueue Q,int e)
{
int i
if(Q.front==Q.rear)
printf("队为空!\n")
for(i=ei<Q.reari++)
Q.base[i]=Q.base[i+1]
Q.rear--
return Q
}
int main()
{
SqQueue Q,Q1
static int qele=0
int i,j=0,k=0,m=0
static int frontd=Q.front
i=InitQueue(Q)
if(i==1)
printf("队初始化成功!!\n")
else
printf("队初始化失败!!\n")
if(Q.front!=Q.rear)
printf("队不为空!!\n")
else
printf("队为空L!!\n")
printf("输入数据(END of '9999'):")
scanf("%d",&qele)
while(qele!=9999||Q.rear==Q.front)
{
EnQueue(Q,qele)
scanf("%d",&qele)
}
frontd=Q.front
while(Q.rear!=Q.front)
{
printf(" %d ",Q.base[Q.front])
Q.front++
j++
}
printf("\n")
Q.front=frontd
printf("输入要出队的元素:")
scanf("%d",&j)
while(Q.front!=Q.rear)
{
if(Q.base[Q.front]==j)
{
printf("%d\n",Q.base[Q.front])
Q=DeQueue(Q,Q.front)
m++
break
}
Q.front++
}
Q.front=frontd
while(Q.front!=Q.rear)
{
printf(" %d ",Q.base[Q.front])
Q.front++
}
printf("\n")
Q.front=frontd
printf("队的长度:%d\n",Q.rear-Q.front)
printf("输入数据(END of '9999'):")
scanf("%d",&qele)
while(qele!=9999||Q.rear==Q.front)
{
EnQueue(Q,qele)
scanf("%d",&qele)
}
Q.front=frontd
printf("队的长度:%d\n",Q.rear-Q.front)
Q.front=frontd
printf("出队顺序:")
while(Q.rear!=Q.front)
{
printf(" %d ",Q.base[Q.front])
Q=DeQueue(Q,Q.front)
m++
}
printf("end\n")
Q.front=0
Q.rear=m
while(Q.rear!=Q.front)
{
free(Q.base)
//Q.base++
Q.front++
if(Q.rear-1==Q.front)
printf("队已经释放!\n")
}
return 0
}
#define ListSize 100//表空间长度typedef char DataType
typedef struct
{
DataType data
int length
}SeqList
void Insert(SeqList* L, DataType x, int i)
{
assert(i>0 &&i<L->lenght+1)
assert(L->Length <ListSize)
for(j=L->length-1j>=i-1j--)
{
L->data[j+1]=L->data[j]
}
L->data[i-1]=x
L->lenght++
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)