• 线性表的链式存储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);
    	}
    }

  • 相关阅读:
    05 DDMS中logcat的使用
    04项目的目录结构分析与资源引用
    03 Android之我的第一个.hello world 程序
    关于myeclipse的有关操作
    jsp运行原理分析
    JSP简介
    prepareStatement与Statement的区别
    正则表达式
    过滤器的作用和用法
    剑指Offer--数值的整数次方
  • 原文地址:https://www.cnblogs.com/yldf/p/6249887.html
Copyright © 2020-2023  润新知