c語言雙向關聯,c++雙向關聯

本文目錄一覽:

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-18 01:57
下一篇 2024-11-18 01:57

相關推薦

  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演着非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28
  • Python作為中心語言,在編程中取代C語言的優勢和挑戰

    Python一直以其簡單易懂的語法和高效的編碼環境而著名。然而,它最近的發展趨勢表明Python的使用範圍已經從腳本語言擴展到了從Web應用到機器學習等廣泛的開發領域。與此同時,C…

    編程 2025-04-28
  • Python基礎語言

    Python作為一種高級編程語言擁有簡潔優雅的語法。在本文中,我們將從多個方面探究Python基礎語言的特點以及使用技巧。 一、數據類型 Python基礎數據類型包括整數、浮點數、…

    編程 2025-04-28

發表回復

登錄後才能評論