在较复杂的线性表中,一个数据元素可以由若干个数据项组成。在这种情况下,常把数据元素称为记录,含有大量记录的线性表又称为文件
概念:用一组地址连续的存储单元依次存储线性表的数据元素,这种存储结构的线性表称为顺序表。
特点:逻辑上相邻的数据元素,物理次序也是相邻的。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NpMMu49F-1678256000198)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20230301091717476.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VhHZYb70-1678256000199)(image/2021-03-12-21-00-33.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SclxGWqi-1678256000199)(image/2021-03-12-21-01-53.png)]
数组有以下限制:
链表是可以克服数组所有限制的数据结构。 链表是非常有用的,因为,
链式存储设计时,各个不同结点的存储空间可以不连续,但是结点内的存储单元地址则必须连续。
typedef struct LNode {int value; // value中存放结点值域,默认是int型struct Lnode *next;//指向后继结点的指针
}LNode; // 定义单链表结点类型
1234
上述定义了一个结构体,包括两部分,一是值域,二是指针域;每当定义一个结点都会产生这两个区域。
这个value与next域必须是挨着的,称这个结点为内部。
假如我们定义若干个不同的结点,把它们连接起来成为一个单链表。
value区域,箭头区域则是指针域指向逻辑上相链接的下一个结点,但是它们在空间上不一定连续。
而对于它们的结点内部一定是连续的。若第一个结点占用两个地址,那么value域的起始地址是1,则指针域的地址就是2。同理若第二个结点的value地址是10,则next域就是11。
因此,在进行链式存储设计时,各个不同结点完全可以存储在不连续的空间上,而对于同一个结点内部,不论划分多少个区域,两个也好,三个也罢,总之内部的单元存储地址是连续的
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4jLx1utk-1678256055499)(image/循环单向链表.png)]
链表的最后一个节点包含链表的第一个节点的地址。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sgXy3iXl-1678256055501)(image/2021-03-12-21-21-07.png)]
struct node
{ int data; struct node *next;
};
struct node *head, *ptr;
ptr = (struct node *)malloc(sizeof(struct node *));
#includelist li;
forward_list li;
#include
#include
struct node
{int data;struct node *next;
};
struct node *head;void beginsert();
void lastinsert();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
int main()
{int choice = 0;while (choice != 7){printf("*********Main Menu*********\n");printf("Choose one option from the following list ...\n");printf("===============================================\n");printf("1.Insert in begining\n2.Insert at last\n");printf("3.Delete from Beginning\n4.Delete from last\n");printf("5.Search for an element\n6.Show\n7.Exit\n");printf("Enter your choice?\n");scanf("%d", &choice);switch (choice){case 1:beginsert();break;case 2:lastinsert();break;case 3:begin_delete();break;case 4:last_delete();break;case 5:search();break;case 6:display();break;case 7:exit(0);break;default:printf("Please enter valid choice..");}}
}
void beginsert()
{struct node *ptr, *temp;int item;ptr = (struct node *)malloc(sizeof(struct node));if (ptr == NULL){printf("OVERFLOW");}else{printf("Enter the node data?");scanf("%d", &item);ptr->data = item;if (head == NULL){head = ptr;ptr->next = head;}else{temp = head;while (temp->next != head)temp = temp->next;ptr->next = head;temp->next = ptr;head = ptr;}printf("node inserted\n");}}
void lastinsert()
{struct node *ptr, *temp;int item;ptr = (struct node *)malloc(sizeof(struct node));if (ptr == NULL){printf("OVERFLOW\n");}else{printf("Enter Data?");scanf("%d", &item);ptr->data = item;if (head == NULL){head = ptr;ptr->next = head;}else{temp = head;while (temp->next != head){temp = temp->next;}temp->next = ptr;ptr->next = head;}printf("node inserted\n");}}void begin_delete()
{struct node *ptr;if (head == NULL){printf("UNDERFLOW");}else if (head->next == head){head = NULL;free(head);printf("node deleted\n");}else{ptr = head;while (ptr->next != head)ptr = ptr->next;ptr->next = head->next;free(head);head = ptr->next;printf("node deleted\n");}
}
void last_delete()
{struct node *ptr, *preptr;if (head == NULL){printf("UNDERFLOW");}else if (head->next == head){head = NULL;free(head);printf("node deleted\n");}else{ptr = head;while (ptr->next != head){preptr = ptr;ptr = ptr->next;}preptr->next = ptr->next;free(ptr);printf("node deleted\n");}
}void search()
{struct node *ptr;int item, i = 0, flag = 1;ptr = head;if (ptr == NULL){printf("Empty List\n");}else{printf("Enter item which you want to search?\n");scanf("%d", &item);if (head->data == item){printf("item found at location %d", i + 1);flag = 0;}else{while (ptr->next != head){if (ptr->data == item){printf("item found at location %d ", i + 1);flag = 0;break;}else{flag = 1;}i++;ptr = ptr->next;}}if (flag != 0){printf("Item not found\n");}}}void display()
{struct node *ptr;ptr = head;if (head == NULL){printf("nothing to print");}else{printf("printing values ... \n");while (ptr->next != head){printf("%d\n", ptr->data);ptr = ptr->next;}printf("%d\n", ptr->data);}}