单链表操作
單鏈表:
 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位技術專家共同創作,文字、視頻、音頻交互閱讀總結
 
                            
                        - 上一篇: 加密工具类 - CryptoUtils.
- 下一篇: 网络流24题 飞行员配对方案问题
