线性表 —— 带头结点单链表(C语言)

线性表之链表操作

1. 结构定义

typedef struct LNode* LinkedList;

typedef struct LNode
{
    ElemType data;
    struct LNode* next;
}LNode;

2. 链表初始化

bool InitLinkedList(LinkedList &L)
{
    L = (LinkedList)malloc(sizeof(struct LNode));
    if(!L) exit(OVERFLOW);
    L->next = NULL;
    return success;
}

3. 在链表指定位置插入元素

/*
    @description: 在链表指定位置插入元素
 */
bool LinkedListInsert(LinkedList &L, int index, ElemType e)
{
    LNode* p = L; int i = 1;
    while(p && i < index)
    {
        p = p->next;
        i++;
    }
    if(!p || i > index) return failed;
    LNode* s = (LNode* )malloc(sizeof(LNode));
    s->data = e;
    s->next = p->next;
    p->next = s;
    return success;
}

4. 删除链表指定位置的元素

/*
    @description: 删除链表指定位置的元素
 */
bool LinkedListDelete(LinkedList &L, int index)
{
    LNode* p = L; int i = 1;
    while(p->next && i < index)
    {
        p = p->next;
        i++;
    }
    if(!p->next || i > index) return failed;
    LNode* s = p->next;
    p->next = s->next;
    free(s);
    return success;
}

5. 获取指定位置元素

/*
    @description: 获取链表指定位置元素
 */
bool GetLinkedListElem(LinkedList L, int index, ElemType &e)
{
    LNode* p = L->next; int i = 1;
    while(p && i < index)
    {
        p = p->next;
        i++;
    }
    if(!p || i > index)return failed;
    e = p->data;
    return success;
}

6. 获取指定元素在链表中的位置

/*
    @description: 获取链表指定位置元素
    @return: 查询不到元素e则返回-1, 反之返回位置index
 */
int LocateLinkedListElem(LinkedList L, ElemType e)
{
    LNode* p = L->next; int i = 1;
    while(p && p->data != e)
    {
        p = p->next;
        i++;
    }
    if(p)return i;
    return -1;
}

7. 从头到尾遍历链表

void ReadLinkedList(LinkedList &L)
{
    LNode* p = L->next;
    while(p)
    {
        printf("%d\n",p->data);
        p = p->next;
    }
}
/*头结点头文件 hlinklist.h*/ #include <stdio.h> typedef int datatype; typedef struct link_node { datatype data; struct link_node *next; }node; /*初始化链表*/ node *init() { node *head; head=(node *)malloc(sizeof(node)); head->next=0; return head; } /*尾插法创建一个头结点链表*/ node *creat(node *head) { node *r,*s; int x; r=head; printf("在新链表输入数据以0结束:"); scanf("%d",&x); while(x) { s=(node*)malloc(sizeof(node)); s->data=x; r->next=s; r=s; scanf("%d",&x); } r->next=0; return head; } /*打印链表的结点值*/ void print(node *head) { node *p; p=head->next; if(!p) printf("链表内容为空!"); else while(p) { printf("%5d",p->data); p=p->next; } printf("\n"); } /*在单链表查找第i个结点的地址*/ node *find(node *head,int i) { node *p=head; int j=0; if(i<0) {printf("不存在!");return 0;} if(i==0) return head; while(p&&i!=j) { p=p->next; j++; } return p; } /*在头结点单链表第i个位置后插入一个数*/ node *insert(node *head,int i,datatype x) { node *p,*q; q=find(head,i); if(!q) { printf("插入的位置不存在!\n");return head;} else { p=(node *)malloc(sizeof(node)); p->data=x; p->next=q->next; q->next=p; } return head; } /*在头结点单链表删除一个为x的值*/ node *dele(node *head,datatype x) { node *pre=head,*p; p=head; while(p&&p->data!=x) { pre=p;p=p->next; } if(p) { pre->next=p->next; free(p); } return head; } /*把头结点单链表倒置(以结点形式 )*/ node *Dao_zhi(node *head) { node *p,*s; p=head->next; head->next=NULL; while(p) { s=p; p=p->next; s->next=head->next; head->next=s; } return head; } /*删除链表重复的结点 */ node *dele1(node *head) { node *pre,*p,*q; if(head->next==0||!head->next->next) { printf("链表为空!"); return head; } //pre=head->next; q=head->next; while(q) { pre=q; p=q->next; while(p) { while(p&&q->data!=p->data) { pre=p;p=p->next; } if(p) { pre->next=p->next; free(p); } p=pre->next; } q=q->next; } return head; }
这是一个关于编程的问题,我可以回答。下面是一个示例C语言程序: ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next; } Node; void delete_common(Node *l1, Node *l2) { Node *p = l1->next; Node *pre = l1; while (p) { Node *q = l2->next; int found = 0; while (q) { if (p->data == q->data) { found = 1; break; } q = q->next; } if (found) { pre->next = p->next; free(p); p = pre->next; } else { pre = p; p = p->next; } } } int main() { Node *l1 = (Node *)malloc(sizeof(Node)); l1->next = NULL; Node *l2 = (Node *)malloc(sizeof(Node)); l2->next = NULL; Node *p = l1; for (int i = 1; i <= 10; ++i) { Node *node = (Node *)malloc(sizeof(Node)); node->data = i; node->next = NULL; p->next = node; p = p->next; } p = l2; for (int i = 5; i <= 15; ++i) { Node *node = (Node *)malloc(sizeof(Node)); node->data = i; node->next = NULL; p->next = node; p = p->next; } printf("Before delete_common:\n"); p = l1->next; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); delete_common(l1, l2); printf("After delete_common:\n"); p = l1->next; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); return 0; } ``` 这个程序实现了在头结点的l1删除在l2存在的元素的功能。具体来说,它遍历l1的每个元素,然后在l2查找是否存在相同的元素。如果存在,则在l1删除该元素。最后,程序输出删除前和删除后的l1元素,以验证程序的正确性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值