双链表排序的操作

代码片段(5)
数据结构的课程设计,比较简单的&~&1006B&&&&
#pragma once
template &class T&
class DLinkNode
DLinkNode& T &*
DLinkNode& T &*
DLinkNode() ;
DLinkNode( T data , DLinkNode& T &* prev = NULL , DLinkNode& T &* next = NULL ) ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of DLinkNode.h
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
DLinkNode& T &::DLinkNode()
this -& prev = NULL ;
this -& prev = NULL ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of DLinkNode.h
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
DLinkNode& T &::DLinkNode( T data , DLinkNode& T &* prev /* = NULL
*/, DLinkNode& T &* next /* = NULL */ )
this -& data =
this -& prev =
this -& next =
数据结构的课程设计,比较简单的&~&7KB&&&&
#pragma once
#include "DLinkNode.h"
#include &iostream&
template & class T &
class DoublyLinkedList
DLinkNode& T &*
DoublyLinkedList() ;
//构造函数,构造空的链表
DoublyLinkedList( T table[] , int count ) ;
~DoublyLinkedList() ;
//析构函数
void outPut() ;
//输出函数
int length() ;
//获取单链表长度函数
bool isEmpty() ;
//判断是否双链表是否为空
DLinkNode& T &* getNode( int i ) ;
//获得第i个结点的指针
T get( int i ) ;
//获得第i个元素
bool set( int i , T x ) ;
//设定第i个元素为x
DLinkNode& T &* insert( int i , T x ) ;
//在第i个位置插入第一个元素x
bool remove( int i , T& old ) ;
//删除第i个结点,讲被删除元放在old中
void clear() ;
//清空链表
friend ostream& operator&& ( ostream& out , DoublyLinkedList& T && list)
list.outPut() ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// 定义构造函数,来构造空的双链表
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
DoublyLinkedList& T &::DoublyLinkedList()
head = NULL ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of DoublyLinkedList.h
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
DoublyLinkedList& T &::DoublyLinkedList( T table[] , int count )
this -& head = NULL ;
if ( cout & 0 )
head = new DLinkNode& T &( table[ 0 ] , NULL , NULL ) ;
DLinkNode& T &* temp =
for( int i = 1 ; i & i ++ )
temp -& next
= new DLinkNode& T &( table[ i ] , temp , NULL ) ;
temp = temp -&
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of DoublyLinkedList.h
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
void DoublyLinkedList& T &::outPut()
DLinkNode& T &* temp =
while( temp != NULL )
cout && temp -& data && " " ;
temp = temp -&
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of DoublyLinkedList.h
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
int DoublyLinkedList& T &::length()
DLinkNode& T &* temp =
int len = 0 ;
while( temp != NULL )
temp = temp -&
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of DoublyLinkedList.h
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
bool DoublyLinkedList& T &::isEmpty()
return head == NULL ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of DoublyLinkedList.h
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
DLinkNode& T &* DoublyLinkedList& T &::getNode( int i )
int len = length() ;
if( i & 0 || i & len )
return NULL ;
int number = 0 ;
DLinkNode& T &* temp =
while( temp != NULL && number & i )
number ++ ;
temp = temp -&
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of DoublyLinkedList.h
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
T DoublyLinkedList& T &::get( int i )
DLinkNode& T &* temp = getNode( i ) ;
if( temp != NULL )
return temp -&
throw"双链表为空或者参数i指定元素序号无效" ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of DoublyLinkedList.h
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
bool DoublyLinkedList& T &::set( int i , T x )
DLinkNode& T &* temp = getNode( i ) ;
if( temp != NULL )
temp -& data =
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of DoublyLinkedList.h
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
DLinkNode& T &* DoublyLinkedList& T &::insert( int i , T x )
//int len = this -& length() ;
//if( i & len )
//if( i & 0 )
// i = 0 ;
DLinkNode& T &* temp = NULL ;
if( head == NULL || i & 0 )
temp = new DLinkNode& T &( x , NULL , head
DLinkNode& T &* tempNode = getNode( i - 1 ) ;
temp = new DLinkNode& T &( x , tempNode , tempNode -& next ) ;
tempNode -& next =
//tempNode -& prev =
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of DoublyLinkedList.h
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
bool DoublyLinkedList& T &::remove( int i , T& old )
DLinkNode& T &* temp = getNode( i ) ;
if( temp != NULL )
temp -& next -& prev = temp -&
temp -& prev -& next = temp -&
old = temp -&
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of DoublyLinkedList.h
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
void DoublyLinkedList& T &::clear()
DLinkNode& T &* temp =
while( temp != NULL )
DLinkNode& T &* tempNode =
temp = temp -&
delete tempN
head = NULL ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of DoublyLinkedList.h
////////////////////////////////////////////////////////////////////////////////////////////////////
template & class T &
DoublyLinkedList& T &::~DoublyLinkedList()
数据结构的课程设计,比较简单的&~&1KB&&&&
#pragma once
#include &iostream&
struct Element
friend ostream& operator&&( ostream& out , const Element& elem ) ;
//重载输出函数
bool operator==( Element& elem ) ;
//重载相等函数
bool operator!=( Element& elem ) ;
//重载不等函数
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of Element.h
////////////////////////////////////////////////////////////////////////////////////////////////////
ostream& operator&&( ostream& out , const Element& elem)
out&&"( "&& elem.row &&" , "&& elem.column &&" , "&&elem.value &&" ) " ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of Element.h
////////////////////////////////////////////////////////////////////////////////////////////////////
bool Element::operator==( Element& elem )
if( row == elem.row && column == elem.column && value == elem.value )
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of Element.h
////////////////////////////////////////////////////////////////////////////////////////////////////
bool Element::operator!=( Element& elem )
if( (* this == elem ) )
数据结构的课程设计,比较简单的&~&651B&&&&
#include "DoublyLinkedList.h"
#include "SeqSparseMatrix.h"
#include &iostream&
int main()
Element itema[] = { { 0 , 2 , 11 } , { 0 , 4 , 17 } , { 4 , 4 , 50 } ,{ 1 , 1 , 20 } ,{ 3 , 5 , 28 } } ;
SeqSparseMatrix smata( 5 , 6 , itema , 5 ) ;
Element itemb[] = { { 0 , 2 ,10 } , { 1 , 4 , 9 } , { 3 ,5 ,20 } , { 4 , 5 ,31 } ,{ 4 , 2 , 1 } } ;
SeqSparseMatrix smatb( 5 , 6 ,itemb , 5 ) ;
cout&&smata&&endl&&smatb&&
cout&&smata&&
cout&&smata + smatb&&
if( smata == smata )
cout&&"caosiyuan "&&
cout&&smatb.transpostion()&&
system( "pause" ) ;
return 0 ;
数据结构的课程设计,比较简单的&~&7KB&&&&
#pragma once
#include "Element.h"
#include "DoublyLinkedList.h"
#include &iomanip&
class SeqSparseMatrix
DoublyLinkedList& Element&
SeqSparseMatrix( int rows = 4 , int columns = 4 ) ;//构造空的稀疏矩阵,指定行数和列数
SeqSparseMatrix( int rows , int columns , Element item[] , int n ) ;
//构造函数
int get( int i , int j ) ;
//获取第i行第j列元素的值
bool set( int i , int j , int value ) ;
//设置第i行第j列元素的值为value
bool set( Element item ) ;
//按照主序插入一个三元组
void outPut() ;
//输出函数
SeqSparseMatrix& transpostion() ;
//稀疏矩阵的转置
friend ostream& operator&&( ostream& out , SeqSparseMatrix& mat ) ;
SeqSparseMatrix& operator+=( SeqSparseMatrix& mat ) ;
friend SeqSparseMatrix& operator+( SeqSparseMatrix& mata , SeqSparseMatrix& matb ) ;
bool operator==( SeqSparseMatrix& mat ) ;
//重载相等函数
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of SeqSparseMatrix.h
////////////////////////////////////////////////////////////////////////////////////////////////////
SeqSparseMatrix::SeqSparseMatrix( int rows /* = 4
*/, int columns /* = 4 */ )
if( rows &= 0 || columns &= 0 )
throw"矩阵的行或列非正数异常" ;
this -& rows
this -& columns =
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of SeqSparseMatrix.h
////////////////////////////////////////////////////////////////////////////////////////////////////
SeqSparseMatrix::SeqSparseMatrix( int rows , int columns , Element item[] , int n )
if( rows &= 0 || columns &= 0 )
throw"矩阵的行或列非正数异常" ;
this -& rows
this -& columns =
for( int i = 0 ; i & i ++ )
set( item[ i ] ) ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of SeqSparseMatrix.h
////////////////////////////////////////////////////////////////////////////////////////////////////
int SeqSparseMatrix::get( int i , int j )
if( i & 0 || i & rows || j & 0 || j & columns )
throw"矩阵元素的行或列序号越界异常" ;
int number = 0 ;
while( number & list.length() )
Element elem = list.get( number ) ;
if( elem.row ==i && elem.column == j )
return elem.
if( elem.row & i || elem.row == i && elem.column & j )
number ++ ;
return 0 ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of SeqSparseMatrix.h
////////////////////////////////////////////////////////////////////////////////////////////////////
bool SeqSparseMatrix::set( int i , int j , int value )
Element item = { i , j , value } ;
return set( item ) ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of SeqSparseMatrix.h
////////////////////////////////////////////////////////////////////////////////////////////////////
bool SeqSparseMatrix::set( Element item )
if ( item.row &= 0 && item.row & this -& rows && item.column &= 0 || item.column & this -& columns )
int number = 0 ;
while( number & list.length() )
Element elem = list.get( number ) ;
if ( elem.row == item.row && elem.column == item.column )
list.set( number , item ) ;
if( elem.row & item.row || elem.row == item.row && elem.column & item.column )
number ++ ;
list.insert( number , item ) ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of SeqSparseMatrix.h
////////////////////////////////////////////////////////////////////////////////////////////////////
void SeqSparseMatrix::outPut()
cout&&"三元组顺序表:"&&
cout&&"稀疏矩阵( "&&rows&&"×"&&columns&&" ) : \n" ;
int number = 0 ;
int len = list.length() ;
if( number & len )
elem = list.get( number ++ ) ;
for( int i = 0 ; i & i ++ )
for( int j = 0 ; j & j ++ )
if( i == elem.row && j == elem.column )
cout&&setw( 5 )&&elem.
if( number & len )
elem = list.get( number ++ ) ;
else cout&& setw( 5 )&& 0 ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of SeqSparseMatrix.h
////////////////////////////////////////////////////////////////////////////////////////////////////
ostream& operator&&(ostream& out , SeqSparseMatrix& mat )
mat.outPut() ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of SeqSparseMatrix.h
////////////////////////////////////////////////////////////////////////////////////////////////////
SeqSparseMatrix& SeqSparseMatrix::operator+=( SeqSparseMatrix& mat )
if( this -& rows != mat.rows || this -& columns != mat.columns )
throw"两个矩阵阶数不同,不能相加" ;
int i = 0 , j = 0 ;
while( i & list.length() && j & mat.list.length() )
Element item = this -& list.get( i ) ;
Element elem = mat.list.get( j ) ;
if( elem.row == item.row && elem.column == item.column )
item.value += elem.
list.set( i ,item ) ;
else if( item.row & elem.row || item.row == elem.row && item.column & elem.column )
this -& list.insert( i , elem ) ;
while( j & mat.list.length() )
this -& list.insert( i ++ , mat.list.get( j ++ ) ) ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of SeqSparseMatrix.h
////////////////////////////////////////////////////////////////////////////////////////////////////
SeqSparseMatrix& operator+( SeqSparseMatrix& mata , SeqSparseMatrix& matb )
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of SeqSparseMatrix.h
////////////////////////////////////////////////////////////////////////////////////////////////////
bool SeqSparseMatrix::operator==( SeqSparseMatrix& mat )
if( rows != mat.rows || columns != mat.columns || list.length() != mat.list.length() )
= list.length() ;
int mark = 1 ;
while( mark == 1 && i & len )
Element item = this -& list.get( i ) ;
Element elem = mat.list.get( i ) ;
if( item != elem )
mark = 0 ;
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of SeqSparseMatrix.h
////////////////////////////////////////////////////////////////////////////////////////////////////
SeqSparseMatrix& SeqSparseMatrix::transpostion()
int len = list.length() ;
SeqSparseMatrix mat( columns ,rows ) ;
for( int i = 0 ; i & i ++ )
Element elem = list.get( i ) ;
Element item = { elem.column , elem.row , elem.value } ;
mat.set( item ) ;
开源中国-程序员在线工具:
小伙子写的不错,拿来改改用
开源从代码分享开始
曹思源的其他代码建立双向链表 实现对双向链表的插入 删除操作·_百度知道
建立双向链表 实现对双向链表的插入 删除操作·
要有必要的注释··
提问者采纳
#include&iostream&using std::using std::template&typename T& class Node{
//节点的定义public: Node(T t):item(t),previous(0),next(0){} T Node* Node*};template&typename T& class TwoWayLinkedList{
//双向链表的定义public: TwoWayLinkedList():length(0),head(0),tail(0){}; ~TwoWayLinkedList(); int getLength(){} void add(T t); T get(int i); void insert(int i,T t); void del(int i); void display();private: Node&T&* Node&T&*};template &typename T& void TwoWayLinkedList&T&::add(T t){
//在链表的结尾添加新元素 if(head==0){
//如果链表为空
head=new Node&T&(t);
tail= } else {
Node&T&* q=new Node&T&(t);
//新建一个节点储存新元素
tail-&next=q;
//加到当前链表的结尾
q-&previous=
//它的前者为当前的结尾
tail=tail-&
//把结尾移到这个新元素 } length++;}template&typename T& T TwoWayLinkedList&T&::get(int i){
//返还下标为i的元素 if(i&=length||i&0) return 0;
//如果没有这个下标返还0 int j=0; Node&T&* p= while(j&i){
//找到下标所指的节点
j++; } T item=(p-&T);}template&typename T& void TwoWayLinkedList&T&::insert(int i, T t){
//在下标为i处插入一个新元素 if(i&=0){
//如果下标小于等于0,插在最前面
Node&T&* p=new Node&T&(t);
head-&previous=p;
length++; } else if(i&=length) add(t);
//如果下标大于最大下标加在最后面 else{
Node&T&* n=new Node&T&(t);
Node&T&* t=
while(j&i-1){
//找到所插位置前面的元素
n-&next=t-&
n-&previous=t;
(t-&next)-&previous=n;
t-&next=n;
length++; }}template&typename T& void TwoWayLinkedList&T&::del(int i){
删除下标为i的元素 if(i&0||i&=length) // 如果没有这个下标直接退出 else{
if(length==0)
//如果链表为空推出
else if(length==1){
//如果只有一个元素
else if(i==0){ // 如果要删除第一个元素
Node&T&* t=
head=head-&
//把第一个移至下一个
head-&previous=0;
else if(i==length-1){
//如果要删除最后一个元素
Node&T&* t=
tail=tail-&
//把最后一个往前移动一位
tail-&next=0;
Node&T&* t=
while(i&1){
//找到要删除元素的前一个
Node&T&* p=t-&
t-&next=p-&
(p-&next)-&previous=t;
length--; }}template&typename T& TwoWayLinkedList&T&::~TwoWayLinkedList(){
//拆构函数 Node&T&* t= Node&T&* while(t!=0){
//清理每一个节点的空间
t=t-& }}template&typename T& void TwoWayLinkedList&T&::display(){
//按顺序显示全部元素 T int i=0; while(i&length){
item=this-&get(i);
cout&&item&&& &; } cout&&}主函数你自己添把。
提问者评价
谢谢啦·呵呵·
其他类似问题
按默认排序
其他1条回答
#include &iostream&struct Node{
//节点中的数据
结构体Node的成员变量
//指向下一节点的指针,习惯用next命名 结构体Node的成员变量
Node( const int& d=int() )
//结构体也可以有构造函数 ,d=T()来指定默认值
:data(d),next(NULL)
//用构造函数来初始化成员变量data和指针
//所有数据类型,默认初始化都为0,这里data默认初始化为0};class Chain
//封装链表{private:
//数据成员通常都是private的
//首先,我们要一个Node型的指针来保存链表的第一个节点;
//再用一个整型变量来记录当前链表内的节点数public:
//在构造函数里建立一个空链表,即head指向NULL
:head(NULL),length(0){}
//节点数为0;
//当我们在类中定义函数时(不是声明),相当于在前面加上一个inline修饰
void delall()
//这个函数用来删除链表中的所有节点
//定义一个Node型指针用来保存要删除的节点
while( head != NULL )
//当head的指向不为NULL时,就是链表中还存在节点
//这里备份head的当前指向节点
head = head-& //把head的指向改变为下一节点
//把head的原指向给删除掉
//如此一直下去,尾节点的next肯定是指向NULL的,那删除最后一个的时候
//head就被赋值为NULL,不满足循环条件,退出循环
length = 0;
//把节点数归零
~Chain(){ delall(); }
//在析构函数中调用delall函数,来完成对象销毁时清理工作
//这样一个链表必须具备的功能就实现了。下面我们来实现他的增、删、查、改功能
Node*& getpoint( int position )
//对链表的操作,其实全部通过指针来实现的,
//那就需要定义一个函数来返回当前节点的指针(引用)
if( position&0 || position&length )
//对节点编号进行合法检查
position =
//如果是非法节点编号,那么就把他修改为最后一个节点编号
if( position==0 )
//如果编号为0,那就是第一个节点了,
//直接返回head就是指向第一个节点的,注意返回的是head本身
Node* head_bak =
//如果编号合法并且不是第一个节点,就开始遍历链表
for( int i=1; i & i++ )
//为什么不直接用head
//注意这里修改的是成员变量。你把head改了,以后到哪找链表
//我们都是通过head一个一个的往下找节点的。head被修改了。后果显而易见
head_bak = head_bak-&
//通过备份的指针来遍历到指定编号前一个节点
//i不从0开始,减少运算,提高效率
return head_bak-&
//这里如果返回head_bak的话。那就是需要的前一个节点了
void insert( const int& data, int position ) //如果不修改参数的话,使用引用做参数的时候,最好加上const
Node* pin = new Node(data);
//需要调用Node的构造函数
pin-&next = getpoint(position);
//把指定位置的指针返回给新节点的指针
//也就是说,把新的节点的next指向原来这个位置的节点。
getpoint(position) =
//getpoint()返回的是引用,我们可以直接修改它
//前面的一个节点的next指向我们新的节点。
//链表的节点数+1
int del( const int& data )
int position = find(data);
if( position !=-1 )
//-1代表没找到
Node* &pnext = getpoint(position);
//用getponit()来获得指定节点的指向信息
Node* pbak =
//用来备份节点的指向信息
pnext = pnext-&
//把next指向改为下下个节点。
//把&&重载,直接输出链表
friend ostream& operator&&( ostream& os, const Chain& oc )
Node* phead = oc.
os && &[ &;
while( phead !=NULL )
//判断是否到尾节点
os && phead-&data && ' ';
phead = phead-&
os && &] &;
//这个函数,应该没什么好说的了
//如果还是不理解,当成固定模式去背吧
}};void show(){
cout && &******************************& &&
cout && &2- 向链表内添加节点(数据,节点号)& &&
cout && &3- 删除链表内某一个数据(数据)& &&
cout && &0- 退出& &&
cout && &******************************& &&}int main(){
int position, data, choice, data_
while( choice != 0 )
cout && &请选择:&;
switch ( choice )
cout && &请输入要插入的数据和插入位置:& ;
cin && data &&
link.insert( data,position );
cout && link &&
cout && &请输入要删除的数据:&;
link.del( data );
cout && link &&
双向链表的相关知识
您可能关注的推广
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 c语言链表 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信