日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

单链表操作

發布時間:2025/5/22 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 单链表操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

單鏈表:
1 2 3 4 5 6

1.設計節點
typedef int datatype;
typedef struct node
{
datatype data;
struct node *next;

}listnode,*linklist;

listnode a; === struct node a;
linklist p =malloc === struct node *p =malloc;

2.初始化空鏈表
1.linklist L =NULL;


2.
linklist init_list()
{
linklist L=malloc(sizeof(listnode))
L->next=NULL;
return L
}


3.插入(尾)

bool insert_node( int data,linklist L)
{
//產生新節點
linklist new=malloc(sizeof(listnode));
new->data=data;
new->next=NULL;

//定位
linklist p=L;
while(p->next!=NULL)
{
p=p->next;

}
//新節點插入

p->next=new;
return true;
}

4.顯示
show(linklist L)
{
p=L->next;
while(p!=NULL)
{
printf( ,p->data);
p=p->next;
}

}
-----------
void revert(linklist L)
{

//p指向要插入到排序好鏈的節點(也是未排序好的鏈表的第一節點)
//q 指向未排序好的鏈表的第二節點
linklist p,q;

p=L->next;
L->next=NULL;

while(p!=NULL)
{
q=p->next;
p->next=L->next;
L->next=p;
p=q;

}

}

================================================================================
int main()
{
linklist L;
L=init_list();

for(i=1;i<=5;i++)
{
insert_node(i,L)

}
show(L);
revert(L)
show(L)

}

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <errno.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>

#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <pthread.h>

typedef int datatype;

struct node
{
datatype data;
struct node *next;
};

struct node *init_list(void)
{
struct node *p = malloc(sizeof(struct node));
p->next = NULL;

return p;
}

void insert(int n, struct node *head)
{
// 創建一個新節點
struct node *new = malloc(sizeof(struct node));
new->data = n;
new->next = NULL;

// 找到單鏈表的最后一個節點
struct node *p = head;
while(p->next != NULL)
{
p = p->next;
}

// 將最后這個節點的next指向新節點
p->next = new;
}

void show(struct node *head)
{
struct node *p = head->next;

while(p != NULL)
{
printf("%d\t", p->data);
p = p->next;
}

printf("\n");
}

//單鏈表的逆序

void inverse(struct node *head)
{
struct node *p, *q;
p = head->next;

head->next = NULL;//將鏈表斷成兩個鏈表

while(p != NULL)
{
q = p->next;
p->next = head->next;
head->next = p;

p = q;


}
}

int main(int argc, char **argv)
{
struct node *head;
head = init_list();

int n;
scanf("%d", &n);

int i;
for(i=1; i<=n; i++)
{
insert(i, head);
}
show(head);

inverse(head);
show(head);

return 0;
}

轉載于:https://www.cnblogs.com/defen/p/5201829.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的单链表操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。