Linked list

Singly Linked list

In a singly linked list, we omit the prev pointer in each element
Address of the head node give us access of the complete list

Array VS linked list

Array Linked list
Cost of accessing an element Ο(1) Ο(n) in average case
Memory requirement fixed size no unused memory , extra memory for pointer variables, more flexible
Cost of Inserting element at beginning Ο(n) Ο(1)
Inserting element at the end ① Ο(1) if array isn't full. ② Ο(n) if array is full. Ο(n)
Inserting element at ith element Ο(n) Ο(n)

How to create a linked list

Implement in C :
1.create a node first:

struct Node
{
    int data;
    struct Node* next;   
}* head;            

2.Put your element in the list:

void Put(int x)
{
    struct Node* temp =(struct Node*)malloc(sizeof(struct Node));  //create a node
    temp->data=x;  //put in the data
    temp->next=head;  //make next node as head
    head=temp;  //let head point to the next node
}

3.Traverse the linked list
psudocode first:

node* temp=A
while( temp -> link != NULL)
{
   temp=temp -> link;
   print" temp ->data
}

In C code:

void Display()
{
    struct Node* temp=head;
    printf("Current list is ");
    while(temp !=NULL)  //traverse
    {
        printf("%d",temp->data);
        temp=temp->next;
    }
    printf("\n");
}

4.Free memory
psudocode first:

node = head              
while node != null:     
    temp = node         
    node = node.next    
    free temp          
head = null 

In C code:

void Free(struct Node*head)
{
    struct Node*temp;
    while(head != NULL)  
    {
        temp=head;  
        head=head->next;  
        free(temp);  
    }
    head=NULL;  
}

View complete C code of create linked list here.

Inserting into a linked list

LIST -Insert (L, x)
next [x ]←head [L]
if head [L]≠NIL 
then prev [head [L ]]←x 
head [L]←x 
prev [x]←NIL

Implement in C:

void Insert(int data,int n)
{
    struct Node* temp1=(struct Node*)malloc(sizeof(struct Node));
    temp1->data=data;
    temp1->next=NULL;
    if(n == 1)  
    {
        temp1->next=head;  
        head=temp1;  
        return;
    }
    struct Node* temp2=(struct Node*)malloc(sizeof(struct Node));
    temp2=head;
    int i;
    for(i=0; i<n-2; i++)  
    {
        temp2 = temp2->next;
    }
    temp1->next = temp2->next;
    temp2->next = temp1;
}

View complete C code of insertion here.

Searching a linked list

List _ Search(L,  k)
x ←head[L]
   while x≠NIL and key[x]≠k
   do x←next[x]
return x

It takesΘ(n) time in the worst case.
**Implement in C: **

void Search(int n)
{
    while(head != NULL)
    {
        if(head ->data == n)
        {
            printf("element found\n");
            return;
        }
        head =head->next;
    }
    printf("element don't exist in this list");
}

View complete C code of Search particular element in linked list here.

Delete

Delete at nth position :
void Delete(int n)
{
    struct Node* temp1=head;  
    if(n==1)
    {
        head=temp1->next;   
        free(temp1);  
        return;
    }
    int i;
    for(i=0; i<n-2; i++)
    {
        temp1=temp1->next;  
    }
    struct Node*temp2 = temp1-> next;  
    temp1->next = temp2->next;  
    free(temp2);   
}

**View complete Delete code here. **

Sort linked list

We use merge sort to sort linked list
1.Split your list into two parts


void split(struct node* Node,struct node **front,struct node **back)
{
    struct node* a;
    struct node* b;
    if(Node==NULL || Node->next==NULL)
    {
        *front=Node;
        *back=NULL;
    }
    else
    {
        a=Node;
        b=Node->next;
        while(b != NULL)
        {
            b=b->next;
            if(b!= NULL)
            {
                a=a->next;
                b=b->next;
            }
        }
        *front=Node;
        *back =a->next;
        a->next= NULL;
    }
}
struct node* merge(struct node *a,struct node *b)
{
    struct node* list=NULL;
    if(a==NULL)
    {
        return b;
    }
    else if(b==NULL)
    {
        return a;
    }
    if(a->data > b->data)
    {
        list=b;
        list->next=merge(a,b->next);
    }
    else
    {
        list=a;
        list->next=merge(a->next,b);
    }
    return list;
}
void mergesort(struct node** Node)
{
    head=*Node;
    struct node* a=NULL;
    struct node* b=NULL;

    if(head==NULL || head->next== NULL)
    {
        return;
    }
    split(head,&a,&b);
    mergesort(&a);
    mergesort(&b);
    *Node=merge(a,b);
}

Complete code here

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,567评论 0 23
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,360评论 0 33
  • 周五的晚上,高峰期,实在不好打车。她想取消订单的那一秒,系统分配了一辆车,2.6公里开过来,还都是红色的拥堵路段。...
    良仔lz阅读 1,148评论 0 0
  • 柴静的《看见》是从她刚进央视的时候,历经10年风雨成长,所写出来的一本书。在这本书中我们不光能了解柴静这个人,还能...
    钱莱爱读书阅读 3,199评论 0 1
  • 艾苏搬着小板凳坐在母亲的身边,母亲正用一把大铁剪在给公鸡放血。公鸡的脖子和翅膀被母亲的大手牢牢地握着,鲜红的血从脖...
    斑斑的四喜丸子阅读 3,134评论 0 2

友情链接更多精彩内容