本文目錄一覽:
C語言指針作函數參數中數據的雙向傳遞是什麼意思?
通過參數告訴函數:參數指向的單元存放着你要的數據(供函數讀寫),處理完後也可以將結果放到那些單元(函數結束後供調用程序讀寫),這樣雙方都可以讀寫數據和結果,稱為雙向傳遞。
C語言、雙向鏈表
//Node.h 聲明類Node
#ifndef Node_H
#define Node_H
template class T
class LinkList; //為是Node類的友員類而聲明
template class T
class Node
{
public:
friend class LinkListT; //將LinkList類設為友元類
private:
T data;
NodeT *next;
};
#endif
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。//LinkList.h 聲明類LinkList
#ifndef LinkQueue_H
#define LinkQueue_H
#include “Node.h”
template class T
class LinkList
{
public:
LinkList( ); //建立只有頭結點的空鏈表
LinkList(T a[], int n); //建立有n個元素的單鏈表
~LinkList( ); //析構函數
int Length( ); //求單鏈表的長度
T Get(int i); //取單鏈表中第i個結點的元素值
int Locate(T x); //求單鏈表中值為x的元素序號
void Insert(int i, T x); //在單鏈表中第i個位置插入元素值為x的結點
T Delete(int i); //在單鏈表中刪除第i個結點
void PrintList( ); //遍歷單鏈表,按序號依次輸出各元素
private:
NodeT *first; //單鏈表的頭指針
};
#endif
。。。。。。。。。。。。。。。。。。。
//LinkList.cpp
#include “LinkList.h”
/*
*前置條件:單鏈表不存在
*輸 入:無
*功 能:構建一個單鏈表
*輸 出:無
*後置條件:構建一個單鏈表
*/
template class T
LinkListT:: LinkList( )
{
first=new NodeT; first-next=NULL;
}
/*
*前置條件:單鏈表不存在
*輸 入:順序表信息的數組形式a[],單鏈表長度n
*功 能:將數組a[]中元素建為長度為n的單鏈表
*輸 出:無
*後置條件:構建一個單鏈表
*/
template class T
LinkListT:: LinkList(T a[ ], int n)
{
first=new NodeT; //生成頭結點
NodeT *r,*s;
r=first; //尾指針初始化
for (int i=0; in; i++)
{
s=new NodeT; s-data=a[i]; //為每個數組元素建立一個結點
r-next=s; r=s; //插入到終端結點之後
}
r-next=NULL; //單鏈表建立完畢,將終端結點的指針域置空
}
/*
*前置條件:無
*輸 入:無
*功 能:無
*輸 出:無
*後置條件:無
*/
template class T
LinkListT:: ~LinkList()
{
}
/*
*前置條件:單鏈表存在
*輸 入:查詢元素位置i
*功 能:按位查找位置為i的元素並輸出值
*輸 出:查詢元素的值
*後置條件:單鏈表不變
*/
template class T
T LinkListT::Get(int i)
{
NodeT *p; int j;
p=first-next; j=1; //或p=first; j=0;
while (p ji)
{
p=p-next; //工作指針p後移
j++;
}
if (!p) throw “位置”;
else return p-data;
}
/*
*前置條件:單鏈表存在
*輸 入:查詢元素值x
*功 能:按值查找值的元素並輸出位置
*輸 出:查詢元素的位置
*後置條件:單鏈表不變
*/
template class T
int LinkListT::Locate(T x)
{
NodeT *p; int j;
p=first-next; j=1;
if(pp-next){
while(p-data!=x)
{
p=p-next;
j++;
}
return j;
}
else throw “位置”;
}
/*
*前置條件:單鏈表存在
*輸 入:插入元素x,插入位置i
*功 能:將元素x插入到單鏈表中位置i處
*輸 出:無
*後置條件:單鏈表插入新元素
*/
template class T
void LinkListT::Insert(int i, T x)
{
NodeT *p; int j;
p=first ; j=0; //工作指針p初始化
while (p ji-1)
{
p=p-next; //工作指針p後移
j++;
}
if (!p) throw “位置”;
else {
NodeT *s;
s=new NodeT;
s-data=x; //向內存申請一個結點s,其數據域為x
s-next=p-next; //將結點s插入到結點p之後
p-next=s;
}
}
/*
*前置條件:單鏈表存在
*輸 入:無
*功 能:輸出單鏈表長度
*輸 出:單鏈表長度
*後置條件:單鏈表不變
*/
template class T
int LinkListT::Length( )
{
Node T *p = first-next;
int i = 0;
while(p)
{
p = p-next;
i++;
}
return i;
}
/*
*前置條件:單鏈表存在
*輸 入:要刪除元素位置i
*功 能:刪除單鏈表中位置為i的元素
*輸 出:無
*後置條件:單鏈表刪除元素
*/
template class T
T LinkListT::Delete(int i)
{
NodeT *p; int j;
p=first ; j=0; //工作指針p初始化
while (p ji-1) //查找第i-1個結點
{
p=p-next;
j++;
}
if (!p || !p-next) throw “位置”; //結點p不存在或結點p的後繼結點不存在
else {
NodeT *q; int x;
q=p-next; x=q-data; //暫存被刪結點
p-next=q-next; //摘鏈
delete q;
return x;
}
}
/*
*前置條件:單鏈表存在
*輸 入:無
*功 能:單鏈表遍歷
*輸 出:輸出所有元素
*後置條件:單鏈表不變
*/
template class T
void LinkListT::PrintList( )
{
NodeT *p;
p=first-next;
while (p)
{
coutp-dataendl;
p=p-next;
}
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。#includeiostream.h
#include”LinkList.cpp”
#include”Node.h”
void main( )
{
LinkList int a;
cout”執行插入操作:”endl;
try
{
a.Insert(1,4);
a.Insert(2,5);
a.Insert(3,6);
}
catch(char* wrong)
{
cout wrong; //如失敗提示失敗信息
}
cout”已經插入「4,5,6」”endl;
cout”單鏈表a的長度為:”endl;
couta.Length()endl; //返回單鏈表長度
coutendl;
cout”單鏈表a的元素為:”endl;
a.PrintList(); //顯示鏈表中所有元素
coutendl;
cout”按位查找第二個元素:”endl;
cout”第二個元素為:”;
couta.Get(2)endl; //查找鏈表中第二個元素
coutendl;
cout”按值查找5″endl;
cout”值為5的元素位置為:”;
couta.Locate(5)endl; //查找元素5,並返回在單鏈表中位置
coutendl;
cout”執行刪除4的操作”endl;
a.Delete(1); //刪除元素4
cout”已刪除成功,單鏈表a的長度為”;
couta.Length()endl;
coutendl;
cout”鏈表a中的元素為:”endl;
a.PrintList();
int r[ ]={1,2,3,4,5};
LinkList int b(r,5); //根據數組創建順序表
cout”執行插入操作前單鏈表b為:”endl;
b.PrintList(); //輸出單鏈表所有元素
cout”插入前單鏈表b的長度為:”;
coutb.Length()endl;
try
{
b.Insert(3,8);
}
catch(char* wrong)
{
cout wrong; //如失敗提示失敗信息
}
cout”執行插入操作後單鏈表b為:”endl;
b.PrintList(); //輸出單鏈表b所有元素
cout”插入後單鏈表b的長度為:”;
coutb.Length()endl;
coutendl;
try
{
if(a.Length()){
cout”執行刪除第一個元素操作:”endl;
coutendl;
b.Delete(1); //刪除b中第一個元素
cout”已刪除成功,單鏈表b的長度為:”;
coutb.Length()endl;
}
else{
cout”順序表b長度為0″endl;
}
}
catch(char* wrong)
{
cout wrong; //如失敗提示失敗信息
}
cout”執行插入操作後單鏈表b為:”endl;
b.PrintList(); //輸出單鏈表所有元素
}
如何創建c語言的雙向循環鏈表
#includestdio.h
#includestdlib.h
#includeiostream
using namespace std;
int n;
typedef struct node
{
int data;
struct node *prior;
struct node *next;
}node,*DoubleLinkList;
void Creat_List(DoubleLinkList L)
{
DoubleLinkList r,s;
cout”請輸入n的值”endl;
cinn;
s = L;
s-prior = s;
s-data = n;
for(int i=0;in;i++)
{
r = (DoubleLinkList)malloc(sizeof(node));
L-next = r;
r-next = s-next;
r-prior = L;
s-next-prior = r;
r-data = i;
L = r;
}
}
void Desplay_List(DoubleLinkList L)
{
DoubleLinkList p;
p = L-next;
for(int i=0;in;i++)
{
cout”輸出:”p-dataendl;
p = p-next;
}
}
void Destroy_List(DoubleLinkList L)
{
DoubleLinkList p;
p = L;
node *temp;
for(int k=0;kn;k++)
{
p = L-next;
temp = p-next;
temp-prior-next = temp-next;
temp-next-prior = temp-prior;
free(temp);
}
cout”空間釋放成功”endl;
}
void main()
{
node a;
DoubleLinkList p;
p = a;
Creat_List(p);
Desplay_List(p);
Destroy_List(p);
}
由於我也不熟悉,我自己保存的創建例子,希望對你有幫助~
使用C語言實現雙向鏈表的建立、刪除和插入
#includestdio.h
#includestdlib.h
#includemalloc.h
struct list{
int data;
struct list *next;
struct list *pre;
};
typedef struct list node;
typedef node *link;
link front=NULL,rear,ptr,head=NULL;
link push(int item){
link newnode=(link)malloc(sizeof(node));
newnode-data=item;
if(head==NULL)
{
head=newnode;
head-next=NULL;
head-pre=NULL;
rear=head;
}
else
{
rear-next=newnode;
newnode-pre=rear;
newnode-next=NULL;
rear=newnode;
}
return head;
}
void makenull(){
front=NULL;
rear=NULL;
}
empty(){
if(front==NULL)
return 1;
else
return 0;
}
int tops(){
if(empty())
return NULL;
else
return rear-data;
}
void pop(){
if(empty())
printf(“stack is empty!\n”);
else
rear=rear-pre;
}
void display(link l){
link p;
p=l;
while(p!=NULL){
printf(“%d-“,p-data);
p=p-next;
}
}
void main(){
int n,i;
printf(“input your first data!\n”);
scanf(“%d”,n);
front=push(n);
/*another data*/
for(i=0;i3;i++)
{
printf(“input:\n”);
scanf(“%d”,n);
push(n);
}
ptr=front;
display(ptr);
printf(“\n Please enter any key to pop”);
pop();
ptr=front;
display(ptr);
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/156600.html