线性表的链式存储实现(有头结点)

来源:互联网 发布:动画演示软件 编辑:程序博客网 时间:2024/05/21 09:30
//线性表的链式存储实现(有头结点)#include <stdio.h>struct node{    int element;    struct node* next;};struct node* Insert(struct node* head,int i,int number){    int j=0;    struct node* p;    p=head;    while(p&&j<i-1){        p=p->next;        j++;    }    if(p==NULL){        printf("Error\n");        return;    }    struct node* t;    t=(struct node*)malloc(sizeof(struct node));    t->element=number;    t->next=p->next;    p->next=t;    return;}Delete(struct node* head,int i){     struct node* p;     p=head;     int location=0;     while(location<i-1&&p){         p=p->next;         location++;     }     if(location!=i-1||p->next==NULL){//元素不到i或者i位置为NULL        printf("Error\n");        return;     }     struct node* temp=p->next;     p->next=temp->next;     free(temp);     return;};struct node*Init(struct node* head){    head=(struct node*)malloc(sizeof(struct node));    head->next=NULL;    return head;};int main(){    struct node* head;    //初始化    head=Init(head);    //插入    Insert(head,1,10);//插到第i个位置    //删除    Delete(head,1);//删除第i个元素    return 0;}

1 0
原创粉丝点击