• 线性表的链式存储C语言版


    #include <stdio.h>
    #include <malloc.h>
    
    #define N 10
    
    typedef struct Node
    {	
    	int data;
    	struct  Node *next;
    }Node, *LinkedList;
    
    void initList(LinkedList *L);
    int ListInsert(LinkedList *L, int index, int e);
    int ListDelete(LinkedList *L, int index, int *e);
    void printList(LinkedList L);
    
    int main(int argc, char const *argv[])
    {
    	int e;
    	LinkedList L=NULL;
    	initList(&L);
    	for (int i = 1; i <= N; ++i)
    	{
    		ListInsert(&L,i,i);
    	}
    	printList(L);
    	for (int i = L->data; i >=1; --i)
    	{
    		ListDelete(&L,i,&e);
    		printf("%d	", e);
    	}
    	return 0;
    }
    
    
    
    void initList(LinkedList *L)
    {
    	
    	(*L) = (Node*)malloc(sizeof(Node));
    	(*L)->data = 0;
    	(*L)->next = NULL;
    }
    
    int ListInsert(LinkedList *L, int index, int e)
    {
    	if(index>(*L)->data+1 || index<=0) return -1;
    	int i = 0;
    	Node *p = (*L);
    
    	while(p)
    	{
    		
    		if(i==index-1)
    		{
    				Node *t = (Node*)malloc(sizeof(Node));
    				t->data = e;
    				t->next = p->next;
    				p->next = t;
    				(*L)->data += 1;
    				return 0;
    		}
    		++i;
    		p = p->next;
    	}
    
    
    
    
    }
    int ListDelete(LinkedList *L, int index, int *e)
    {
    	if(index<=0 || index>=(*L)->data+1) return -1;
    	int i=0;
    	Node* p = (*L);
    	while(p)
    	{
    		if(i==index-1)
    		{
    			Node *q = p->next;
    			p->next = q->next;
    			*e = q->data;
    			free(q);
    			q = NULL;
    			return 0;
    		}
    
    		++i;
    		p = p->next;
    	}
    }
    void printList(LinkedList L)
    {
    	Node *p = L;
    	while(p->next)
    	{
    		p = p->next;
    		printf("%d	",p->data);
    	}
    }

  • 相关阅读:
    几种常用的排序算法
    Charles 抓包工具安装和采坑记录
    当你骂特朗普的时候你究竟在骂什么
    苹果公司的另一面:沃兹尼亚克
    网络爬虫设计中需要注意的几个问题
    微信小程序 canvas 绘图问题总结
    自己动手做智能家居之:智能空调控制
    Allegro导入PADS文件
    C#
    C#
  • 原文地址:https://www.cnblogs.com/yldf/p/11900154.html
Copyright © 2020-2023  润新知