有表头链表的基本操作

来源:互联网 发布:python 元组转换字典 编辑:程序博客网 时间:2024/06/07 15:38
今天没事做,重温了一下单链表的基本操作,运行的平台是ubuntu14.04上的GCC。其中包括初始化单链表,向链表中添加元素,从链表中删除元素,遍历链表并打印,按元素查找,销毁链表。
#include<stdio.h>#include<stdlib.h>typedef struct node{    int ID;    int data;    struct node *pNext;}Node;//有头节点的链表的操作Node *head = NULL;//函数声明int init();int addNode(int ID , int data);int deleteNode_addr(int index);void destroyNodeList();void show(Node *list);int find(int find_id);void main(){        init();//初始化        int a;        do        {                printf("1.增加  2.删除 3.打印 4.查找 0.退出\n");                scanf("%d",&a);                int x,y;                int m;                int t;                switch(a)                {                    case 1:                               printf("输入ID与data(中间用空格隔开):\n");                               scanf("%d %d",&x,&y);                               if (addNode(x,y)==1) printf("添加成功!\n");                               else printf("添加失败!");                               break;                    case 2:                               printf("输入删除的位置:\n");                               scanf("%d",&m);                               if (deleteNode_addr(m)==1) printf("删除成功!\n");                               else printf("删除失败!\n");                               break;                    case 3:                               printf("打印这个表:\n");                               show(head);                               break;                    case 4:                                printf("输入需要查找的ID:");                                scanf("%d",&t);                                if(find(t)==1) printf("表中有这个ID!\n");                                else printf("表中没有这个ID!\n");                    default:                                break;                }           }while(a>0&&a<=4);        destroyNodeList();        printf("清除这个表成功!");}//创建链表,头结点data=0,pNext=NULL;int init(){    head = (Node*) malloc(sizeof(Node));    if(NULL == head)    {        printf("初始化错误!\n");        return 0;    }    else    {        head->data = 0;                head->ID = 0;        head->pNext = NULL;                printf("初始化成功!\n");        return 1;    }}//增加节点int addNode(int ID , int data){    if(NULL == head)    {        return 0;    }    Node* p = head->pNext;    Node* q = head;    while(NULL != p)    {        q = p;        p = p->pNext;    }    Node* new_node = (Node*)malloc(sizeof(Node));    new_node->ID = ID;    new_node->data = data;    new_node->pNext = NULL;    q->pNext = new_node;    new_node->pNext = NULL;    return 1;   }//按节点位置删除int deleteNode_addr(int index){    //bunengshanchubiaotou    if(index == 0)    {        printf("删除无效,表已是空表!\n");        return 0;    }    //判断是否为空表    if(NULL == head)    {        printf("删除无效,表已是空表!\n");        return 0;           }    //求表的长度    Node* p = head->pNext;    int length = 0;    while(NULL != p)    {        length ++;        p = p->pNext;    }    //判断输入的长度是否比表长    if(length < index)    {                printf("输入表的长度错误!\n");        return 0;    }    else    {        Node* q = head;        p = head;                int i;        for(i=0;i<index;i++)        {            q = p;            p = p->pNext;        }        Node* t = p->pNext;        q->pNext = t;        free(p);        return 1;    }}//销毁void destroyNodeList(){    if(NULL == head)    {        return;    }    if(NULL == head->pNext)    {        free(head);        head = NULL;        return;    }    Node* p = head->pNext;    while(NULL != p)    {        Node* tmp = p;        p = p->pNext;        free(tmp);    }    free(head);    head = NULL;}//打印void show(Node *list){    Node *p;        p=list->pNext;//跳过head的内容    if(!p)    {        printf("链表为空!\n");    }    while(p)    {        printf("ID=%d, data=%d, ID_addr=%p, data_addr=%p\n", p->ID ,p->data , &p->ID ,&p->data);        p=p->pNext;    }}//按id查找int find(int find_id){    Node *p;    p=head->pNext;    while(p)    {        if(p->ID==find_id)            return 1;        p=p->pNext;    }    return 0;}
1 0
原创粉丝点击