1. 程式人生 > >動態連結串列的建立

動態連結串列的建立

 #include <stdio.h>
//List結構樣式
typedef struct node
{ int data;
struct node *next;
}Node;

//建立head的空鏈
Node *createList()
{
Node *head = (Node *)malloc(sizeof(Node));
if(NULL == head)
exit(-1);
head->next = NULL;
return head;
}

Node *insertList(Node *head,int i) //頭插法插入連結串列資料
{
Node *cur = (Node *)malloc(sizeof(Node));
cur->data = i;
cur->next = head->next;
head->next = cur;
return head;
}

void traverseList(Node *head) //將連結串列節點中的資料輸出
{
head = head->next;
while(head)
{
printf("%d\n",head->data);
head = head->next;
}
}

int main(void)
{
Node *head = createList(); //建立帶頭節點的空連結串列
for(int i =1; i<=10; i++)
insertList(head,i);
traverseList(head);
return 0;
}

相關推薦

動態連結串列建立及使用

連結串列的建立及使用一直是一個小難題,今天我分享一下我的學習心得吧。 首先,要建立一個頭結點。 struct node { int data; struct node *next; }; 完成後分為4步:1、建立連結串列 2、輸

動態連結串列建立

 #include <stdio.h> //List結構樣式 typedef struct node { int data; struct node *next; }Node; //建立head的空鏈 Node *

單鏈表的建立(靜態連結串列動態連結串列)

       連結串列是一種很重要的資料結構,在實際工作中也有著很多的應用,這次我們先討論一下最簡單的單鏈表。        關於單鏈表的操作有很多,當然最基本的就是要建立一個單鏈表,對於單鏈表來說,可以按照型別分為靜態連結串列和動態連結串列。 1.靜態連結串列 靜態連結串

簡單靜態、動態連結串列建立

靜態連結串列的建立: #include <stdio.h> struct student { int number; float score; struct student *next; };

【C語言】動態連結串列和靜態連結串列建立

動態連結串列和靜態連結串列 #include<stdio.h> #include<stdlib.h> #include<malloc.h> struct wep{

C語言建立動態連結串列

    所謂建立動態連結串列是指在程式執行過程中從無到有地建立起一個連結串列,即一個一個地開闢結點和輸入各結點資料,並建立起前後相鏈的關係。 程式碼如下: #include <stdio.h> #include <stdib.h。 #define LEN

C語言_動態連結串列的簡單建立與節點刪除

程式詳解:        1、輸入學生的姓名和年齡,進行順序排列。        2、刪除其中某位學生的資料 #include<stdio.h> #include<stdlib.h> #include<string.h> #define

動態連結串列建立、節點記憶體空間申請以及釋放

1.動態連結串列的初始化: typedef struct _STACK{     void* data;     int size;     struct _STACK* next;     struct _STACK* pre;} STACK; STACK *stack;

動態連結串列建立—尾插法

今天嘗試了簡單動態連結串列的建立  不幸的是失敗了  目前還沒有找出錯在哪兒  明天一定要搞清楚錯誤的原因  到時在對本文進行修改 #include <iostream> using namespace std; struct node {int num;str

動態連結串列建立(程式碼)

/*** 動態連結串列的建立 ***/#include<stdio.h>#include<stdlib.h>#define ID struct Student#define LEN sizeof(ID)    //該巨集定義為後續結構體的使用提供便捷ID * creative( int

動態連結串列建立,輸出,刪除,新增

動態連結串列的建立: #include <stdio.h> #include <malloc.h> #define NULL 0 #define LEN sizeof(struct student) struct student {

C語言-動態連結串列建立遍歷與插入

#include<stdio.h> #include<stdlib.h> #include<string.h> #define P printf typedef struct num {int id;struct num *next; }

linux 下c語言建立單向動態連結串列的理解

#include <stdio.h> #include <malloc.h>              //分配記憶體要加上這個庫函式 struct weapon {   int price;   int atk;   struct weapon* next; };  //建立一個武器

資料結構(連結串列系列):連結串列建立連結串列刪除特定節點,連結串列氣泡排序,連結串列快速排序

一、連結串列的理解: 1,各個節點間地址存放可以不連續,雖說是表,但是指標存在是為了找到其他的節點,如果連續了,都沒必要用連結串列了。 2,各節點依賴上一節點,要找到某一個節點必須找到他的上一個節點,所以要訪問連結串列,必須要知道頭指標,然後從頭指標訪問開始。 3,各節點間原來是獨立的,本

購物表(動態連結串列)+鬧鐘提醒(多執行緒)

基本連結串列的應用 增 刪 查 找 排 模糊查詢 核心程式碼如下: #include <stdio.h> #include <math.h> #include <string.h> #include <malloc.h> #incl

單向非迴圈連結串列連結串列建立、節點插入、連結串列列印、節點長度計算、連結串列清空、連結串列銷燬

/* 單向非迴圈連結串列:    初始化    前插入     後插入    列印    連結串列長度    清空 &

約瑟夫環問題(動態連結串列操作)n個學生圍成一圈,每m個出隊,輸出所有出隊的序列

 需求:掌握連結串列的簡單操作(增刪查改)詳解在備註中指出 #include <iostream> #include <stdlib.h> #include <stdio.h> /* run this program using the

c學習筆記--5 結構體實現動態連結串列

這裡不得不多說一句,對於c來說指標我認為最好用的就是連結串列,有很多實用的地方 #include<string.h> #include<stdio.h> //C語言 連結串列篇 //結構體實現單向連結串列 struct MyStruc

線性表:動態連結串列

在資料結構中我們經常會用到連結串列來處理一些問題, 連結串列同順序表一樣, 包括靜態連結串列和動態連結串列, 靜態連結串列並不是很常用, 但動態連結串列的重要性是不言而喻的. 接下來我們看一下動態連結串列的基本操作, 以書上的資料為參考, 同時自己對動態連結串列增加了新的認識和總結

133UVa救濟金髮放——動態連結串列

注意,當要刪除的兩個節點相鄰時,counter= counter->right;  clock= clock->left;  #include<stdio.h> class