zoukankan      html  css  js  c++  java
  • 单链表的一些操作

    #include <stdio.h>
    #include <stdlib.h>
    typedef int ElemType;
    typedef struct Node 
    {
        ElemType data;               
        struct Node *next;          
    }Node,*LinkedList;
    LinkedList LinkedListInit() 
    {
        Node *L;
        L = (Node *)malloc(sizeof(Node)); 
        if(L == NULL) { 
            printf("申请内存空间失败
    ");
        }
        L->next = NULL;                  
     	return L;
    }
    LinkedList LinkedListCreat_H() {//前插法 
        Node *L;
        L = (Node *)malloc(sizeof(Node));   
        L->next = NULL;                     
        ElemType x;                         
        while(scanf("%d",&x) != EOF ){
            Node *p;
            p = (Node *)malloc(sizeof(Node));    
            p->data = x;                      
            p->next = L->next;                    
            L->next = p; 
        }
        return L; 
    } 
    LinkedList LinkedListCreat_E()//后插法 
    {
    	Node *L,*r;
    	L=(Node *)malloc(sizeof(Node));
    	L->next=NULL;
    	r=L; 
    	ElemType x;
    	while(scanf("%d",&x)!=EOF)
    	{
    		Node *p;
    		p=(Node *)malloc(sizeof(Node));
    		p->data=x;
    		p->next=NULL;
    		r->next=p;
    		r=p; 
    	}
    	return L;
    }
    LinkedList LinkedListInsert(LinkedList L,int i,ElemType x) //插入 
    {
        Node *pre;                     
        pre = L;
        int tempi = 0;
        for (tempi = 1; tempi < i; tempi++) {
        	pre = pre->next;                  
    	}
        Node *p;                                
        p = (Node *)malloc(sizeof(Node));
        p->data = x; 
        p->next = pre->next;
        pre->next = p;
         
        return L;                           
    } 
    
    LinkedList LinkedListDelete(LinkedList L,ElemType x)//删除 
    {
        Node *p,*pre;                    
        p = L;
        while(p->data != x) {               
            pre = p; 
            p = p->next;
        }
        pre->next = p->next;          
        free(p);
        return L;
    } 
    LinkedList LinkedListLook(LinkedList L,ElemType x)//查找 
    {
    	int q=0;
    	Node *p,*pre;
    	p=L;
    	while(p->data!=x)
    	{
    		pre=p;
    		p=p->next;
    		q++;
    	}
    	printf("该数据在链表中的位置是第%d个!
    ",q);
    }
    int main() 
    {
        LinkedList list,start;
        int a;
        printf("1.前插法创建单链表
    ");
        printf("2.后插法创建单链表
    ");
        printf("请输入:");
        scanf("%d",&a);
        if(a==1)
        {
        	printf("前插法:请输入单链表的数据:"); 
        	list = LinkedListCreat_H();
        	for(start = list->next; start != NULL; start = start->next) 
    		{
        		printf("%d ",start->data);
    		}
        	printf("
    ");
    	}
     	else if(a==2)
     	{
     		printf("后插法:请输入单链表的数据:");
    		list = LinkedListCreat_E();
    		for(start = list->next; start != NULL; start = start->next) 
    		{
        		printf("%d ",start->data);
    		}
    		printf("
    ");
    	} 
        int i;
        ElemType x;
        printf("请输入插入数据的位置:");
        scanf("%d",&i);
        printf("请输入要插入的值:");
        scanf("%d",&x);
        LinkedListInsert(list,i,x);
        for(start=list->next;start!=NULL;start=start->next)
        {
        	printf("%d ",start->data);
    	}
        printf("
    ");
        printf("请输入要删除的元素的值:");
        scanf("%d",&x);
        LinkedListDelete(list,x); 
        for(start = list->next; start != NULL; start = start->next) {
        	printf("%d ",start->data);
    	}
        printf("
    ");
        printf("请输入要查找的数据:");
    	scanf("%d",&x);
        LinkedListLook(list,x);
        return 0;
    }
    
  • 相关阅读:
    计算机的启动过程
    project
    ERROR
    告别,是另一种体验
    Kean博客2006年9月-2007年8月链接
    AutoCAD .NET开发大师Kean有价值的博客 2006年8月 .NET内容整理
    VS2010 VS2012拖拽NumericUpDown控件直接卡死的解决办法
    2006-7有价值的Kean博客——Calling ObjectARX functions from a .NET Application(PInvoke)
    使用NetApi渲染Cad模型
    Kean专题:拖动一个属性块(JIG拖拽)
  • 原文地址:https://www.cnblogs.com/cnlik/p/11851863.html
Copyright © 2011-2022 走看看