您的位置:首页 > 理论基础 > 数据结构算法

数据结构与算法--链式线性表操作

2020-06-25 04:34 375 查看

#include
#include <stdio.h>
using namespace std ;

typedef int DataType;

struct node
{
DataType saleAmount ;//销售数量
struct node *next;//指向下一产品的指针
} ;

typedef struct node * pNode;

//函数功能: 遍历链表并输出链表中各数据元素的值
void traverseList_link(pNode pllist) //功能是输出线性表pllist的元素,以空格为分界符;
{
pNode temp = pllist->next ;
while(temp != NULL)
{
cout<saleAmount <<" " ;
temp = temp->next;
}
}

//创建一个空的链表
pNode createNullList_link(void )
{
struct node* temp = new struct node ;
temp->next = NULL;
temp->saleAmount = 0;
return temp;
}

void insert_link(pNode pllist ,DataType x )
{
pNode temp = new struct node ;
temp->saleAmount = x ;
temp->next = NULL;
pNode p = pllist->next , pre = pllist;
while(p!=NULL)
{
pre = p ;
p = p->next ;
}
pre->next = temp ;
}

void reverseList_link( pNode pllist)
{
//请在此处填入代码,实现链表逆置功能
/********** Begin **********/
struct node *p,*q;
p=pllist->next;
pllist->next=NULL;
while§
{
q=p;
p=p->next;
q->next=pllist->next;
pllist->next=q;
}

/********** End **********/

}

int main(void)
{
pNode pllist = createNullList_link() ;
int num ;
DataType data;
cin>>num;
for(int i = 0;i < num; i++)
{
cin>>data ;
insert_link(pllist, data);
}
reverseList_link(pllist) ;
traverseList_link(pllist);
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐