复习单链表的实现

大三了,作为非计算机专业的学生,找工作真的有点压力,过几天就是暑期实习生的面试了,我知道这是最后的一次机会,所以一定好好复习,最近把数据结构看了一遍,从基本的数组链表,到高级的红黑树,哈希表,决定都用博客来记录一下,以下是单链表的实现:
/*****************************单链表总结*********************************/
/******************************2013/5/29*********************************/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct Node
{
    int data;
    struct Node*next;
}SLnode, *SLNode;

/*****************************初始化并创建一个单链表**********************/
SLNode CreateSList()
{
    int len;
    int val;
    SLNode head = (SLNode)malloc(sizeof(SLnode));//创建一个头结点,并使其数据域为空
    if(head == NULL)
    {
        printf("内存分配失败\n");
        exit(-1);
    }
    head->data = NULL;
    SLNode tail = head;
    printf("请输入链表的节点个数:len=");
    scanf("%d",&len);
    for(int i = 0; i<len; i++)
    {
        printf("请输入第%d个节点的值:",i+1);
        scanf("%d",&val);
        SLNode pNew = (SLNode)malloc(sizeof(SLnode));
        if(pNew==NULL)
        {
            printf("内存分配失败\n");
            exit(-1);
        }
        pNew->data = val;
        tail->next = pNew;
        pNew->next = NULL;
        tail = pNew;
    }
    return head;
}
/*****************************获取当前单链表的长度**************************/
int ListLength(SLNode head)
{
    SLNode p = head;
    int size = 0;
    while(p->next != NULL)
    {
        p = p->next;
        size++;
    }
    return size;
}
/*****************************在第i(0<= i <= size)个位置插入节点******************************/
int ListInsert(SLNode head, int pos, int x)
{
    SLNode p, q;
    int j = -1;
    p = head;
    
    while(p->next != NULL && j < pos-1)//先找到插入位置pos-1
    {
        p = p->next;
        j++;
    }
    if(j != pos-1)
    {
       printf("插入参数出错\n");
       return 0;
    }
   q = (SLNode)malloc(sizeof(SLnode));
   if(q == NULL)
   {
        printf("内存分配失败\n");
        exit(-1);
   }
   q->data = x;
   q->next = p->next;
   p->next = q;
   
   return 1;   
}
/*****************************在第i(0<= i <= size-1)个位置删除节点******************************/
int ListDelete(SLNode head, int pos, int *x)
{
    SLNode p, q;
    int j = -1;
    p = head;
    while(p->next != NULL && j < pos-1)
    {
        p = p->next;
        j++;
    }
    if(j != pos-1)
    {
        printf("删除位置出错\n");
        return 0;
    }
    q = p->next;
    p->next = q->next;
    *x = q->data;
    free(q);
    
    return 1;
}
/*****************************遍历单链表******************************/
void displayList(SLNode head)
{
    SLNode p = head;
    while(p->next != NULL)
    {
        printf("%d  ",p->next->data);
        p = p->next;
    }
    return ;

}
/*****************************撤销单链表******************************/
void Destroy(SLNode *head)
{
    SLNode p, q;
    p = *head;
    while(p != NULL)
    {
        q = p;
        p = p->next;
        free(q);
    }
    *head = NULL;
    return ;
}
/*****************************测试函数******************************/
int main()
{
    SLNode head;
    int x;
    
    head = CreateSList();
    displayList(head);
   
    printf("\n链表的长度为:%d",ListLength(head));

   if(ListInsert(head,4,30))
        printf("\n插入成功,插入的元素为:%d\n",30);     
   else
        printf("插入失败\n");   
    displayList(head);
  
    if(ListDelete(head,3,&x))   
        printf("\n删除成功,删除的元素为:%d\n",x);   
    else  
        printf("删除失败\n");
    displayList(head);  
    Destroy(&head); 
    
    return 0;
}

你可能感兴趣的:(编程)