7-6 链表操作-插入、查找和删除 (100 分)

请编写创建链表和输出链表的函数。对于以下数据结点的结构定义,
针对带头结点的链表,请编程完成以下功能。
struct LNode{
int data;                  //数据域
struct LNode *next;        //指针域
};
struct LNode *head;            //头指针
输入数据包含若干组命令和数据,一组数据中的第1个字符代表命令,
接下来的是该命令需要的数据。
(1)如果命令是I,功能为创建空链表,对应函数:void List_Init(head);
(2)如果命令是A,后跟一个整数data,功能为向链表尾部追加一个数据data,
对应函数:void List_Append(head,data);
(3)如果命令是C,后跟一个整数N,再跟N个整数,功能为向链表尾部追加N个
数据,可通过调用List_Append()函数实现;
(4)如果命令是P,功能遍历输出链表中所有数据,数据间用一个空格分隔,对应
函数:void List_print(head),如果链表未建立,输出“List not defined!”,
如果链表为空输出:“List is empty!”。

以上为上一题目的内容,在此基础上,增加设计如下功能:

(5)如果命令是N,后跟一个整数n和d,功能为向链表的第n个位置插入数据d,
可通过调用List_Insert(head,n,d)函数实现;
(6)如果命令是F,后跟一个整数d,功能为在链表查找数据d,返回其位序,若
找不到返回-1。可通过调用List_Find(head,d)函数实现;
(7)如果命令是D,后跟一个整数n,功能为删除链表第n个位置的数据,可通过
调用List_Delete(head,n)函数实现。

输入格式:

若干组命令和数据,很多命令和数据写在一起请注意识别。

输出格式:

根据输入命令输出相应内容,详见输出样例。

输入样例:

I C 5 100 200 300 400 500 P
F 500 N 3 23 N 5 31 P
D 4 P F 99
A 6 P

输出样例:

100 200 300 400 500
index:5
100 200 23 300 31 400 500
100 200 23 31 400 500
Not Found!
100 200 23 31 400 500 6

输入样例:

I A 100 A 200 A 300 C 4 400 500 600 700 P
F 800 F 500
N 4 4 N 5 5 P
D 2 D 2 D 2 P
D 2 D 2 D 2 P
D 1 D 1 P
D 1 D 1 P

输出样例:

100 200 300 400 500 600 700
Not Found!
index:5
100 200 300 4 5 400 500 600 700
100 5 400 500 600 700
100 600 700
700
List is empty!

 

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

struct LNode{
    int data;                  //数据域
    struct LNode *next;        //指针域
};
struct LNode *head = NULL;
struct LNode *List_Init(struct LNode *head)//新建一个空链表
{
    head=(struct LNode *)malloc(sizeof(struct LNode));
    head->next=NULL;
    head->data=INT_MAX;
    return head;
}
void List_Append(struct LNode *head,int data)//向链表的尾部追加一个数据data
{
    struct LNode *p,*q;
    p=head;
    q=(struct LNode *)malloc(sizeof(struct LNode));
    q->data=data;
    q->next=NULL;
    while(p->next!=NULL){
        p=p->next;
    }
    p->next=q;
}
void List_print(struct LNode *head)//遍历输出链表中的所有数据,如果链表未建立,输出"List not defined!"如果链表为空输出:"List is empty!"
{
    struct LNode *p;
    if(head==NULL){
        printf("List not defined!\n");
        return;
    }
    if(head->next==NULL){
        printf("List is empty!\n");
        return;
    }
    p=head->next;//因为这是一个带头结点的链表,所以head实际上是没有数的,它指向的才存了第一个数
    printf("%d",p->data);
    p=p->next;
    while(p!=NULL){
        printf(" %d",p->data);
        p=p->next;
    }
    printf("\n");
}
void List_Insert(struct LNode *head,int n,int d){
    int i;
    struct LNode *p,*q;
    q=(struct LNode *)malloc(sizeof(struct LNode));
    q->data=d;
    q->next=NULL;
    p=head;
    for(i=1;i<n;i++)p=p->next;
    q->next=p->next;
    p->next=q;
}
int List_Find(struct LNode *head,int d)//在链表中查找数据d,并返回其位序,如果找不到则返回-1
{
    int cnt=1;
    struct LNode *p;
    p=head->next;
    while(p!=NULL){
        if(p->data==d)return cnt;
        p=p->next;
        cnt++;
    }
    return -1;
}
void List_Delete(struct LNode *head,int n)//删除链表第n个位置的数据
{
    struct LNode *p,*q;
    int i;
    p=head;
    q=p->next;
    for(i=1;i<n;i++){
        if(p!=NULL)p=p->next;
    }
    if(p==NULL || p->next==NULL)return;
    q=p->next;
    p->next=p->next->next;
    free(q);
}
int main()
{
    //head=NULL;
    char c;
    int i,n,N,data;
    while(scanf("%c",&c)!=EOF){
        if(c=='I')head = List_Init(head);
        if(c=='A'){
            scanf("%d",&data);
            List_Append(head,data);
        }
        if(c=='C'){
            scanf("%d",&N);
            for(i=0;i<N;i++){
                scanf("%d",&data);
                List_Append(head,data);
            }
        }
        if(c=='P')List_print(head);
        if(c=='N'){
            scanf("%d%d",&n,&data);
            List_Insert(head,n,data);
        }
        if(c=='F'){
            scanf("%d",&data);
            if(List_Find(head,data)==-1)printf("Not Found!\n");
            else printf("index:%d\n",List_Find(head,data));
        }
        if(c=='D'){
            scanf("%d%d",&n);
            List_Delete(head,n);
        }
    }
    return 0;
}

 

 

 

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双向链表是一种常见的数据结构,它可以在 O(1) 时间内实现插入删除操作,并且可以双向遍历链表。 以下是一个简单的双向链表的实现,包括插入删除查找操作: ```python class Node: def __init__(self, data): self.data = data self.prev = None self.next = None class DoublyLinkedList: def __init__(self): self.head = None def insert(self, data): new_node = Node(data) if self.head is None: self.head = new_node else: cur_node = self.head while cur_node.next is not None: cur_node = cur_node.next cur_node.next = new_node new_node.prev = cur_node def delete(self, data): cur_node = self.head while cur_node is not None: if cur_node.data == data: if cur_node.prev is not None: cur_node.prev.next = cur_node.next else: self.head = cur_node.next if cur_node.next is not None: cur_node.next.prev = cur_node.prev return else: cur_node = cur_node.next def find(self, data): cur_node = self.head while cur_node is not None: if cur_node.data == data: return True else: cur_node = cur_node.next return False ``` 在上面的代码中,我们定义了一个 `Node` 类来表示双向链表中的节点,包括节点存储的数据、前驱节点和后继节点。然后,我们定义了 `DoublyLinkedList` 类来表示双向链表。在 `DoublyLinkedList` 类中,我们实现了插入删除查找操作。 在 `insert` 方法中,我们首先创建一个新节点,并将其插入链表的末尾。如果链表为空,则将新节点作为头节点。否则,我们遍历整个链表,直到找到链表的最后一个节点,然后将新节点插入链表的末尾。 在 `delete` 方法中,我们遍历整个链表查找删除的节点。如果找到了该节点,则将其从链表删除。如果要删除的节点是头节点,则将头指针指向下一个节点。否则,我们将前驱节点的 `next` 指针指向要删除节点的后继节点,将后继节点的 `prev` 指针指向要删除节点的前驱节点。 在 `find` 方法中,我们遍历整个链表查找存储指定数据的节点。如果找到了这个节点,则返回 True,否则返回 False。 在使用双向链表时,我们需要注意以下几点: - 双向链表有两个指针,因此需要更多的内存来存储它们。 -插入删除节点时,需要更新前驱节点和后继节点的指针。 - 双向链表可以双向遍历,因此可以从前往后或从后往前遍历链表- 如果双向链表只有一个指针指向头节点,则可以将其视为单向链表

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值