1. 程式人生 > >c++ 建立簡單鏈表

c++ 建立簡單鏈表

// First.cpp: 定義控制檯應用程式的入口點。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

typedef struct List {
	int data;
	struct List *next;
}List;//定義一個結構體  儲存連結串列  資料+指標
List* creat() {//建立連結串列
	List *p ,*head;
	head = new List;//宣告頭節點
	p = head;//宣告移動指標  最開始指向頭結點
	int x, cycle = 1;
	while (cycle)
	{
		cout << "Please input the data for single linker : ";
		cin >> x;

		if (x != 0)
		{
			List *s = new List;//宣告List物件來儲存資料  這就是一個連結串列的節點
			s->data = x;//將資料儲存到此節點
			cout << "Input data : " << x << endl;
			p->next = s;//將此節點與頭節點連線
			p = s;//向後移動指標,以便下一次連結   p一直指向連結串列最後的一個節點
		}
		else
		{
			cycle = 0;
			cout << "Input done! " << endl;
		}
	}
	head = head->next;
	p->next = NULL;
	return head;
}
void print(List *head) {//列印輸出連結串列
	List *p = head;
	while (p != NULL) {
		cout << p->data;
		p = p->next;
	}
}
int main()
{
        List *head = creat();//引用建立連結串列
	print(head);//引用列印
	free(head);
	return 0;

}

記錄下來  防止以後忘了  簡單的連結串列實現  包括建立列印 

相關推薦

c++ 建立單鏈

// First.cpp: 定義控制檯應用程式的入口點。 // #include "stdafx.h" #include <iostream> using namespace std; typedef struct List { int data; str

含頭結點的C++尾插法建立單鏈並輸出

#include <iostream> using namespace std; const int N = 10; struct Node { int data; Node *ne

c實現單鏈

clu eof print 實現 ext 實現簡單 std [0 des #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next

c++ 建立有序單鏈,以及兩個有序單鏈合併

首先是Node結點的建立 //建立結點 class Node { public: Node(){ next=NULL;data=0; } Node(int n){ next=NULL;da

C語言」單鏈/雙向鏈建立/遍歷/插入/刪除

ins lin mon 雙向鏈表 gte aix5 tag cbe ssp MVC%E6%9E%B6%E6%9E%84%E5%AD%A6%E4%B9%A0%E4%B9%8BEasyFirst%E2%80%94%E2%80%94%E5%BF%AB%E7%82%B9%E5%A4

C語言建立迴圈單鏈並輸出

Description  依次輸入n(n>0)個整數,建立帶表頭結點的迴圈單鏈表,並依次輸出單鏈表中的元素值。 提示: 結點結構如下: typedef struct Node {      int data;   &

C++資料結構-單鏈建立

    前邊我們建立了順序儲存結構的線性表,簡稱順序表,順序表最大的問題是:插入和刪除需要移動大量的元素。為了解決 這個問題, 我們引入鏈式儲存結構的線性表,簡稱連結串列,連結串列與順序表不同,連結串列的每個結點在記憶體中是分開存放的,每個結點都包含資料域和指標域:     

C語言實現單鏈建立、插入、刪除

節點資料結構: /* *定義鏈式儲存線性表的結構 */ typedef struct LNode { int data; //資料域 struct LNode * next;

單鏈C語言實現

連結串列原理見連結 函式名取得不是很好,大家見諒。 list標頭檔案如下: #ifndef __LIST_DEMO_H__ #define __LIST_DEMO_H__ #include <stdio.h> #include <malloc

C語言實現單鏈節點的刪除(帶頭結點)

data art pos grand urn ria tps move sni 我在之前一篇博客《C語言實現單鏈表節點的刪除(不帶頭結點)》中具體實現了怎樣在一個不帶頭結點的單鏈表的刪除一個節點,在這一篇博客中我改成了帶頭結點的單鏈表。代碼演示樣例上傳至 h

C語言實現單鏈的節點插入(帶頭結點)

alloc tails 函數 file ret con 實現 單獨 fun 我在之前一篇博客《C語言實現單鏈表(不帶頭結點)節點的插入》中具體實現了怎樣在一個不帶頭結點的單鏈表中進行節點的插入。可是在實際應用中,帶頭結點的鏈表更為經常使用。更為方便。今天我們

C語言寫單鏈的創建、釋放、追加(即總是在最後的位置增加節點)

程序 mage 而不是 自己的 物理 2-66 exit 只為 對數 昨天周末給學妹講了一些指針的知識,本來我對指針就是似懂非懂的狀態,經過昨天一講,我對指針的學習就更深刻了果然給別人講課也是學習的一個方法。加上最近復習數據結構,發現我的博客裏沒有鏈表的博文,

c語言實現單鏈的逆序輸出

<span style="font-family: Arial, Helvetica, sans-serif;">可以用遞迴,如果沒到連結串列尾,則遞迴查詢,否則輸出當前值。下面只是演算法表示,不能直接放到程式裡編譯執行。</span><span style="fo

C 將一個單鏈拆成3個迴圈連結串列,其中一個是純數字,一個純字母,一個其他字元

前面相關操作在這呢,這個函式依託於此 //結構體 typedef struct Node { ElementType data; struct Node * next; } LNode, * LinkNode; //將一個單鏈表拆成3個迴圈連結串列,其中一個是純數字

C 無頭結點單鏈若干操作

顧名思義就是從一開就是有用的結點 文章目錄 定義結構體 建立 輸出連結串列的值 在第i個位置前插入 刪除第i個節點 頭插法 尾插法 完整程式程式碼 定義結構體 typedef struct Node {

建立一個單鏈,並刪除連結串列中值為W的元素

#include<iostream> #include<algorithm> #include<string.h> #include<stdio.h> #include<malloc.h> using namespace std; typede

c語言實現單鏈的所有介面

此次工程還是使用了3個原始檔slist.h(標頭檔案原始碼),slist.c(實現介面的具體程式碼),test.c(單鏈表邏輯) slist.h #pragma once #include<stdio.h> #include<stdlib.h> #inclu

C語言》單鏈——記憶體管理

《C語言》單鏈表——記憶體管理 Main.c MemManage.h MemManage.c Main.c #define SAFE_MEMORY #include "MemManage.h" #include <time.h&

C語言》單鏈——增、刪、查、改、銷燬、排序、翻轉、合併、取中間節點...

《C語言》單鏈表——增、刪、查、改、銷燬、排序、翻轉、合併、取中間節點... Main.c S_List.h S_LIst.c Main.c #include "S_List.h" #include <time.h> v