單鏈表的基礎操作(頭插法、尾插法、插入和刪除)
阿新 • • 發佈:2018-11-26
一、連結串列的建立(頭插法和尾插法)
1、頭插法:把後建立的結點插在頭部。用這種方法建立起來的連結串列的實際順序與輸入順序剛好向反,輸出時為倒序!
下面附上程式碼:
struct node *headcreat() { struct node *p,*q,*head; head = (struct node*)malloc(sizeof(struct node)); p = (struct node*)malloc(sizeof(struct node)); p->next = NULL; //先讓最後一個節點p指向空 printf("請輸入數字,以0結尾:\n"); do{ q = (struct node*)malloc(sizeof(struct node)); scanf("%d",&q->data); if(q->data == 0)break; //輸入數字為0時跳出迴圈,停止操作(0不在連結串列內) L++; //L為全域性變數,用於計算連結串列長度,對於實現功能沒有影響 q->next = p->next; p->next = q; }while(true); head = p; //頭節點head為空 return (head); }
2、尾插法:將後建立的結點插在連結串列尾部,這種方法建立起來的連結串列的實際順序與輸入順序相同,在對連結串列順序有嚴格要求時,建議使用尾插法!
程式碼如下:
在這裡插入程式碼片struct node *tailcreat() { struct node *p,*q,*head; int n = 0; head = (struct node*)malloc(sizeof(struct node)); p = (struct node*)malloc(sizeof(struct node)); printf("請輸入數字,以0結尾:\n"); do{ q = (struct node*)malloc(sizeof(struct node)); scanf("%d",&q->data);L++; if(q->data == 0)break; if(n == 0) //當輸入第一個數字時,將這個節點賦值給頭節點的指標域 { head->next = q; p->next = q; p = q; n++; } p->next = q; p = q; }while(true); p->next = NULL; //容易忽略,後果就是在display時瘋狂輸出!! return (head); }
二、連結串列的輸出
這個不用多說啦~程式碼如下:
void display(struct node *head)
{
while(head->next!=NULL) //此處不能為head!=NULL,因為下面的printf輸出的是當前位置的下一個結點的資料,若寫成head->NULL,則指標指到NULL前個結點時,(head->next)->data無法輸出
{
printf("%d ",(head->next)->data);
head = head->next;
}
}
三、連結串列的插入
由於新建的連結串列是有頭結點的連結串列,固不必討論插入的位置是在頭部還是在中間或尾部,若沒有頭結點,則要分為插在頭部和插在中間(尾部)兩種情況討論,因為插在頭部需要改變head的值,而插在中間或尾部就不用改變head。這也是我建議建立有頭指標的連結串列的原因!
為了便於理解,可以畫張圖~
程式碼如下:
struct node *insert(struct node *head)
{
struct node *r,*p;
int i,j;
p = head;
r = (struct node*)malloc(sizeof(struct node));
printf("請輸入你要插入的位置:\n");
scanf("%d",&i);
printf("請輸入你要插入的數字:\n");
scanf("%d",&r->data);
for(j=1;j<i;j++) //將指標移動到要插入新結點的位置
p = p->next;
r->next = p->next;
p->next = r;
L++;
return(head);
}
四、結點的刪除
同樣不需要考慮要刪除的位置啦~原因同上
struct node *del(struct node *head)
{
struct node *p;
int i;
p = head;
printf("請輸入你要刪除的數字:\n");
scanf("%d",&i);
while(i!=(p->next)->data) //當要指標移動到要刪除的結點的前一個結點時
p = p->next;
p->next = (p->next)->next;
L--;
return(head);
}
以上就是單鏈表的基本操作我對於連結串列的j理解還不是很深刻!歡迎指正交流下面附上完整程式碼:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
int L;
struct node *tailcreat()
{
struct node *p,*q,*head;
int n = 0;
head = (struct node*)malloc(sizeof(struct node));
p = (struct node*)malloc(sizeof(struct node));
printf("請輸入數字,以0結尾:\n");
do{
q = (struct node*)malloc(sizeof(struct node));
scanf("%d",&q->data);L++;
if(q->data == 0)break;
if(n == 0)
{
head->next = q;
p->next = q;
p = q;
n++;
}
p->next = q;
p = q;
}while(true);
p->next = NULL;
return (head);
}
struct node *headcreat()
{
struct node *p,*q,*head;
head = (struct node*)malloc(sizeof(struct node));
p = (struct node*)malloc(sizeof(struct node));
p->next = NULL;
printf("請輸入數字,以0結尾:\n");
do{
q = (struct node*)malloc(sizeof(struct node));
scanf("%d",&q->data);
if(q->data == 0)break;
L++;
q->next = p->next;
p->next = q;
}while(true);
head = p;
return (head);
}
struct node *insert(struct node *head)
{
struct node *r,*p;
int i,j;
p = head;
r = (struct node*)malloc(sizeof(struct node));
printf("請輸入你要插入的位置:\n");
scanf("%d",&i);
printf("請輸入你要插入的數字:\n");
scanf("%d",&r->data);
for(j=1;j<i;j++)
p = p->next;
r->next = p->next;
p->next = r;
L++;
return(head);
}
struct node *del(struct node *head)
{
struct node *p;
int i;
p = head;
printf("請輸入你要刪除的數字:\n");
scanf("%d",&i);
while(i!=(p->next)->data)
p = p->next;
p->next = (p->next)->next;
L--;
return(head);
}
void display(struct node *head)
{
while(head->next!=NULL)
{
printf("%d ",(head->next)->data);
head = head->next;
}
}
int main()
{
struct node *head;
head = tailcreat();
display(head);
head = insert(head);
display(head);
head = del(head);
display(head);
return 0;
}