當前位置:首頁 » 編程語言 » c語言單鏈表的初始化
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言單鏈表的初始化

發布時間: 2022-02-13 11:08:18

c語言數據結構單鏈表初始化

你說的那個是逆序建立n個節點的鏈表,建完頭結點後由於鏈表中沒有其他節點就把頭結點的next置為空,這一點如果不明白的話去看鏈表那一章,有介紹!!!

之後從後往前建立鏈表!!剛開始是L->next=NULL

要注意p是每次新建的節點,這樣從後往前以此建立鏈表,只要明白p是每次新建的節點,和從後往前建立,就會明白的!!給你花了張圖!!

Ⅱ c語言單鏈表初始化問題

這里q是什麼,q是形參,你無論怎麼變q,出了函數傳進來的值還是不變。假如Node是一個int,你傳入一個int* q,你要改變q是不是用*q=0;
一樣的,在這個例子里,我會傳入一個Node** q ,然後所有q改成*q 來實現。
表達能力不行,反正就那意思吧。應該沒錯,嗯。

Ⅲ c語言數據結構單鏈表的初始化 插入 銷毀 元素的取出 刪除 操作 求詳細C源代碼 謝謝。。。。

單鏈表功能大全,嘿嘿
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
int nDate;
struct node *pstnext;
}Node;
//鏈表輸出
void output(Node *head)
{
Node *p = head->pstnext;
while(NULL != p)
{
printf("%d ", p->nDate);
p = p->pstnext;
}
printf("\r\n");
}
//鏈表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
printf("分配內存失敗\r\n");
return NULL;
}
head->pstnext = NULL;

p = head;
while(cycle)
{
printf("請輸入數據且當輸入數據為0時結束輸入\r\n");
scanf("%d", &Date);
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
printf("分配內存失敗\r\n");
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}
//單鏈表測長
void length(Node *head)
{
Node *p = head->pstnext;
int j=0;
while(NULL != p)
{
p = p->pstnext;
j++;
}
printf("%d\r\n", j);
}
//鏈表按值查找
void research_Date(Node *head, int date)
{
Node *p;
int n=1;
p = head->pstnext;
while(NULL != p && date != p->nDate)
{
p = p->pstnext;
++n;
}
if(NULL == p)
{
printf("鏈表中沒有找到該值");
}else if(date == p->nDate)
{
printf("要查找的值%d在鏈表中第%d個位置\r\n", date, n);
}
return;
}
//按序號查找
void research_Number(Node *head, int Num)
{
Node *p=head;
int i = 0;
while(NULL != p && i < Num)
{
p = p->pstnext;
i++;
}
if(p == NULL)
{
printf("查找位置不合法\r\n");
}else if(i == 0)
{
printf("查找位置為頭結點\r\n");
}else if(i == Num)
{
printf("第%d個位置數據為%d\r\n", i, p->nDate);
}
}
//在指定元素之前插入新結點
void insert_1(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre && j < i-1)
{
pre = pre->pstnext;
j++;
}
if(NULL == pre || j > i-1)
{
printf("插入位置不存在\r\n");
}else
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配內存失敗\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}

}
//在指定元素之後插入新結點
void insert_2(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre->pstnext && j < i)
{
pre = pre->pstnext;
j++;
}
if(j == i)
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配內存失敗\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}else
{
printf("插入位置不存在\r\n");
}
}
//刪除指定結點
void Delete_1(Node *head, int i3)
{
Node *p = head, *pre = NULL;
int j = 0;
while(NULL != p && j < i3)
{
pre = p;
p = p->pstnext;
j++;
}
if(NULL == p)
{
printf("刪除位置不存在\r\n");
}else
{
pre->pstnext = p->pstnext;
free(p);
}
}
//指定刪除單鏈表中某個數據,並統計刪除此數據的個數
int Delete_2(Node *head, int Delete_date)
{
int count = 0;
Node *p = head, *q;
while(NULL != p->pstnext)
{
q = p->pstnext;
if(q->nDate == Delete_date)
{
p->pstnext = q->pstnext;
free(q);
++count;
}
else
{
p = q;
}
}
return count;
}
//鏈表逆置
void Reverse_list(Node *head)
{
Node *q, *s;
if(NULL == head->pstnext || NULL == head->pstnext->pstnext)
{
return;
}
q = head->pstnext->pstnext;
head->pstnext->pstnext = NULL;
while(NULL != q)
{
s = q->pstnext;
q->pstnext = head->pstnext;
head->pstnext = q;
q = s;
}
}
//單鏈表的連接
void connect_list(Node *head, Node *head_New)
{
Node *p = head;
while(NULL != p->pstnext)
{
p = p->pstnext;
}
p->pstnext = head_New->pstnext;
}
//單鏈表銷毀
void destroy_list(Node* head)
{
while (NULL != head)
{
Node* temp = head;
head = head->pstnext;
free(temp);
}
}
void main()
{
int date, num; //待查找數據
int i3; //指定刪除元素的位置
int i1, i2, Newdate_1, Newdate_2; //待插入的新數據
int Delete_date, k; //待刪除的數據與其個數
Node *Head = NULL; //定義頭結點
Node *Head_New = NULL;

//鏈表建立
Head = creat();
printf("輸出建立的單鏈表\r\n");
output(Head);

//單鏈表測長
printf("單鏈表長度為\r\n");
length(Head);

//鏈表按值查找
printf("請輸入待查找的數據\r\n");
scanf("%d", &date);
research_Date(Head, date);

//鏈表按序號查找
printf("請輸入待查找序號\r\n");
scanf("%d", &num);
research_Number(Head, num);

//在指定第i1個元素之前插入新元素Newdate
printf("在指定第i個元素之前插入新元素Newdate");
printf("請輸入i與元素且以逗號間隔\r\n");
scanf("%d,%d", &i1, &Newdate_1);
insert_1(Head, i1, Newdate_1);
printf("插入後新鏈表\r\n");
output(Head);

//在指定第i2個元素之後插入新元素Newdate
printf("在指定第i個元素之後插入新元素Newdate");
printf("請輸入i與元素且以逗號間隔\r\n");
scanf("%d,%d", &i2, &Newdate_2);
insert_2(Head, i2, Newdate_2);
printf("插入後新鏈表\r\n");
output(Head);

//指定刪除i3元素
printf("刪除元素的位置\r\n");
scanf("%d", &i3);
Delete_1(Head, i3);
printf("刪除後新鏈表\r\n");
output(Head);

//指定刪除單鏈表中某個數據,並統計刪除此數據的個數
printf("請輸入待刪除的元素\r\n");
scanf("%d", &Delete_date);
k = Delete_2(Head, Delete_date);
printf("刪除後新鏈表\r\n");
output(Head);
printf("刪除指定元素在鏈表中的個數為:");
printf("%d\r\n", k);

//單鏈表逆置
Reverse_list(Head);
printf("逆置後輸出\r\n");
output(Head);

//單鏈表的連接
printf("建立一個新鏈表\r\n");
Head_New = creat();
printf("輸出新鏈表");
output(Head);
printf("將新鏈表連接到原來鏈表的尾部並輸出\r\n");
connect_list(Head, Head_New);
output(Head);
destroy_list(Head);

return;
}

Ⅳ 大俠們怎麼用C語言實現初始化一個鏈表啊~~~~~~~~~~

單鏈表:

#include<stdio.h>
#include<stdlib.h>

typedef struct LNode
{
char data;
struct LNode *next;
}LNode,*LinkList;

void InitList(LinkList *L) //初始化
{
*L = (LinkList)malloc(sizeof(LNode));
(*L)->next = NULL;
}
char GetElem(LinkList L,int i) //返回i 位置的元素
{
//L為帶頭結點的單鏈表的頭指針。
LinkList p;
int j;
p = L->next; j = 1; //初始化,p指向第一個結點,j為計數器

while(p && j<i)
{
p = p->next;
++j;
}
if(!p || j>i) exit(1); //i位置元素不存在
return(p->data);
}

void ListInsert(LinkList *L,int i,char e)//插入
{
//在帶頭結點的單鏈線性表L中第i個位置前插入元素e
LinkList p,s;
int j;
p = *L; j = 0;
while(p && j<i-1)//尋找第i-1個結點
{
p = p->next;
++j;
}
if(!p || j>i-1) exit(1); //i位置不正確
s = (LinkList)malloc(sizeof(LNode)); //新結點
s->data = e;
s->next = p->next;
p->next = s;
}

void ListDelete(LinkList *L,int i)//刪除i位置元素
{
LinkList p,q;
int j;
p = *L; j = 0;

while(p->next && j<i-1)//尋找第i個結點
{//這以p->next開始是為了避開頭結點,頭結點不可刪除
//而且這樣是必須的,這樣是為了有被刪除點前面結點的地址,這樣才能有前繼結點,連接刪除點的後繼結點.
p = p->next;
++j;
}

if(!(p->next) || j>i-1) exit(1); //刪除位置不合理
q = p->next; //刪除該點
p->next = q->next;
free(q);
}

void CreateList(LinkList *L,int n)
{
//逆位序輸入n個元素的值,建立帶表頭結點的單鏈線性表L
int i;
LinkList p;
(*L) = (LinkList)malloc(sizeof(LNode));
(*L)->next = NULL; //先建立帶頭結點的單鏈表
for(i=n;i>0;--i)
{
p = (LinkList)malloc(sizeof(LNode)); //生成新結點
scanf("%c",&p->data);
fflush(stdin);
p->next = (*L)->next;
(*L)->next = p;
}

//*****************另一插入法**************************
q = *L = (LinkList)malloc(sizeof(LNode));
(*L)->next = NULL;

for(i=n;i>0;--i)
{
p = (LinkList)malloc(sizeof(LNode));
scanf("%c",&p->data);
fflush(stdin);
p->next = NULL;
q->next = p;
q = p;
//q->next = p;
}
//***********************************************************
}
void MergeList(LinkList *La,LinkList *Lb,LinkList *Lc)
{
//鏈表La,Lb按遞增排列
LinkList pa,pb,pc;
pa = (*La)->next;
pb = (*Lb)->next;
(*Lc) = pc = (*La);

while(pa && pb)
{
if(pa->data <= pb->data)
{
pc->next = pa;
pc = pa;
pa = pa->next;
}
else
{
pc->next = pb;
pc = pb;
pb = pb->next;
}
}
pc->next = pa?pa:pb; //插入剩餘段
//free(Lb); //釋放Lb頭結點
}
void display(LinkList *L)
{
LinkList di;
di = (*L)->next;
while(di!=NULL)
{
printf("%c\t",di->data);
di = di->next;
}
printf("\n");
}
void main()
{
LinkList link,link2,link3;

InitList(&link);
InitList(&link2);
CreateList(&link,5);
CreateList(&link2,5);
display(&link);
display(&link2);
MergeList(&link,&link2,&link3);
display(&link3);
}

Ⅳ c語言鏈表初始化中

p = head->next;??這個怎麼理解

本來head->next = NULL;
現在 p = NULL?何意?

16行報錯 8成是你的類型定義的有問題
typedef struct _node
{
struct _node *pPri;
struct _node *pNext;
int a;

}sNode,*psNode;

Ⅵ 怎麼用C語言初始化一個鏈表啊~

所謂初始化鏈表就是把鏈表的數據置空,也就是所有的數據,指針都為null
舉個單鏈表的例子

該演算法的結果將單鏈表head置為空表,只需要將頭節點的指針置為null即可。演算法實現如下

void setnull(struct Lnode *head)
{
head->next=NULL;
}

雙鏈表和循環鏈表依次類推·

Ⅶ C語言初始化單鏈表!

在單鏈表A中刪除所有和單鏈表B中元素相同的結點
#include
"stdafx.h"
#include
<stdio.h>
#include
<malloc.h>
#define
SIZE
sizeof(struct
node)
struct
node{
int
data;
struct
node
*next;
};
void
init(struct
node
*LC){
int
n;
struct
node
*Str,*p;
p=(struct
node
*)malloc(SIZE);
Str=LC;
printf("請輸入鏈表A,以小於零的數結束輸入:\n");
scanf("%d",&n);
while(n>=0){
p->data=n;
Str->next=p;
Str=p;
p=(struct
node
*)malloc(SIZE);
scanf("%d",&n);
Str->next=NULL;
}
printf("您輸入的序列的是:\n");
for
(Str=LC->next;Str!=NULL;){
printf("%d
",Str->data);
Str=Str->next;
}
printf("\n");
}
void
delet_LA(struct
node
*LA,struct
node
*pa){
struct
node
*p;
struct
node
*q;
p=LA;
q=p;
for
(p;p!=pa;){
q=p;
p=p->next;
}
q->next=pa->next;
}
void
delete_same(struct
node
*LA,struct
node
*LB){
struct
node
*pa,*pb;
pa=LA->next;
pb=LB->next;
for
(pb;pb!=NULL;){
for
(pa;pa!=NULL;){
if
(pb->data==pa->data){
delet_LA(LA,pa);
pa=LA->next;
}
else{
pa=pa->next;
}
}
pb=pb->next;
pa=LA->next;
}
printf("處理後的單鏈表A為:\n");
for
(pa=LA->next;pa!=NULL;){
printf("%d
",pa->data);
pa=pa->next;
}
}
void
main(){
struct
node
*LA;
struct
node
*LB;
LA=(struct
node
*)malloc(SIZE);
if(LA!=NULL)
LA->data=-1;
LB=(struct
node
*)malloc(SIZE);
if(LB!=NULL)
LB->data=-1;
init(LA);
init(LB);
delete_same(LA,LB);
}

Ⅷ c語言,單鏈表,這個p不是初始化了嗎怎麼改

#include<stdio.h>
#include<stdlib.h>

typedefintElemType;
typedefstructnode{
ElemTypedata;
structnode*next;
}*LinkList,*pNode,NODE;

LinkListCreatList(){
pNodep,head;
ElemTypee;
head=p=(pNode)malloc(sizeof(NODE));
while(scanf("%d",&e)==1&&e){
p->next=(pNode)malloc(sizeof(NODE));
p->next->data=e;
p=p->next;
}
p->next=NULL;
returnhead;
}

voidShowList(LinkListhead){
pNodep=head->next;
while(p){
printf("%d",p->data);
p=p->next;
}
printf(" ");
}

intmain(){
LinkListhead=CreatList();
if(head)ShowList(head);
return0;
}

Ⅸ 求用c語言初始化一個單鏈表,然後插入刪除的源代碼

# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>

typedef struct Node
{
int date; //數據域
struct Node * pNext; //指針域
}NODE,*PNODE;

PNODE create_list(void); //創建鏈表
void traverse_list(PNODE); //遍歷鏈表
bool is_empty(PNODE); //鏈表是否為空
int list_len(PNODE); //求鏈表長度
bool insert_list(PNODE,int , int ); //插入鏈表結點
bool del_list(PNODE, int, int *); //刪除鏈表結點
void sort_list(PNODE); //排序

int main(void)
{
int len;
int dval = 0;

PNODE pHead = NULL;

pHead = create_list();

// insert_list(pHead, 3, 26);
traverse_list(pHead);
del_list(pHead,2,&dval);
printf("刪除的值是:%d\n",dval);

// sort_list(pHead);

traverse_list(pHead);

if ( is_empty(pHead) )
{
printf("鏈表是空的!\n");
}

len = list_len(pHead);

printf("鏈表的長度是:%d\n",len);

return 0;
}

PNODE create_list(void)
{
int i;
int len;
int val;

PNODE pHead = (PNODE)malloc(sizeof(NODE)); //創建頭結點

if (NULL == pHead)
{
printf("鏈表創建失敗!\n");
exit(-1);
}

PNODE pTail = pHead; //創建尾結點
pTail->pNext = NULL;

printf("請輸入要創建的鏈表: ");
scanf("%d",&len);

for (i=0; i<len; i++)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));

if (NULL == pNew)
{
printf("鏈表創建失敗,可能原因:內存不足!\n");
exit(-1);
}

printf("請輸入第%d個的值: ",i+1);
scanf("%d",&val);

pNew->date = val;
pTail->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew;
}

return pHead;
}

void traverse_list(PNODE pHead)
{
PNODE p = pHead->pNext;

while ( NULL != p)
{
printf("%d ",p->date);
p = p->pNext;
}

printf("\n");

return ;
}

bool is_empty(PNODE pHead)
{
if ( NULL == pHead->pNext )
{
return true;
}else
{
return false;
}

}

int list_len(PNODE pHead)
{
int len = 0;

PNODE p = pHead->pNext;

while ( NULL != p )
{
len++;
p = p->pNext;
}

return len;
}

bool insert_list(PNODE pHead, int place, int val) //我自己的:
{ /*
int len = list_len(pHead);
int i;

if ( place > len)
{
printf("Error!\n");
return false;
// exit(-1);
}

PNODE p = (PNODE)malloc(sizeof(NODE));

PNODE prev;

if (NULL == p)
{
printf("內存分配失敗!\n");
return false;
// exit(-1);
}

p->date = val;

PNODE temp = pHead;

for (i=0; i<place; i++)
{
prev = temp;
temp = temp->pNext;
}

p->pNext = temp;
prev->pNext = p;
*/

// 傳說中,一個牛人的經典演算法:

int i = 0;
PNODE p = pHead;

while (NULL != p && i < place-1)
{
p = p->pNext;
i++;
}

if (i > place-1 || NULL == p)
return false;

PNODE pNew = (PNODE)malloc(sizeof(NODE));

if (NULL == pNew)
{
printf("分配失敗了!\n");
return false;
}

pNew->date = val;
PNODE q = p->pNext;
p->pNext = pNew;
pNew->pNext = q;

return true;
}

bool del_list(PNODE pHead, int place, int * pval)
{ /* 我自己的演算法:
int len = list_len(pHead);
int i;

if (place > len)
{
printf("Error! 下標越界!\n");
return -1;
}

PNODE pTem;
PNODE temp = pHead;
PNODE prev;

for (i=0; i<place; i++)
{
prev = temp;
temp = temp->pNext;
}

prev->pNext = temp->pNext;
pTem = temp;
* pval = temp->date;
free(pTem);
*/
// 牛人優化的代碼

int i = 0;
PNODE p = pHead;

while ( NULL != p->pNext && i < place - 1 )
{
p = p->pNext;
i++;
}

if ( NULL == p->pNext || i >= place - 1)
{
printf("ERROR! 刪除錯誤!!\n");
return false;
}
PNODE pLast = (PNODE)malloc(sizeof(NODE));
pLast = p->pNext;
*pval = pLast->date;
p->pNext = pLast->pNext;

free(pLast);

return true;
}

void sort_list(PNODE pHead)
{
int i,j=0,t;
int len = list_len(pHead);

PNODE a; //指向首節點
PNODE b ; //指向首節點後的位置
// PNODE t = (PNODE)malloc(sizeof(NODE));

for (i=0,a = pHead->pNext; i<len-1; i++, a = a->pNext)
{
for (j=0, b = a->pNext; j<len-i-1; j++,b = b->pNext)
{
if ( a->date < b->date )
{
t = a->date;
a->date = b->date;
b->date = t;
}
}
}

return ;
}

Ⅹ C語言初始化單鏈表!

定義單鏈表就是可以不用頭指針,你的想法是對的!